Class: Ciri::Key
- Inherits:
-
Object
- Object
- Ciri::Key
- Defined in:
- lib/ciri/key.rb
Overview
Class Method Summary collapse
Instance Method Summary collapse
- #ec_key ⇒ Object
- #ecdsa_signature(data) ⇒ Object
- #ecies_decrypt(data, shared_mac_data = '') ⇒ Object
- #ecies_encrypt(message, shared_mac_data = '') ⇒ Object
-
#initialize(ec_key: nil, raw_public_key: nil, raw_private_key: nil) ⇒ Key
constructor
initialized from ec_key or raw keys ec_key is a OpenSSL::PKey::EC object, raw keys is bytes presented keys.
-
#raw_public_key ⇒ Object
raw public key.
-
#regenerate_ec_key ⇒ Object
regenerate ec_key from raw_public_key and raw_private_key can used to validate the public_key.
- #to_address ⇒ Object
Constructor Details
#initialize(ec_key: nil, raw_public_key: nil, raw_private_key: nil) ⇒ Key
initialized from ec_key or raw keys ec_key is a OpenSSL::PKey::EC object, raw keys is bytes presented keys
62 63 64 65 66 |
# File 'lib/ciri/key.rb', line 62 def initialize(ec_key: nil, raw_public_key: nil, raw_private_key: nil) @ec_key = ec_key @raw_public_key = raw_public_key @raw_private_key = raw_private_key end |
Class Method Details
.ecdsa_recover(msg, signature) ⇒ Object
44 45 46 47 |
# File 'lib/ciri/key.rb', line 44 def ecdsa_recover(msg, signature) raw_public_key = Crypto.ecdsa_recover(msg, signature, return_raw_key: true) Ciri::Key.new(raw_public_key: raw_public_key) end |
.random ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/ciri/key.rb', line 49 def random ec_key = OpenSSL::PKey::EC.new('secp256k1') ec_key.generate_key while (raw_priv_key = ec_key.private_key.to_s(2).size) != 32 warn "generated privkey is not 32 bytes, bytes: #{raw_priv_key.size} privkey: #{Utils.to_hex raw_priv_key} -> regenerate it..." ec_key.generate_key end Ciri::Key.new(ec_key: ec_key) end |
Instance Method Details
#ec_key ⇒ Object
96 97 98 |
# File 'lib/ciri/key.rb', line 96 def ec_key @ec_key ||= Ciri::Utils.create_ec_pk(raw_privkey: @raw_private_key, raw_pubkey: @raw_public_key) end |
#ecdsa_signature(data) ⇒ Object
73 74 75 |
# File 'lib/ciri/key.rb', line 73 def ecdsa_signature(data) Crypto.ecdsa_signature(secp256k1_key, data) end |
#ecies_decrypt(data, shared_mac_data = '') ⇒ Object
81 82 83 |
# File 'lib/ciri/key.rb', line 81 def ecies_decrypt(data, shared_mac_data = '') Crypto.ecies_decrypt(data, ec_key, shared_mac_data) end |
#ecies_encrypt(message, shared_mac_data = '') ⇒ Object
77 78 79 |
# File 'lib/ciri/key.rb', line 77 def ecies_encrypt(, shared_mac_data = '') Crypto.ecies_encrypt(, ec_key, shared_mac_data) end |
#raw_public_key ⇒ Object
raw public key
69 70 71 |
# File 'lib/ciri/key.rb', line 69 def raw_public_key @raw_public_key ||= ec_key.public_key.to_bn.to_s(2) end |
#regenerate_ec_key ⇒ Object
regenerate ec_key from raw_public_key and raw_private_key can used to validate the public_key
91 92 93 94 |
# File 'lib/ciri/key.rb', line 91 def regenerate_ec_key @ec_key = nil ec_key end |