Method: EC::PrivateKey#initialize

Defined in:
lib/elliptic/private_key.rb

#initialize(input = nil, group: nil) ⇒ PrivateKey

Returns a new instance of PrivateKey.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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