Sha256: 152ed4f1bf87b1d0d5fd5672a505d29f78f554610d3a2bf0db01311776eb7d52

Contents?: true

Size: 1004 Bytes

Versions: 3

Compression:

Stored size: 1004 Bytes

Contents

# frozen_string_literal: true

module JWT
  module Aws
    module KMS
      # Represent a AWS HMAC key
      # https://docs.aws.amazon.com/kms/latest/developerguide/hmac.html
      class HmacKey
        include JWT::JWA::SigningAlgorithm

        MAPPINGS = {
          "HS256" => "HMAC_SHA_256",
          "HS384" => "HMAC_SHA_384",
          "HS512" => "HMAC_SHA_512"
        }.freeze

        def initialize(algorithm:)
          @alg = algorithm
        end

        def sign(data:, signing_key:, **)
          KMS.client.generate_mac(key_id: signing_key, mac_algorithm: mac_algorithm, message: data).mac
        end

        def verify(data:, verification_key:, signature:, **)
          KMS.client.verify_mac(key_id: verification_key, mac_algorithm: mac_algorithm, message: data,
                                mac: signature).mac_valid
        end

        private

        attr_reader :key_id

        def mac_algorithm
          MAPPINGS.fetch(alg, nil)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jwt-aws-kms-1.1.0 lib/jwt/aws/kms/hmac_key.rb
jwt-aws-kms-1.0.0 lib/jwt/aws/kms/hmac_key.rb
jwt-aws-kms-0.6.0 lib/jwt/aws/kms/hmac_key.rb