preHashingDigest method

  1. @visibleForTesting
Uint8List preHashingDigest({
  1. required List<int> password,
  2. required List<int> nonce,
  3. required List<int> optionalSecret,
  4. required List<int> associatedData,
})

Computes the pre-hashing digest.

Implementation

@visibleForTesting
Uint8List preHashingDigest({
  required List<int> password,
  required List<int> nonce,
  required List<int> optionalSecret,
  required List<int> associatedData,
}) {
  // Allocate bytes
  final capacity = 10 * 4 +
      password.length +
      nonce.length +
      optionalSecret.length +
      associatedData.length;
  final tmp = ByteData(capacity);

  // Set 6 fixed parameters
  var i = 0;
  tmp.setUint32(i, parallelism, Endian.little);
  i += 4;
  tmp.setUint32(i, hashLength, Endian.little);
  i += 4;
  tmp.setUint32(i, memory, Endian.little);
  i += 4;
  tmp.setUint32(i, iterations, Endian.little);
  i += 4;
  tmp.setUint32(i, version, Endian.little);
  i += 4;
  tmp.setUint32(i, mode.code, Endian.little);
  i += 4;

  // Set 4 variable-length parameters
  i = _setSequence(tmp, i, password);
  i = _setSequence(tmp, i, nonce);
  i = _setSequence(tmp, i, optionalSecret);
  i = _setSequence(tmp, i, associatedData);

  // BLAKE2
  final tmpBytes = Uint8List.view(
    tmp.buffer,
    tmp.offsetInBytes,
    tmp.lengthInBytes,
  );
  final result = const DartBlake2b().hashSync(tmpBytes).bytes as Uint8List;
  tmpBytes.fillRange(0, tmpBytes.length, 0);
  return result;
}