Sha256: b28e9d920b5c474462a93759bf9a011962e3656941fb19280d84548598d43804

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module JWT
  module JWA
    class HmacRbNaClFixed
      include JWT::JWA::SigningAlgorithm

      def initialize(alg, hmac)
        @alg = alg
        @hmac = hmac
      end

      def sign(data:, signing_key:)
        signing_key ||= ''
        Deprecations.warning("The use of the algorithm #{alg} 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 signing_key.is_a?(String)

        hmac.auth(padded_key_bytes(signing_key, hmac.key_bytes), data.encode('binary'))
      end

      def verify(data:, signature:, verification_key:)
        verification_key ||= ''
        Deprecations.warning("The use of the algorithm #{alg} 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 verification_key.is_a?(String)

        hmac.verify(padded_key_bytes(verification_key, hmac.key_bytes), signature.encode('binary'), data.encode('binary'))
      rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
        false
      end

      register_algorithm(new('HS512256', ::RbNaCl::HMAC::SHA512256))

      private

      attr_reader :hmac

      def padded_key_bytes(key, bytesize)
        key.bytes.fill(0, key.bytesize...bytesize).pack('C*')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jwt-2.9.1 lib/jwt/jwa/hmac_rbnacl_fixed.rb
jwt-2.9.0 lib/jwt/jwa/hmac_rbnacl_fixed.rb