lib/cose/key/base.rb in cose-0.5.0 vs lib/cose/key/base.rb in cose-0.6.0

- old
+ new

@@ -3,19 +3,53 @@ 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.enforce_type(map, kty, error_message) - if map[LABEL_KTY] != kty - raise error_message - 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