class JWT::JWA::HmacRbNaCl

Attributes

hmac[R]

Public Class Methods

from_algorithm(algorithm) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl.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.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.rb, line 17
def sign(data:, signing_key:)
  Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
  hmac.auth(key_for_rbnacl(hmac, signing_key).encode('binary'), data.encode('binary'))
end
verify(data:, signature:, verification_key:) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 22
def verify(data:, signature:, verification_key:)
  Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
  hmac.verify(key_for_rbnacl(hmac, verification_key).encode('binary'), signature.encode('binary'), data.encode('binary'))
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
  false
end

Private Instance Methods

key_for_rbnacl(hmac, key) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 35
def key_for_rbnacl(hmac, key)
  key ||= ''
  raise JWT::DecodeError, 'HMAC key expected to be a String' unless key.is_a?(String)

  return padded_empty_key(hmac.key_bytes) if key == ''

  key
end
padded_empty_key(length) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 44
def padded_empty_key(length)
  Array.new(length, 0x0).pack('C*').encode('binary')
end