Sha256: e88bed601946a93e713e08f7f27a5bfd72a91749a26b6c0d42875a1fcdb01cc4

Contents?: true

Size: 758 Bytes

Versions: 2

Compression:

Stored size: 758 Bytes

Contents

# frozen_string_literal: true

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

module COSE
  module Algorithm
    class HMAC < Base
      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

2 entries across 2 versions & 1 rubygems

Version Path
cose-0.10.0 lib/cose/algorithm/hmac.rb
cose-0.9.0 lib/cose/algorithm/hmac.rb