Class: EC::PrivateKey

Inherits:
Object
  • Object
show all
Defined in:
lib/elliptic/private_key.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Class Method Details

.convert(*args, **kwargs) ⇒ Object



4
5
6
7
8
9
10
# 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) ⇒ Object Also known as: from_base64

todo/check: only use (allow) base64 for

der (binary)-encoded? why? why not?


18
# File 'lib/elliptic/private_key.rb', line 18

def self.decode_base64( str ) new( Base64.decode64(str)); end

.decode_der(str) ⇒ Object Also known as: from_der



14
# File 'lib/elliptic/private_key.rb', line 14

def self.decode_der( str ) new( str ); end

.decode_pem(str) ⇒ Object Also known as: from_pem



13
# File 'lib/elliptic/private_key.rb', line 13

def self.decode_pem( str ) new( str ); end

.generate(group: nil) ⇒ Object



27
# File 'lib/elliptic/private_key.rb', line 27

def self.generate( group: nil ) new( group: group ); end

Instance Method Details

#groupObject

more helpers for debugging / internals



81
# File 'lib/elliptic/private_key.rb', line 81

def group()    @pkey.group; end

#private?Boolean

todo/check: keep - needed? - why? why not?

Returns:

  • (Boolean)


83
# File 'lib/elliptic/private_key.rb', line 83

def private?() @pkey.private?; end

#public?Boolean

todo/check: keep - needed? - why? why not?

Returns:

  • (Boolean)


84
# File 'lib/elliptic/private_key.rb', line 84

def public?()  @pkey.public?;  end

#public_keyObject



66
67
68
69
70
# 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) ⇒ Object



73
74
75
76
# 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_base64Object



62
# File 'lib/elliptic/private_key.rb', line 62

def to_base64()  Base64.encode64( to_der ); end

#to_derObject



61
# File 'lib/elliptic/private_key.rb', line 61

def to_der()     @pkey.to_der; end

#to_iObject



49
# File 'lib/elliptic/private_key.rb', line 49

def to_i()  @pkey.private_key.to_i;           end

#to_pemObject



60
# File 'lib/elliptic/private_key.rb', line 60

def to_pem()     @pkey.to_pem; end

#to_sObject

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?


54
55
56
57
# 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_textObject



82
# File 'lib/elliptic/private_key.rb', line 82

def to_text()  @pkey.to_text; end