class
OpenSSL::Cipher
- OpenSSL::Cipher
- Reference
- Object
Overview
A class which can be used to encrypt and decrypt data using a specified cipher.
NOTE The ciphers available to an application are determined by the linked version of the system SSL library. A comprehensive list of ciphers can be found in the OpenSSL Cipher documentation.
require "random/secure"
key = Random::Secure.random_bytes(64) # You can also use OpenSSL::Cipher#random_key to do this same thing
iv = Random::Secure.random_bytes(32) # You can also use OpenSSL::Cipher#random_iv to do this same thing
def encrypt(data)
cipher = OpenSSL::Cipher.new("aes-256-cbc")
cipher.encrypt
cipher.key = key
cipher.iv = iv
io = IO::Memory.new
io.write(cipher.update(data))
io.write(cipher.final)
io.rewind
io.to_slice
end
def decrypt(data)
cipher = OpenSSL::Cipher.new("aes-256-cbc")
cipher.decrypt
cipher.key = key
cipher.iv = iv
io = IO::Memory.new
io.write(cipher.update(data))
io.write(cipher.final)
io.rewind
io.gets_to_end
end
Defined in:
openssl_ext/cipher.crInstance Method Summary
-
#auth_data=(data : Bytes | String) : Bytes | String
Authenticates additional data (AAD) for an AEAD cipher such as AES-GCM.
-
#gcm_tag : Bytes
Returns the authentication tag after encryption has finished with
#final. -
#gcm_tag=(tag : Bytes) : Bytes
Supplies the expected tag before calling
#finalduring AES-GCM decryption.
Instance Method Detail
Authenticates additional data (AAD) for an AEAD cipher such as AES-GCM. Set the key and IV first, then supply AAD before processing the payload. Encryption and decryption must use the same AAD. For AES-GCM, repeated calls append bytes.
Supplies the expected tag before calling #final during AES-GCM decryption.