Class: EllipticCurve::PrivateKey
- Inherits:
-
Object
- Object
- EllipticCurve::PrivateKey
- Defined in:
- lib/privatekey.rb
Instance Attribute Summary collapse
-
#curve ⇒ Object
Returns the value of attribute curve.
-
#secret ⇒ Object
Returns the value of attribute secret.
Class Method Summary collapse
- .fromDer(string) ⇒ Object
- .fromPem(string) ⇒ Object
- .fromString(string, curve = Curve::SECP256K1) ⇒ Object
Instance Method Summary collapse
-
#initialize(curve = Curve::SECP256K1, secret = nil) ⇒ PrivateKey
constructor
A new instance of PrivateKey.
- #publicKey ⇒ Object
- #toDer ⇒ Object
- #toPem ⇒ Object
- #toString ⇒ Object
Constructor Details
#initialize(curve = Curve::SECP256K1, secret = nil) ⇒ PrivateKey
Returns a new instance of PrivateKey.
8 9 10 11 |
# File 'lib/privatekey.rb', line 8 def initialize(curve=Curve::SECP256K1, secret=nil) @curve = curve @secret = secret ? secret : Utils::RandomInteger.between(1, @curve.n - 1) end |
Instance Attribute Details
#curve ⇒ Object
Returns the value of attribute curve.
6 7 8 |
# File 'lib/privatekey.rb', line 6 def curve @curve end |
#secret ⇒ Object
Returns the value of attribute secret.
6 7 8 |
# File 'lib/privatekey.rb', line 6 def secret @secret end |
Class Method Details
.fromDer(string) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/privatekey.rb', line 50 def self.fromDer(string) hexadecimal = Utils::Binary.hexFromByteString(string) privateKeyFlag, secretHex, curveData, publicKeyString = Utils::Der.parse(hexadecimal)[0] if privateKeyFlag != 1 raise Exception.new("Private keys should start with a '1' flag, but a '#{privateKeyFlag}' was found instead") end curve = Curve.getbyOid(curveData[0]) privateKey = self.fromString(secretHex, curve) if privateKey.publicKey.toString(true) != publicKeyString[0] raise Exception.new("The public key described inside the private key file doesn't match the actual public key of the pair") end return privateKey end |
.fromPem(string) ⇒ Object
45 46 47 48 |
# File 'lib/privatekey.rb', line 45 def self.fromPem(string) privateKeyPem = Utils::Pem.getContent(string, PemTemplate) return self.fromDer(Utils::Binary.byteStringFromBase64(privateKeyPem)) end |
.fromString(string, curve = Curve::SECP256K1) ⇒ Object
64 65 66 |
# File 'lib/privatekey.rb', line 64 def self.fromString(string, curve=Curve::SECP256K1) return PrivateKey.new(curve, Utils::Binary.intFromHex(string)) end |
Instance Method Details
#publicKey ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/privatekey.rb', line 13 def publicKey curve = @curve publicPoint = Math.multiply( curve.g, @secret, curve.n, curve.a, curve.p ) return PublicKey.new(publicPoint, curve) end |
#toDer ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/privatekey.rb', line 29 def toDer publicKeyString = self.publicKey.toString(true) hexadecimal = Utils::Der.encodeConstructed( Utils::Der.encodePrimitive(Utils::Der::DerFieldType.integer, 1), Utils::Der.encodePrimitive(Utils::Der::DerFieldType.octetString, Utils::Binary.hexFromInt(@secret)), Utils::Der.encodePrimitive(Utils::Der::DerFieldType.oidContainer, Utils::Der.encodePrimitive(Utils::Der::DerFieldType.object, @curve.oid)), Utils::Der.encodePrimitive(Utils::Der::DerFieldType.publicKeyPointContainer, Utils::Der.encodePrimitive(Utils::Der::DerFieldType.bitString, publicKeyString)) ) return Utils::Binary.byteStringFromHex(hexadecimal) end |
#toPem ⇒ Object
40 41 42 43 |
# File 'lib/privatekey.rb', line 40 def toPem der = self.toDer() return Utils::Pem.create(Utils::Binary.base64FromByteString(der), PemTemplate) end |
#toString ⇒ Object
25 26 27 |
# File 'lib/privatekey.rb', line 25 def toString return Utils::Binary.hexFromInt(@secret) end |