Method: ECIES::Crypt#encrypt
- Defined in:
- lib/ecies/crypt.rb
permalink #encrypt(key, message) ⇒ String
Encrypts a message to a public key using ECIES.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ecies/crypt.rb', line 59 def encrypt(key, ) key.public_key? or raise "Must have public key to encrypt" @cipher.reset ephemeral_key = OpenSSL::PKey::EC.generate(key.group) ephemeral_public_key_octet = ephemeral_key.public_key.to_octet_string(:compressed) shared_secret = ephemeral_key.dh_compute_key(key.public_key) key_pair = kdf(shared_secret, @cipher.key_len + @mac_length, ephemeral_public_key_octet) cipher_key = key_pair.byteslice(0, @cipher.key_len) hmac_key = key_pair.byteslice(-@mac_length, @mac_length) @cipher.encrypt @cipher.iv = IV @cipher.key = cipher_key ciphertext = @cipher.update() + @cipher.final mac = OpenSSL::HMAC.digest(@mac_digest, hmac_key, ciphertext + @mac_shared_info).byteslice(0, @mac_length) ephemeral_public_key_octet + ciphertext + mac end |