class JWT::JWA::HmacRbNaCl
Implementation of the HMAC family of algorithms (using RbNaCl)
Attributes
Public Class Methods
Source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 9 def self.from_algorithm(algorithm) new(algorithm, ::RbNaCl::HMAC.const_get(algorithm.upcase.gsub('HS', 'SHA'))) end
Source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 13 def initialize(alg, hmac) @alg = alg @hmac = hmac end
Public Instance Methods
Source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 18 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
Source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 23 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
Source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 36 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
Source
# File lib/jwt/jwa/hmac_rbnacl.rb, line 45 def padded_empty_key(length) Array.new(length, 0x0).pack('C*').encode('binary') end