class COSE::Key::EC2

Constants

ALGS
CRV
CRVS
D
X
Y

Attributes

crv[RW]
d[RW]
x[RW]
y[RW]

Public Class Methods

new(attrs = {}) click to toggle source
Calls superclass method COSE::Key::new
# File lib/cose/key/ec2.rb, line 22
def initialize(attrs = {})
  super
  self.crv = attrs[CRV]
  self.x = attrs[X]
  self.y = attrs[Y]
  self.d = attrs[D]
end

Public Instance Methods

alg_key() click to toggle source
# File lib/cose/key/ec2.rb, line 30
def alg_key
  ALGS.invert[alg] or
  raise UknownAlgorithm, 'Unknown Algorithm'
end
crv_key() click to toggle source
# File lib/cose/key/ec2.rb, line 35
def crv_key
  CRVS.invert[crv] or
  raise UknownAlgorithm, 'Unknown Curve'
end
crv_name() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/cose/key/ec2.rb, line 62
def to_key
  key = OpenSSL::PKey::EC.new crv_name
  key.private_key = OpenSSL::BN.new(d, 2) if d
  key.public_key = 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)
  )
  key
end
verify(signature, signature_base_string) click to toggle source
# File lib/cose/key/ec2.rb, line 76
def verify(signature, signature_base_string)
  public_key = to_key
  public_key.verify digest, signature, signature_base_string
end