BLAKE2b constructor

BLAKE2b({
  1. int digestLength = 64,
  2. Blake2bConfig? config,
})

Creates a BLAKE2b hash instance with the specified digest length and optional configuration.

Parameters:

  • digestLength: The length of the hash digest in bytes (default is 64 bytes).
  • config: Optional configuration for BLAKE2b (e.g., key, salt, personalization).

Throws:

Implementation

BLAKE2b({int digestLength = 64, Blake2bConfig? config}) {
  if (digestLength < 1 || digestLength > _digestLength) {
    throw ArgumentException.invalidOperationArguments(
      "BLAKE2b",
      name: "digestLength",
      reason: "Incorrect diest length.",
    );
  }
  getDigestLength = digestLength;
  config = _validateConfig(config);
  int klength = 0;
  if (config != null && config.key != null) {
    klength = config.key!.length;
  }

  int fanout = 1;
  int maxDepth = 1;
  final tree = config?.tree;
  if (tree != null) {
    fanout = tree.fanout;
    maxDepth = tree.maxDepth;
  }

  _state[0] ^=
      (getDigestLength | (klength << 8) | (fanout << 16) | (maxDepth << 24));

  if (tree != null) {
    _state[1] ^= tree.leafSize;
    _state[2] ^= tree.nodeOffsetLowBits;
    _state[3] ^= tree.nodeOffsetHighBits;
    _state[4] ^= (tree.nodeDepth | (tree.innerDigestLength << 8));
    _lastNode = tree.lastNode;
  }
  final salt = config?.salt;
  if (salt != null) {
    _state[8] ^= BinaryOps.readUint32LE(salt, 0);
    _state[9] ^= BinaryOps.readUint32LE(salt, 4);
    _state[10] ^= BinaryOps.readUint32LE(salt, 8);
    _state[11] ^= BinaryOps.readUint32LE(salt, 12);
  }
  final personalization = config?.personalization;
  if (personalization != null) {
    _state[12] ^= BinaryOps.readUint32LE(personalization, 0);
    _state[13] ^= BinaryOps.readUint32LE(personalization, 4);
    _state[14] ^= BinaryOps.readUint32LE(personalization, 8);
    _state[15] ^= BinaryOps.readUint32LE(personalization, 12);
  }
  _initialState = List<int>.from(_state, growable: false);
  final key = config?.key;
  if (key != null && _keyLength > 0) {
    _paddedKey = List<int>.filled(_blockSize, 0);
    _paddedKey!.setAll(0, key.asBytes);
    _buffer.setAll(0, _paddedKey!);
    _bufferLength = _blockSize;
  }
}