Class: COSE::Key::EC2

Inherits:
COSE::Key show all
Defined in:
lib/cose/key/ec2.rb

Constant Summary collapse

CRV =
-1
X =
-2
Y =
-3
D =
-4
ES256 =
-7
ES384 =
-35
ES512 =
-36
P256 =
1
P384 =
2
P521 =
3

Constants inherited from COSE::Key

ALG, BASE_IV, KID, KTY, KTY_EC2, KTY_OKP, KTY_RSA, KTY_SYMMETRIC, OPS

Instance Attribute Summary collapse

Attributes inherited from COSE::Key

#alg, #base_iv, #kid, #kty, #ops, #raw

Instance Method Summary collapse

Methods inherited from COSE::Key

decode, detect, #to_pem, #to_s, #to_text

Constructor Details

#initialize(attrs = {}) ⇒ EC2

Returns a new instance of EC2.



19
20
21
22
23
24
25
# File 'lib/cose/key/ec2.rb', line 19

def initialize(attrs = {})
  super
  self.crv = attrs[CRV]
  self.x = attrs[X]
  self.y = attrs[Y]
  self.d = attrs[D]
end

Instance Attribute Details

#crvObject

Returns the value of attribute crv.



17
18
19
# File 'lib/cose/key/ec2.rb', line 17

def crv
  @crv
end

#dObject

Returns the value of attribute d.



17
18
19
# File 'lib/cose/key/ec2.rb', line 17

def d
  @d
end

#xObject

Returns the value of attribute x.



17
18
19
# File 'lib/cose/key/ec2.rb', line 17

def x
  @x
end

#yObject

Returns the value of attribute y.



17
18
19
# File 'lib/cose/key/ec2.rb', line 17

def y
  @y
end

Instance Method Details

#curve_nameObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cose/key/ec2.rb', line 27

def curve_name
  case crv
  when P256
    'prime256v1'
  when P384
    'secp384r1'
  when P521
    'secp521r1'
  else
    raise 'Unknown Curve'
  end
end

#digestObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cose/key/ec2.rb', line 40

def digest
  case alg
  when ES256
    OpenSSL::Digest::SHA256
  when ES384
    OpenSSL::Digest::SHA384
  when ES512
    OpenSSL::Digest::SHA512
  else
    raise 'Unknown Algorithm'
  end.new
end

#to_keyObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cose/key/ec2.rb', line 53

def to_key
  key = OpenSSL::PKey::EC.new curve_name
  key.private_key = OpenSSL::BN.new(d, 2) if d
  key.public_key = OpenSSL::PKey::EC::Point.new(
    OpenSSL::PKey::EC::Group.new(curve_name),
    OpenSSL::BN.new([
      '04' +
      x.unpack('H*').first +
      y.unpack('H*').first
    ].pack('H*'), 2)
  )
  key
end