Class: EC::PrivateKey
- Inherits:
-
Object
- Object
- EC::PrivateKey
- Defined in:
- lib/elliptic/private_key.rb
Class Method Summary collapse
- .convert(*args, **kwargs) ⇒ Object
-
.decode_base64(str) ⇒ Object
(also: from_base64)
todo/check: only use (allow) base64 for der (binary)-encoded? why? why not?.
- .decode_der(str) ⇒ Object (also: from_der)
- .decode_pem(str) ⇒ Object (also: from_pem)
- .generate(group: nil) ⇒ Object
Instance Method Summary collapse
-
#group ⇒ Object
more helpers for debugging / internals.
-
#initialize(input = nil, group: nil) ⇒ PrivateKey
constructor
A new instance of PrivateKey.
-
#private? ⇒ Boolean
todo/check: keep - needed? - why? why not?.
-
#public? ⇒ Boolean
todo/check: keep - needed? - why? why not?.
- #public_key ⇒ Object
- #sign(message) ⇒ Object
- #to_base64 ⇒ Object
- #to_der ⇒ Object
- #to_i ⇒ Object
- #to_pem ⇒ Object
-
#to_s ⇒ Object
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?.
- #to_text ⇒ Object
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
#group ⇒ Object
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?
83 |
# File 'lib/elliptic/private_key.rb', line 83 def private?() @pkey.private?; end |
#public? ⇒ Boolean
todo/check: keep - needed? - why? why not?
84 |
# File 'lib/elliptic/private_key.rb', line 84 def public?() @pkey.public?; end |
#public_key ⇒ Object
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( ) signature_der = @pkey.dsa_sign_asn1( ) Signature.decode_der( signature_der ) end |
#to_base64 ⇒ Object
62 |
# File 'lib/elliptic/private_key.rb', line 62 def to_base64() Base64.encode64( to_der ); end |
#to_der ⇒ Object
61 |
# File 'lib/elliptic/private_key.rb', line 61 def to_der() @pkey.to_der; end |
#to_i ⇒ Object
49 |
# File 'lib/elliptic/private_key.rb', line 49 def to_i() @pkey.private_key.to_i; end |
#to_pem ⇒ Object
60 |
# File 'lib/elliptic/private_key.rb', line 60 def to_pem() @pkey.to_pem; end |
#to_s ⇒ Object
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_text ⇒ Object
82 |
# File 'lib/elliptic/private_key.rb', line 82 def to_text() @pkey.to_text; end |