Class: EllipticCurve::PrivateKey

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#curveObject

Returns the value of attribute curve.



6
7
8
# File 'lib/privatekey.rb', line 6

def curve
  @curve
end

#secretObject

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

#publicKeyObject



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

#toDerObject



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

#toPemObject



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

#toStringObject



25
26
27
# File 'lib/privatekey.rb', line 25

def toString
    return Utils::Binary.hexFromInt(@secret)
end