Class: EC::PrivateKey

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PrivateKey.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/elliptic.rb', line 166

def initialize( input=nil, group: nil )
  ec_group = GROUP[ group || 'secp256k1' ]
  @pkey = OpenSSL::PKey::EC.new( ec_group )

  if input.nil?     ## auto-generate new key
    @pkey.generate_key
  else
    num = if input.is_a?( String )  ## assume hex string
            input.to_i( 16 )
          else                      ## assume integer
            input
          end
    @pkey.private_key = OpenSSL::BN.new( num )
    ## auto-calculate public key too
    @pkey.public_key = @pkey.group.generator.mul( @pkey.private_key )
  end
end

Class Method Details

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



155
156
157
158
159
160
161
# File 'lib/elliptic.rb', line 155

def self.convert( *args, **kwargs )
  if args.size==1 && args[0].is_a?( PrivateKey )
    args[0]   ## pass through as is (alread a private key)
  else
    new( args[0], group: kwargs[:group] )
  end
end

.generate(group: nil) ⇒ Object



164
# File 'lib/elliptic.rb', line 164

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

Instance Method Details

#groupObject



189
# File 'lib/elliptic.rb', line 189

def group() @pkey.group; end

#private?Boolean

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

Returns:

  • (Boolean)


206
# File 'lib/elliptic.rb', line 206

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

#public_keyObject



192
193
194
195
196
# File 'lib/elliptic.rb', line 192

def public_key
  ## cache returned public key - why? why not?
  @pub ||= PublicKey.new( @pkey.public_key )
  @pub
end

#sign(message) ⇒ Object



199
200
201
202
# File 'lib/elliptic.rb', line 199

def sign( message )
  signature_der = @pkey.dsa_sign_asn1( message )
  Signature.decode_der( signature_der )
end

#to_iObject



184
# File 'lib/elliptic.rb', line 184

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

#to_sObject

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

even with leading zeros !!! - why? why not?


187
# File 'lib/elliptic.rb', line 187

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

#to_textObject

helpers for debugging



205
# File 'lib/elliptic.rb', line 205

def to_text()  @pkey.to_text; end