class EC::PrivateKey

Public Class Methods

convert( *args, **kwargs ) click to toggle source
# File lib/elliptic/private_key.rb, line 4
def self.convert( *args, **kwargs )
  if args.size==1 && args[0].is_a?( PrivateKey )
    args[0]   ## pass through as is (already a private key)
  else
    new( args[0], group: kwargs[:group] )
  end
end
decode_base64( str ) click to toggle source

todo/check: only use (allow) base64 for

der (binary)-encoded? why? why not?
# File lib/elliptic/private_key.rb, line 18
def self.decode_base64( str ) new( Base64.decode64(str)); end
Also aliased as: from_base64
decode_der( str ) click to toggle source
# File lib/elliptic/private_key.rb, line 14
def self.decode_der( str ) new( str ); end
Also aliased as: from_der
decode_pem( str ) click to toggle source
# File lib/elliptic/private_key.rb, line 13
def self.decode_pem( str ) new( str ); end
Also aliased as: from_pem
from_base64( str )
Alias for: decode_base64
from_der( str )
Alias for: decode_der
from_pem( str )
Alias for: decode_pem
generate( group: nil ) click to toggle source
# File lib/elliptic/private_key.rb, line 27
def self.generate( group: nil ) new( group: group ); end
new( input=nil, group: nil ) click to toggle source
# File lib/elliptic/private_key.rb, line 30
def initialize( input=nil, group: nil )
  if input.nil?     ## auto-generate new key
      ec_group = GROUP[ group || 'secp256k1' ]
      @pkey = OpenSSL::PKey::EC.new( ec_group )
      @pkey.generate_key  # note: will generate private/public key pair
  elsif input.is_a?( Integer )
      ec_group = GROUP[ group || 'secp256k1' ]
      @pkey = OpenSSL::PKey::EC.new( ec_group )
      @pkey.private_key = OpenSSL::BN.new( input )
      ## auto-calculate public key too
      @pkey.public_key = @pkey.group.generator.mul( @pkey.private_key )
  else  ## assume string with possible der/pem/etc. encoding
      ## todo/check: add hex-string auto-detect too - why? why not?
      @pkey = OpenSSL::PKey::EC.new( input )
      ## todo/check: make sure public key gets restored too with pem/der-encoding??
  end
end

Public Instance Methods

group() click to toggle source

more helpers for debugging / internals

# File lib/elliptic/private_key.rb, line 81
def group()    @pkey.group; end
private?() click to toggle source
# File lib/elliptic/private_key.rb, line 83
def private?() @pkey.private?; end
public?() click to toggle source
# File lib/elliptic/private_key.rb, line 84
def public?()  @pkey.public?;  end
public_key() click to toggle source
# File lib/elliptic/private_key.rb, line 66
def public_key
  ## cache returned public key - why? why not?
  @pub ||= PublicKey.new( @pkey.public_key )
  @pub
end
sign( message ) click to toggle source
# File lib/elliptic/private_key.rb, line 73
def sign( message )
  signature_der = @pkey.dsa_sign_asn1( message )
  Signature.decode_der( signature_der )
end
to_base64() click to toggle source
# File lib/elliptic/private_key.rb, line 62
def to_base64()  Base64.encode64( to_der ); end
to_der() click to toggle source
# File lib/elliptic/private_key.rb, line 61
def to_der()     @pkey.to_der; end
to_i() click to toggle source
# File lib/elliptic/private_key.rb, line 49
def to_i()  @pkey.private_key.to_i;           end
to_pem() click to toggle source
# File lib/elliptic/private_key.rb, line 60
def to_pem()     @pkey.to_pem; end
to_s() click to toggle source

todo/check/fix: make it always a 32 byte (64 hex chars) string

                  even with leading zeros !!! - why? why not?
todo/check - add hex alias - why? why not?
# File lib/elliptic/private_key.rb, line 54
def to_s
   ## todo/fix:  use number of bytes depending on curve (e.g. secp256k1 = 32-byte/256-bit)
   @pkey.private_key.to_i.to_s(16).rjust(64, '0'); # convert to hex and make sure it's 32 bytes (64 characters)
end
to_text() click to toggle source
# File lib/elliptic/private_key.rb, line 82
def to_text()  @pkey.to_text; end