Module: JWT::Algos::Hmac

Defined in:
lib/jwt/algos/hmac.rb

Constant Summary collapse

SUPPORTED =
%w[HS256 HS512256 HS384 HS512].freeze

Class Method Summary collapse

Class Method Details

.sign(to_sign) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/jwt/algos/hmac.rb', line 8

def sign(to_sign)
  algorithm, msg, key = to_sign.values
  authenticator, padded_key = SecurityUtils.rbnacl_fixup(algorithm, key)
  if authenticator && padded_key
    authenticator.auth(padded_key, msg.encode('binary'))
  else
    OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'sha')), key, msg)
  end
end

.verify(to_verify) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jwt/algos/hmac.rb', line 18

def verify(to_verify)
  algorithm, public_key, signing_input, signature = to_verify.values
  authenticator, padded_key = SecurityUtils.rbnacl_fixup(algorithm, public_key)
  if authenticator && padded_key
    begin
      authenticator.verify(padded_key, signature.encode('binary'), signing_input.encode('binary'))
    rescue RbNaCl::BadAuthenticatorError
      false
    end
  else
    SecurityUtils.secure_compare(signature, sign(JWT::Signature::ToSign.new(algorithm, signing_input, public_key)))
  end
end