module JWT::Algos::HmacRbNaCl

Constants

MAPPING
SUPPORTED

Public Instance Methods

key_for_rbnacl(hmac, key) click to toggle source
# File lib/jwt/algos/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/algos/hmac_rbnacl.rb, line 48
def padded_empty_key(length)
  Array.new(length, 0x0).pack('C*').encode('binary')
end
resolve_algorithm(algorithm) click to toggle source
# File lib/jwt/algos/hmac_rbnacl.rb, line 44
def resolve_algorithm(algorithm)
  MAPPING.fetch(algorithm)
end
sign(algorithm, msg, key) click to toggle source
# File lib/jwt/algos/hmac_rbnacl.rb, line 17
def sign(algorithm, msg, key)
  if (hmac = resolve_algorithm(algorithm))
    hmac.auth(key_for_rbnacl(hmac, key).encode('binary'), msg.encode('binary'))
  else
    Hmac.sign(algorithm, msg, key)
  end
end
verify(algorithm, key, signing_input, signature) click to toggle source
# File lib/jwt/algos/hmac_rbnacl.rb, line 25
def verify(algorithm, key, signing_input, signature)
  if (hmac = resolve_algorithm(algorithm))
    hmac.verify(key_for_rbnacl(hmac, key).encode('binary'), signature.encode('binary'), signing_input.encode('binary'))
  else
    Hmac.verify(algorithm, key, signing_input, signature)
  end
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
  false
end