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:

  • An exception if the provided digestLength is out of the valid range.

Implementation

BLAKE2b({int digestLength = 64, Blake2bConfig? config}) {
  if (digestLength < 1 || digestLength > _digestLength) {
    throw const ArgumentException("blake2b: wrong digest length");
  }
  getDigestLength = digestLength;

  if (config != null) {
    _validateConfig(config);
  }

  int klength = 0;
  if (config != null && config.key != null) {
    klength = config.key!.length;
  }

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

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

  if (config != null && config.tree != null) {
    _state[1] ^= config.tree!.leafSize;

    _state[2] ^= config.tree!.nodeOffsetLowBits;
    _state[3] ^= config.tree!.nodeOffsetHighBits;
    _state[4] ^=
        (config.tree!.nodeDepth | (config.tree!.innerDigestLength << 8));

    _lastNode = config.tree!.lastNode;
  }

  if (config != null && config.salt != null) {
    _state[8] ^= readUint32LE(config.salt!, 0);
    _state[9] ^= readUint32LE(config.salt!, 4);
    _state[10] ^= readUint32LE(config.salt!, 8);
    _state[11] ^= readUint32LE(config.salt!, 12);
  }

  if (config != null && config.personalization != null) {
    _state[12] ^= readUint32LE(config.personalization!, 0);
    _state[13] ^= readUint32LE(config.personalization!, 4);
    _state[14] ^= readUint32LE(config.personalization!, 8);
    _state[15] ^= readUint32LE(config.personalization!, 12);
  }

  _initialState = List<int>.from(_state, growable: false);

  if (config != null && config.key != null && _keyLength > 0) {
    _paddedKey = List<int>.filled(_blockSize, 0);
    _paddedKey!.setAll(0, BytesUtils.toBytes(config.key!));

    _buffer.setAll(0, _paddedKey!);
    _bufferLength = _blockSize;
  }
}