setCipher method
Sets the block cipher and initialization vector (IV) for the Counter (CTR) mode.
This method allows you to specify the block cipher and IV to be used for the CTR mode. It performs necessary validation and setup for encryption. If the provided IV is not null, it copies the IV to the internal counter, overwriting its current value.
Parameters:
cipher
: The block cipher to be used for encryption.iv
: The initialization vector (IV) for the CTR mode.
Throws:
ArgumentException
if the IV length is not equal to the block size of the cipher.
Returns:
- The updated CTR instance after setting the cipher and IV.
Note: It's important to use a unique IV for each message to ensure security and prevent potential vulnerabilities in the CTR mode.
Implementation
CTR setCipher(BlockCipher cipher, List<int>? iv) {
_cipher = null;
if (iv != null && iv.length != _counter.length) {
throw const ArgumentException(
"CTR: iv length must be equal to cipher block size");
}
_cipher = cipher;
if (iv != null) {
_counter.setAll(0, iv);
}
_bufpos = _buffer.length;
return this;
}