Method: OpenSSL::Cipher#iv=

Defined in:
ossl_cipher.c

#iv=(string) ⇒ String

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, but it should be unpredictable to prevent certain kinds of attacks. You may use Cipher#random_iv to create a secure random IV.

Only call this method after calling Cipher#encrypt or Cipher#decrypt.

If not explicitly set, the OpenSSL default of an all-zeroes (“\0”) IV is used.



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ossl_cipher.c', line 499

static VALUE
ossl_cipher_set_iv(VALUE self, VALUE iv)
{
    EVP_CIPHER_CTX *ctx;

    StringValue(iv);
    GetCipher(self, ctx);

    if (RSTRING_LEN(iv) < EVP_CIPHER_CTX_iv_length(ctx))
        ossl_raise(eCipherError, "iv length too short");

    if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, (unsigned char *)RSTRING_PTR(iv), -1) != 1)
	ossl_raise(eCipherError, NULL);

    return iv;
}