File: mac.rb

package info (click to toggle)
ruby-cose 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: ruby: 993; sh: 5; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 761 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

require "cose/recipient"
require "cose/mac0"

module COSE
  class Mac < Mac0
    CONTEXT = "MAC"

    attr_reader :recipients

    def self.keyword_arguments_for_initialize(decoded)
      super.merge(recipients: decoded.last.map { |r| COSE::Recipient.from_array(r) })
    end

    def self.tag
      97
    end

    def initialize(recipients:, **keyword_arguments)
      super(**keyword_arguments)

      @recipients = recipients
    end

    def verify(key, external_aad = nil)
      recipient = recipients.detect { |r| r.headers.kid == key.kid }

      if recipient
        super
      else
        raise(COSE::Error, "No recipient match the key")
      end
    end

    private

    def context
      CONTEXT
    end
  end
end