BLAKE2s constructor

BLAKE2s({
  1. int digestLength = 32,
  2. Blake2sConfig? config,
})

Creates a BLAKE2s 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 BLAKE2s (e.g., key, salt, personalization).

Throws:

Implementation

BLAKE2s({int digestLength = 32, Blake2sConfig? config}) {
  if (digestLength < 1 || digestLength > _digestLength) {
    throw ArgumentException.invalidOperationArguments(
      "BLAKE2s",
      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) {
    final nofHi = (tree.nodeOffset ~/ 0x100000000) & BinaryOps.mask32;
    final nofLo = tree.nodeOffset & BinaryOps.mask32;
    _state[1] ^= tree.leafSize;
    _state[2] ^= nofLo;
    _state[3] ^=
        nofHi | (tree.nodeDepth << 16) | (tree.innerDigestLength << 24);
    _lastNode = tree.lastNode;
  }
  final salt = config?.salt;
  if (salt != null) {
    _state[4] ^= BinaryOps.readUint32LE(salt, 0);
    _state[5] ^= BinaryOps.readUint32LE(salt, 4);
  }
  final personalization = config?.personalization;
  if (personalization != null) {
    _state[6] ^= BinaryOps.readUint32LE(personalization, 0);
    _state[7] ^= BinaryOps.readUint32LE(personalization, 4);
  }
  _initialState = _state.immutable;
  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;
  }
}