instance method
OpenSSL::Cipher#iv=
(::String iv) -> ::String
cipher.iv = string
ext/openssl/ossl_cipher.c
View on GitHub ↗
static VALUE
ossl_cipher_set_iv(VALUE self, VALUE iv)
{
EVP_CIPHER_CTX *ctx;
int iv_len = 0;
StringValue(iv);
GetCipher(self, ctx);
if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER)
iv_len = (int)(VALUE)EVP_CIPHER_CTX_get_app_data(ctx);
if (!iv_len)
iv_len = EVP_CIPHER_CTX_iv_length(ctx);
if (RSTRING_LEN(iv) != iv_len)
ossl_raise(rb_eArgError, "iv must be %d bytes", iv_len);
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, (unsigned char *)RSTRING_PTR(iv), -1) != 1)
ossl_raise(eCipherError, NULL);
return iv;
}
Sets the cipher IV. Please note that since you should never be using ECB mode, an IV is always explicitly required and should be set prior to encryption. The IV itself can be safely transmitted in public.
This method expects the String to have the length equal to iv_len. To use a different IV length with an AEAD cipher, iv_len= must be set prior to calling this method.
NOTE: In OpenSSL API conventions, the IV value may correspond to the “nonce” instead in some cipher modes. Refer to the OpenSSL man pages for details.
See also the man page EVP_CipherInit_ex(3).