Class: COSE::Key::EC2
Constant Summary collapse
- CRV =
-1
- X =
-2
- Y =
-3
- D =
-4
- ALGS =
{ ES256: -7, ES384: -35, ES512: -36 }
- CRVS =
{ 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
-
#crv ⇒ Object
Returns the value of attribute crv.
-
#d ⇒ Object
Returns the value of attribute d.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Attributes inherited from COSE::Key
#alg, #base_iv, #kid, #kty, #ops, #raw
Instance Method Summary collapse
- #alg_key ⇒ Object
- #crv_key ⇒ Object
- #crv_name ⇒ Object
- #digest ⇒ Object
-
#initialize(attrs = {}) ⇒ EC2
constructor
A new instance of EC2.
- #to_key ⇒ Object
- #verify(signature, signature_base_string) ⇒ Object
Methods inherited from COSE::Key
Constructor Details
Instance Attribute Details
#crv ⇒ Object
Returns the value of attribute crv.
20 21 22 |
# File 'lib/cose/key/ec2.rb', line 20 def crv @crv end |
#d ⇒ Object
Returns the value of attribute d.
20 21 22 |
# File 'lib/cose/key/ec2.rb', line 20 def d @d end |
#x ⇒ Object
Returns the value of attribute x.
20 21 22 |
# File 'lib/cose/key/ec2.rb', line 20 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
20 21 22 |
# File 'lib/cose/key/ec2.rb', line 20 def y @y end |
Instance Method Details
#alg_key ⇒ Object
30 31 32 33 |
# File 'lib/cose/key/ec2.rb', line 30 def alg_key ALGS.invert[alg] or raise UknownAlgorithm, 'Unknown Algorithm' end |
#crv_key ⇒ Object
35 36 37 38 |
# File 'lib/cose/key/ec2.rb', line 35 def crv_key CRVS.invert[crv] or raise UknownAlgorithm, 'Unknown Curve' end |
#crv_name ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cose/key/ec2.rb', line 40 def crv_name case crv_key when :P256 'prime256v1' when :P384 'secp384r1' when :P521 'secp521r1' end end |
#digest ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cose/key/ec2.rb', line 51 def digest case alg_key when :ES256 OpenSSL::Digest::SHA256 when :ES384 OpenSSL::Digest::SHA384 when :ES512 OpenSSL::Digest::SHA512 end.new end |
#to_key ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cose/key/ec2.rb', line 62 def to_key point = OpenSSL::PKey::EC::Point.new( OpenSSL::PKey::EC::Group.new(crv_name), OpenSSL::BN.new(['04' + x.unpack('H*').first + y.unpack('H*').first].pack('H*'), 2) ) # Public key data_sequence = OpenSSL::ASN1::Sequence([ OpenSSL::ASN1::Sequence([ OpenSSL::ASN1::ObjectId("id-ecPublicKey"), OpenSSL::ASN1::ObjectId(crv_name) ]), OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed)) ]) if d # Private key data_sequence = OpenSSL::ASN1::Sequence([ OpenSSL::ASN1::Integer(1), OpenSSL::ASN1::OctetString(OpenSSL::BN.new(d, 2).to_s(2)), OpenSSL::ASN1::ObjectId(crv_name, 0, :EXPLICIT), OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed), 1, :EXPLICIT) ]) end OpenSSL::PKey::EC.new(data_sequence.to_der) end |
#verify(signature, signature_base_string) ⇒ Object
90 91 92 93 |
# File 'lib/cose/key/ec2.rb', line 90 def verify(signature, signature_base_string) public_key = to_key public_key.verify digest, signature, signature_base_string end |