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.



13
14
15
# File 'lib/ledger_sync/util/signer.rb', line 13

def initialize(str:)
  @str = str
end

Instance Attribute Details

#strObject (readonly)

Returns the value of attribute str.



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

def str
  @str
end

Class Method Details

.escape(str:) ⇒ Object



29
30
31
# File 'lib/ledger_sync/util/signer.rb', line 29

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

.unescape(str:) ⇒ Object



33
34
35
# File 'lib/ledger_sync/util/signer.rb', line 33

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

Instance Method Details

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



17
18
19
20
21
# File 'lib/ledger_sync/util/signer.rb', line 17

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



23
24
25
26
27
# File 'lib/ledger_sync/util/signer.rb', line 23

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