Class: EllipticCurve::PublicKey
- Inherits:
-
Object
- Object
- EllipticCurve::PublicKey
- Defined in:
- lib/publickey.rb
Instance Attribute Summary collapse
-
#curve ⇒ Object
Returns the value of attribute curve.
-
#point ⇒ Object
Returns the value of attribute point.
Class Method Summary collapse
- .fromCompressed(string, curve = Curve::SECP256K1) ⇒ Object
- .fromDer(string) ⇒ Object
- .fromPem(string) ⇒ Object
- .fromString(string, curve = Curve::SECP256K1, validatePoint = true) ⇒ Object
Instance Method Summary collapse
-
#initialize(point, curve) ⇒ PublicKey
constructor
A new instance of PublicKey.
- #toCompressed ⇒ Object
- #toDer ⇒ Object
- #toPem ⇒ Object
- #toString(encoded = false) ⇒ Object
Constructor Details
#initialize(point, curve) ⇒ PublicKey
Returns a new instance of PublicKey.
11 12 13 14 |
# File 'lib/publickey.rb', line 11 def initialize(point, curve) @point = point @curve = curve end |
Instance Attribute Details
#curve ⇒ Object
Returns the value of attribute curve.
9 10 11 |
# File 'lib/publickey.rb', line 9 def curve @curve end |
#point ⇒ Object
Returns the value of attribute point.
9 10 11 |
# File 'lib/publickey.rb', line 9 def point @point end |
Class Method Details
.fromCompressed(string, curve = Curve::SECP256K1) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/publickey.rb', line 98 def self.fromCompressed(string, curve=Curve::SECP256K1) parityTag, xHex = string[0..1], string[2..-1] if not [EvenTag, OddTag].include? parityTag raise Exception.new("Compressed string should start with 02 or 03") end x = Utils::Binary.intFromHex(xHex) y = curve.y(x=x, isEven=parityTag == EvenTag) return PublicKey.new(point=Point.new(x, y), curve=curve) end |
.fromDer(string) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/publickey.rb', line 56 def self.fromDer(string) hexadecimal = Utils::Binary.hexFromByteString(string) curveData, pointString = Utils::Der.parse(hexadecimal)[0] publicKeyOid, curveOid = curveData if publicKeyOid != EcdsaPublicKeyOid raise Exception.new("The Public Key Object Identifier (OID) should be #{EcdsaPublicKeyOid}, but #{publicKeyOid} was found instead") end curve = Curve.getbyOid(curveOid) return self.fromString(pointString, curve) end |
.fromPem(string) ⇒ Object
51 52 53 54 |
# File 'lib/publickey.rb', line 51 def self.fromPem(string) publicKeyPem = Utils::Pem.getContent(string, PemTemplate) return self.fromDer(Utils::Binary.byteStringFromBase64(publicKeyPem)) end |
.fromString(string, curve = Curve::SECP256K1, validatePoint = true) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/publickey.rb', line 67 def self.fromString(string, curve=Curve::SECP256K1, validatePoint=true) baseLength = 2 * curve.length if string.length > 2 * baseLength and string[0..3] == "0004" string = string[4..-1] end xs = string[0..baseLength - 1] ys = string[baseLength..-1] p = Point.new( Utils::Binary.intFromHex(xs), Utils::Binary.intFromHex(ys) ) publicKey = PublicKey.new(p, curve) if not validatePoint return publicKey end if p.isAtInfinity() raise Exception.new("Public key point at infinity") end if not curve.contains(p) raise Exception.new("Point (#{p.x}, #{p.y}) is not valid for curve #{curve.name}") end if not Math.multiply(p, curve.n, curve.n, curve.a, curve.p).isAtInfinity() raise Exception.new("Point (#{p.x}, #{p.y}) * #{curve.name}.n is not at infinity") end return publicKey end |
Instance Method Details
#toCompressed ⇒ Object
28 29 30 31 32 33 |
# File 'lib/publickey.rb', line 28 def toCompressed baseLength = 2 * @curve.length parityTag = @point.y % 2 == 0 ? EvenTag : OddTag xHex = Utils::Binary.hexFromInt(@point.x).rjust(baseLength, "0") return parityTag + xHex end |
#toDer ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/publickey.rb', line 35 def toDer @hexadecimal = Utils::Der.encodeConstructed( Utils::Der.encodeConstructed( Utils::Der.encodePrimitive(Utils::Der::DerFieldType.object, EcdsaPublicKeyOid), Utils::Der.encodePrimitive(Utils::Der::DerFieldType.object, @curve.oid) ), Utils::Der.encodePrimitive(Utils::Der::DerFieldType.bitString, self.toString(true)) ) return Utils::Binary.byteStringFromHex(@hexadecimal) end |
#toPem ⇒ Object
46 47 48 49 |
# File 'lib/publickey.rb', line 46 def toPem der = self.toDer() return Utils::Pem.create(Utils::Binary.base64FromByteString(der), PemTemplate) end |
#toString(encoded = false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/publickey.rb', line 16 def toString encoded=false baseLength = 2 * @curve.length xHex = Utils::Binary.hexFromInt(@point.x).rjust(baseLength, "0") yHex = Utils::Binary.hexFromInt(@point.y).rjust(baseLength, "0") string = xHex + yHex if encoded return "0004" + string end return string end |