class ExoBasic::ECDSAKeys

Constants

CURVE

Public Class Methods

digest(data) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 91
def self.digest(data)
  OpenSSL::Digest::SHA256.digest(data)
end
from_pem(key_pem) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 59
def self.from_pem(key_pem)
  OpenSSL::PKey::EC.new(key_pem)
end
gen_key(curve=nil) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 85
def self.gen_key(curve=nil)
  c = curve.nil? ? ECDSAKeys::CURVE : curve

  ECDSAKeys.get_key_details(OpenSSL::PKey::EC.new(c).generate_key!)
end
get_key_details(private_key) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 37
def self.get_key_details(private_key)
  private_hex = private_key.to_der.unpack('H*').first
  private_pem = private_key.to_pem

  key2 = OpenSSL::PKey::EC.new(private_key.public_key.group)
  key2.public_key = private_key.public_key

  public_hex = private_key.public_key.to_bn.to_s(16).downcase
  public_pem = key2.to_pem

  {
    :private_hex => private_hex,
    :private_pem => private_pem,
    :public_hex => public_hex,
    :public_pem => public_pem
  }
end
private_from_hex(private_hex) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 63
def self.private_from_hex(private_hex)
  der = [private_hex].pack('H*')
  b64 = Base64.encode64(der)
  pem = ECDSAKeys.s_to_private_pem(b64)

  ECDSAKeys.from_pem(pem)
end
private_pem_to_s(pem) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 17
def self.private_pem_to_s(pem)
  pem.gsub("-----BEGIN EC PRIVATE KEY-----\n", '')
     .gsub("\n-----END EC PRIVATE KEY-----\n", '')
     .gsub("\n", '')
end
public_from_hex(public_hex, curve=nil) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 71
def self.public_from_hex(public_hex, curve=nil)
  # rebuild key
  c     = curve.nil? ? ECDSAKeys::CURVE : curve
  group = OpenSSL::PKey::EC::Group.new(c)
  key   = OpenSSL::PKey::EC.new(group)

  # create point from hex key
  public_key_bn  = OpenSSL::BN.new(public_hex, 16)
  public_key     = OpenSSL::PKey::EC::Point.new(group, public_key_bn)
  key.public_key = public_key

  key
end
public_pem_to_s(pem) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 27
def self.public_pem_to_s(pem)
  pem.gsub("-----BEGIN PUBLIC KEY-----\n", '')
     .gsub("\n-----END PUBLIC KEY-----\n", '')
     .gsub("\n", '')
end
s_to_private_pem(b64) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 23
def self.s_to_private_pem(b64)
  "-----BEGIN EC PRIVATE KEY-----\n#{b64}-----END EC PRIVATE KEY-----\n"
end
s_to_public_pem(b64) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 33
def self.s_to_public_pem(b64)
  "-----BEGIN PUBLIC KEY-----\n#{b64}-----END PUBLIC KEY-----\n"
end
sign_message(private_key_pem, data) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 95
def self.sign_message(private_key_pem, data)
  key = ECDSAKeys.from_pem(private_key_pem)

  digested         = ECDSAKeys.digest(data)
  digested_base64  = Base64.encode64(digested).gsub("\n", '')
  signature        = key.dsa_sign_asn1(digested)
  signature_base64 = Base64.encode64(signature).gsub("\n", '')

  signature_base64
end
to_pem(key) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 55
def self.to_pem(key)
  key.to_pem
end
verify_digested_message(public_key_hex, signature_base64, encoded_digested_data, curve=nil) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 114
def self.verify_digested_message(public_key_hex, signature_base64, encoded_digested_data,
                                 curve=nil)

  key           = ECDSAKeys.public_from_hex(public_key_hex, curve)
  signature     = Base64.decode64(signature_base64)
  digested_data = Base64.decode64(encoded_digested_data)

  key.dsa_verify_asn1(digested_data, signature)
end
verify_message(public_key_pem, signature_base64, data) click to toggle source
# File lib/exobasic/encrypt/ecdsa_keys.rb, line 106
def self.verify_message(public_key_pem, signature_base64, data)
  key           = OpenSSL::PKey::EC.new(public_key_pem)
  signature     = Base64.decode64(signature_base64)
  digested_data = ECDSAKeys.digest(data)

  key.dsa_verify_asn1(digested_data, signature)
end