Class: JWT::JWA::HmacRbNaClFixed

Inherits:
Object
  • Object
show all
Includes:
SigningAlgorithm
Defined in:
lib/jwt/jwa/hmac_rbnacl_fixed.rb

Instance Attribute Summary

Attributes included from SigningAlgorithm

#alg

Instance Method Summary collapse

Methods included from SigningAlgorithm

#header, included, #raise_sign_error!, #raise_verify_error!, #valid_alg?

Constructor Details

#initialize(alg, hmac) ⇒ HmacRbNaClFixed

Returns a new instance of HmacRbNaClFixed.



8
9
10
11
# File 'lib/jwt/jwa/hmac_rbnacl_fixed.rb', line 8

def initialize(alg, hmac)
  @alg = alg
  @hmac = hmac
end

Instance Method Details

#sign(data:, signing_key:) ⇒ Object

Raises:



13
14
15
16
17
18
19
# File 'lib/jwt/jwa/hmac_rbnacl_fixed.rb', line 13

def sign(data:, signing_key:)
  signing_key ||= ''
  Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
  raise JWT::DecodeError, 'HMAC key expected to be a String' unless signing_key.is_a?(String)

  hmac.auth(padded_key_bytes(signing_key, hmac.key_bytes), data.encode('binary'))
end

#verify(data:, signature:, verification_key:) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/jwt/jwa/hmac_rbnacl_fixed.rb', line 21

def verify(data:, signature:, verification_key:)
  verification_key ||= ''
  Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
  raise JWT::DecodeError, 'HMAC key expected to be a String' unless verification_key.is_a?(String)

  hmac.verify(padded_key_bytes(verification_key, hmac.key_bytes), signature.encode('binary'), data.encode('binary'))
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
  false
end