module JWT::Algos::HmacRbNaCl

Constants

MAPPING
SUPPORTED

Public Class Methods

sign(algorithm, msg, key) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 9
def sign(algorithm, msg, key)
  Deprecations.warning("The use of the algorithm #{algorithm} is deprecated and will be removed in the next major version of ruby-jwt")
  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/jwa/hmac_rbnacl.rb, line 18
def verify(algorithm, key, signing_input, signature)
  Deprecations.warning("The use of the algorithm #{algorithm} is deprecated and will be removed in the next major version of ruby-jwt")
  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

Private Class Methods

key_for_rbnacl(hmac, key) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 31
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
resolve_algorithm(algorithm) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 40
def resolve_algorithm(algorithm)
  MAPPING.fetch(algorithm)
end