Class: EC::PrivateKey
- Inherits:
-
Object
- Object
- EC::PrivateKey
- Defined in:
- lib/elliptic.rb
Class Method Summary collapse
Instance Method Summary collapse
- #group ⇒ Object
-
#initialize(input = nil, group: nil) ⇒ PrivateKey
constructor
A new instance of PrivateKey.
-
#private? ⇒ Boolean
todo/check: keep - needed? - why? why not?.
- #public_key ⇒ Object
- #sign(message) ⇒ Object
- #to_i ⇒ Object
-
#to_s ⇒ Object
todo/check/fix: make it always a 32 byte (64 hex chars) string even with leading zeros !!! - why? why not?.
-
#to_text ⇒ Object
helpers for debugging.
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
#group ⇒ Object
189 |
# File 'lib/elliptic.rb', line 189 def group() @pkey.group; end |
#private? ⇒ Boolean
todo/check: keep - needed? - why? why not?
206 |
# File 'lib/elliptic.rb', line 206 def private?() @pkey.private?; end |
#public_key ⇒ Object
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( ) signature_der = @pkey.dsa_sign_asn1( ) Signature.decode_der( signature_der ) end |
#to_i ⇒ Object
184 |
# File 'lib/elliptic.rb', line 184 def to_i() @pkey.private_key.to_i; end |
#to_s ⇒ Object
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_text ⇒ Object
helpers for debugging
205 |
# File 'lib/elliptic.rb', line 205 def to_text() @pkey.to_text; end |