Class: SyrupPay::Jwa::HmacSha256Signature

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::SecurityUtils
Defined in:
lib/jose/jwa/alg/hmac_sha256_signature.rb

Defined Under Namespace

Classes: InvalidKeyLengthError, InvalidVerifyError

Instance Method Summary collapse

Constructor Details

#initializeHmacSha256Signature

Returns a new instance of HmacSha256Signature.



12
13
14
# File 'lib/jose/jwa/alg/hmac_sha256_signature.rb', line 12

def initialize
  @length = 32
end

Instance Method Details

#sign(key, hmac_data) ⇒ Object



16
17
18
19
20
# File 'lib/jose/jwa/alg/hmac_sha256_signature.rb', line 16

def sign(key, hmac_data)
  # valid_key_length!(key, @length)

  OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, hmac_data)
end

#verify!(key, hmac_data, expected) ⇒ Object

Raises:



22
23
24
25
# File 'lib/jose/jwa/alg/hmac_sha256_signature.rb', line 22

def verify!(key, hmac_data, expected)
  actual = UrlSafeBase64.encode64(sign(key, hmac_data))
  raise InvalidVerifyError, "expected : #{expected}, actual : #{actual}" unless secure_compare(actual, expected)
end