Sha256: 6c4f6e3c4aab7bdda17b3cf361e858eccc42f17eef4fe73d7733f05af13f0de1

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module JWT
  module Algos
    module HmacRbNaCl
      MAPPING   = { 'HS512256' => ::RbNaCl::HMAC::SHA512256 }.freeze
      SUPPORTED = MAPPING.keys
      class << self
        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

        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

        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

        def resolve_algorithm(algorithm)
          MAPPING.fetch(algorithm)
        end

        def padded_empty_key(length)
          Array.new(length, 0x0).pack('C*').encode('binary')
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/jwt-2.8.2/lib/jwt/jwa/hmac_rbnacl.rb
jwt-2.8.2 lib/jwt/jwa/hmac_rbnacl.rb
jwt-2.8.1 lib/jwt/jwa/hmac_rbnacl.rb