module JWT::Algos::HmacRbNaClFixed

Constants

MAPPING
SUPPORTED

Public Class Methods

padded_key_bytes(key, bytesize) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 40
def padded_key_bytes(key, bytesize)
  key.bytes.fill(0, key.bytesize...bytesize).pack('C*')
end
resolve_algorithm(algorithm) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 36
def resolve_algorithm(algorithm)
  MAPPING.fetch(algorithm)
end
sign(algorithm, msg, key) click to toggle source
# File lib/jwt/jwa/hmac_rbnacl_fixed.rb, line 10
def sign(algorithm, msg, key)
  key ||= ''
  Deprecations.warning("The use of the algorithm #{algorithm} 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 key.is_a?(String)

  if (hmac = resolve_algorithm(algorithm)) && key.bytesize <= hmac.key_bytes
    hmac.auth(padded_key_bytes(key, hmac.key_bytes), 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_fixed.rb, line 22
def verify(algorithm, key, signing_input, signature)
  key ||= ''
  Deprecations.warning("The use of the algorithm #{algorithm} 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 key.is_a?(String)

  if (hmac = resolve_algorithm(algorithm)) && key.bytesize <= hmac.key_bytes
    hmac.verify(padded_key_bytes(key, hmac.key_bytes), signature.encode('binary'), signing_input.encode('binary'))
  else
    Hmac.verify(algorithm, key, signing_input, signature)
  end
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
  false
end