update method
Updates the BLAKE2b hash with the given data, optionally specifying the data length.
This method updates the hash state with the provided data. If a specific data length is provided, only that amount of data will be processed. If the hash was previously finished, an exception will be thrown.
Parameters:
data
: The data to be hashed.length
(optional): The length of data to process. If not specified, the entire data will be processed.
Returns:
- The modified BLAKE2b instance with updated hash state.
Implementation
@override
BLAKE2b update(List<int> data, {int? length}) {
if (_finished) {
throw const ArgumentException(
"blake2b: can't update because hash was finished.");
}
int left = _blockSize - _bufferLength;
int dataPos = 0;
int dataLength = length ?? data.length;
if (dataLength == 0) {
return this;
}
// Finish buffer.
if (dataLength > left) {
for (int i = 0; i < left; i++) {
_buffer[_bufferLength + i] = data[dataPos + i] & mask8;
}
_processBlock(_blockSize);
dataPos += left;
dataLength -= left;
_bufferLength = 0;
}
// Process data blocks.
while (dataLength > _blockSize) {
for (int i = 0; i < _blockSize; i++) {
_buffer[i] = data[dataPos + i] & mask8;
}
_processBlock(_blockSize);
dataPos += _blockSize;
dataLength -= _blockSize;
_bufferLength = 0;
}
// Copy leftovers to buffer.
for (int i = 0; i < dataLength; i++) {
_buffer[_bufferLength + i] = data[dataPos + i] & mask8;
}
_bufferLength += dataLength;
return this;
}