class JWT::JWA::HmacRbNaClFixed

Attributes

hmac[R]

Public Class Methods

from_algorithm(algorithm) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 8
def self.from_algorithm(algorithm)
  new(algorithm, ::RbNaCl::HMAC.const_get(algorithm.upcase.gsub('HS', 'SHA')))
end
new(alg, hmac) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 12
def initialize(alg, hmac)
  @alg = alg
  @hmac = hmac
end

Public Instance Methods

sign(data:, signing_key:) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 17
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:) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 25
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

Private Instance Methods

padded_key_bytes(key, bytesize) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 41
def padded_key_bytes(key, bytesize)
  key.bytes.fill(0, key.bytesize...bytesize).pack('C*')
end