<commit_before># frozen_string_literal: true

module JWT
  module JWK
    class HMAC
      attr_reader :key
      attr_reader :kid

      KTY = 'oct'.freeze

      def initialize(key, kid = nil)
        raise ArgumentError, 'key must be of type String' unless key.is_a?(String)

        @key = key
        @kid = kid || generate_kid(@key)
      end

      def private?
        true
      end

      # See https://tools.ietf.org/html/rfc7517#appendix-A.3
      def export
        {
          kty: KTY,
          k: key,
          kid: kid
        }
      end

      private

      def generate_kid(hmac_key)
        sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::UTF8String.new(hmac_key),
                                            OpenSSL::ASN1::UTF8String.new(KTY)])
        OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
      end

      class << self
        def import(jwk_data)
          jwk_k = jwk_data[:k] || jwk_data['k']
          jwk_kid = jwk_data[:kid] || jwk_data['kid']

          raise JWT::JWKError, 'Key format is invalid for HMAC' unless jwk_k

          self.new(jwk_k, jwk_kid)
        end
      end
    end
  end<commit_msg>Use the same attribute reader name "keypair" like other jwk's.<commit_after># frozen_string_literal: true

module JWT
  module JWK
    class HMAC
      attr_reader :keypair
      attr_reader :kid

      KTY = 'oct'.freeze

      def initialize(keypair, kid = nil)
        raise ArgumentError, 'keypair must be of type String' unless keypair.is_a?(String)

        @keypair = keypair
        @kid = kid || generate_kid(@keypair)
      end

      def private?
        true
      end

      # See https://tools.ietf.org/html/rfc7517#appendix-A.3
      def export
        {
          kty: KTY,
          k: keypair,
          kid: kid
        }
      end

      private

      def generate_kid(hmac_key)
        sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::UTF8String.new(hmac_key),
                                            OpenSSL::ASN1::UTF8String.new(KTY)])
        OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
      end

      class << self
        def import(jwk_data)
          jwk_k = jwk_data[:k] || jwk_data['k']
          jwk_kid = jwk_data[:kid] || jwk_data['kid']

          raise JWT::JWKError, 'Key format is invalid for HMAC' unless jwk_k

          self.new(jwk_k, jwk_kid)
        end
      end
    end
  end
{'fraction_non_alphanumeric': 0.09953994144709327, 'fraction_numerical': 0.010874111250522794, 'mean_word_length': 1.817432273262662, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '70008', 'n_tokens_mistral': 860, 'n_tokens_neox': 778, 'n_words': 176}