Sha256: b2a1a8de85c4ab1e77b43a56281842250488ea2a248c432bd3b325403a166007

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require "cbor"

module COSE
  module Key
    class Base
      LABEL_BASE_IV = 5
      LABEL_KEY_OPS = 4
      LABEL_ALG = 3
      LABEL_KID = 2
      LABEL_KTY = 1

      def self.deserialize(cbor)
        from_map(CBOR.decode(cbor))
      end

      def self.from_map(map)
        enforce_type(map)

        new(
          base_iv: map[LABEL_BASE_IV],
          key_ops: map[LABEL_KEY_OPS],
          alg: map[LABEL_ALG],
          kid: map[LABEL_KID],
          **keyword_arguments_for_initialize(map)
        )
      end

      attr_reader :kid, :alg, :key_ops, :base_iv

      def initialize(kid: nil, alg: nil, key_ops: nil, base_iv: nil)
        @kid = kid
        @alg = alg
        @key_ops = key_ops
        @base_iv = base_iv
      end

      def serialize
        CBOR.encode(map)
      end

      def map
        map = {
          LABEL_BASE_IV => base_iv,
          LABEL_KEY_OPS => key_ops,
          LABEL_ALG => alg,
          LABEL_KID => kid,
        }

        map.reject { |_k, v| v.nil? }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cose-0.7.0 lib/cose/key/base.rb
cose-0.6.1 lib/cose/key/base.rb
cose-0.6.0 lib/cose/key/base.rb