Class: LedgerSync::Util::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/util/signer.rb

Constant Summary collapse

HMAC_SHA1_DIGEST =
OpenSSL::Digest.new('sha1')
HMAC_SHA256_DIGEST =
OpenSSL::Digest.new('sha256')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str:) ⇒ Signer

Returns a new instance of Signer.



11
12
13
# File 'lib/ledger_sync/util/signer.rb', line 11

def initialize(str:)
  @str = str
end

Instance Attribute Details

#strObject (readonly)

Returns the value of attribute str.



9
10
11
# File 'lib/ledger_sync/util/signer.rb', line 9

def str
  @str
end

Class Method Details

.escape(str:) ⇒ Object



27
28
29
# File 'lib/ledger_sync/util/signer.rb', line 27

def self.escape(str:)
  CGI.escape(str).gsub(/\+/, '%20')
end

.unescape(str:) ⇒ Object



31
32
33
# File 'lib/ledger_sync/util/signer.rb', line 31

def self.unescape(str:)
  CGI.unescape(str.gsub(/%20/, '+'))
end

Instance Method Details

#hmac_sha1(key:, escape: false) ⇒ Object



15
16
17
18
19
# File 'lib/ledger_sync/util/signer.rb', line 15

def hmac_sha1(key:, escape: false)
  ret = Base64.encode64(OpenSSL::HMAC.digest(HMAC_SHA1_DIGEST, key, str)).strip
  ret = self.class.escape(str: ret) if escape
  ret
end

#hmac_sha256(key:, escape: false) ⇒ Object



21
22
23
24
25
# File 'lib/ledger_sync/util/signer.rb', line 21

def hmac_sha256(key:, escape: false)
  ret = Base64.encode64(OpenSSL::HMAC.digest(HMAC_SHA256_DIGEST, key, str)).strip
  ret = self.class.escape(str: ret) if escape
  ret
end