Sha256: 3af18059946447ae5e2b5650b2b58d2b5a09b591ca1b3958755c5b85c93e558b

Contents?: true

Size: 781 Bytes

Versions: 7

Compression:

Stored size: 781 Bytes

Contents

# frozen_string_literal: true

require "cose/algorithm/base"
require "openssl"

module COSE
  module Algorithm
    class HMAC < Base
      BYTE_LENGTH = 8

      attr_reader :hash_function, :tag_length

      def initialize(*args, hash_function:, tag_length:)
        super(*args)

        @hash_function = hash_function
        @tag_length = tag_length
      end

      def mac(key, to_be_signed)
        mac = OpenSSL::HMAC.digest(hash_function, key, to_be_signed)

        if tag_bytesize && tag_bytesize < mac.bytesize
          mac.byteslice(0, tag_bytesize)
        else
          mac
        end
      end

      private

      def tag_bytesize
        @tag_bytesize ||=
          if tag_length
            tag_length / BYTE_LENGTH
          end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cose-1.3.1 lib/cose/algorithm/hmac.rb
cose-1.3.0 lib/cose/algorithm/hmac.rb
cose-1.2.1 lib/cose/algorithm/hmac.rb
cose-1.2.0 lib/cose/algorithm/hmac.rb
cose-1.1.0 lib/cose/algorithm/hmac.rb
cose-1.0.0 lib/cose/algorithm/hmac.rb
cose-0.11.0 lib/cose/algorithm/hmac.rb