Sha256: c6f8026b0bc5c3fdd381102048e595c1153b1993f799007cd94a527fe57407f7

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require "cbor"

module COSE
  module Key
    class EC2
      KTY_LABEL = 1
      ALG_LABEL = 3

      CRV_LABEL = -1
      X_LABEL = -2
      Y_LABEL = -3

      KTY_EC2 = 2

      attr_reader :algorithm, :curve, :x_coordinate, :y_coordinate

      def initialize(algorithm: nil, curve:, x_coordinate:, y_coordinate:)
        if !curve
          raise ArgumentError, "Required curve is missing"
        elsif !x_coordinate
          raise ArgumentError, "Required x-coordinate is missing"
        elsif !y_coordinate
          raise ArgumentError, "Required y-coordinate is missing"
        else
          @algorithm = algorithm
          @curve = curve
          @x_coordinate = x_coordinate
          @y_coordinate = y_coordinate
        end
      end

      def self.from_map(map)
        if map[KTY_LABEL] == KTY_EC2
          new(
            algorithm: map[ALG_LABEL],
            curve: map[CRV_LABEL],
            x_coordinate: map[X_LABEL],
            y_coordinate: map[Y_LABEL]
          )
        else
          raise "Not an EC2 key"
        end
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cose-0.1.0 lib/cose/key/ec2.rb