Class: Cryptos::PublicKey
- Inherits:
-
Object
- Object
- Cryptos::PublicKey
- Includes:
- Utils::Bytes, Utils::Hexas
- Defined in:
- lib/cryptos/public_key.rb
Instance Attribute Summary collapse
-
#private_key ⇒ Object
readonly
Returns the value of attribute private_key.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #check! ⇒ Object
- #coordinates ⇒ Object
-
#initialize(private_key) ⇒ PublicKey
constructor
A new instance of PublicKey.
- #to_s ⇒ Object
-
#to_sec(compressed = true) ⇒ Object
Serialize public key as SEC (Standards for Efficient Cryptography) format.
Methods included from Utils::Bytes
#bignum_to_bytes, #bytes_to_bignum
Methods included from Utils::Hexas
#bignum_to_hex, #bin_to_hex, #byte_to_hex, #bytes_to_hex, #hex_size, #hex_to_little, #int_to_hex, #long_to_hex
Constructor Details
#initialize(private_key) ⇒ PublicKey
Returns a new instance of PublicKey.
7 8 9 10 |
# File 'lib/cryptos/public_key.rb', line 7 def initialize(private_key) @private_key = private_key @x, @y = *ec_multiply(private_key.value, EC_Gx, EC_Gy, EC_p) end |
Instance Attribute Details
#private_key ⇒ Object (readonly)
Returns the value of attribute private_key.
5 6 7 |
# File 'lib/cryptos/public_key.rb', line 5 def private_key @private_key end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
5 6 7 |
# File 'lib/cryptos/public_key.rb', line 5 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
5 6 7 |
# File 'lib/cryptos/public_key.rb', line 5 def y @y end |
Instance Method Details
#check! ⇒ Object
12 13 14 |
# File 'lib/cryptos/public_key.rb', line 12 def check! (x**3 + 7 - y**2) % EC_p == 0 || raise('public key point is not on the curve') end |
#coordinates ⇒ Object
27 28 29 |
# File 'lib/cryptos/public_key.rb', line 27 def coordinates [x, y] end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/cryptos/public_key.rb', line 31 def to_s coordinates.to_s end |
#to_sec(compressed = true) ⇒ Object
Serialize public key as SEC (Standards for Efficient Cryptography) format
19 20 21 22 23 24 25 |
# File 'lib/cryptos/public_key.rb', line 19 def to_sec(compressed = true) if compressed "#{y.even? ? '02' : '03'}#{x_to_sec}" else "04#{x_to_sec}#{y_to_sec}" end end |