Blake2sHash constructor

Blake2sHash(
  1. int digestSize, {
  2. List<int>? key,
  3. List<int>? salt,
  4. List<int>? aad,
})

For internal use only.

Implementation

Blake2sHash(
  int digestSize, {
  this.key,
  List<int>? salt,
  List<int>? aad,
})  : hashLength = digestSize,
      derivedKeyLength = digestSize,
      super(64) {
  if (digestSize < 1 || digestSize > 32) {
    throw ArgumentError('The digest size must be between 1 and 32');
  }

  // Parameter block from the seed
  _s0 = _seed[0] ^ 0x01010000 ^ hashLength;
  _s1 = _seed[1];
  _s2 = _seed[2];
  _s3 = _seed[3];
  _s4 = _seed[4];
  _s5 = _seed[5];
  _s6 = _seed[6];
  _s7 = _seed[7];

  // Add key length to parameter
  if (key != null && key!.isNotEmpty) {
    if (key!.length > 32) {
      throw ArgumentError('The key should not be greater than 32 bytes');
    }
    _s0 ^= key!.length << 8;
  }

  if (salt != null && salt.isNotEmpty) {
    if (salt.length != 8) {
      throw ArgumentError('The valid length of salt is 8 bytes');
    }
    for (int i = 0, p = 0; i < 4; i++, p += 8) {
      _s4 ^= (salt[i] & 0xFF) << p;
    }
    for (int i = 4, p = 0; i < 8; i++, p += 8) {
      _s5 ^= (salt[i] & 0xFF) << p;
    }
  }

  if (aad != null && aad.isNotEmpty) {
    if (aad.length != 8) {
      throw ArgumentError('The valid length of personalization is 8 bytes');
    }
    for (int i = 0, p = 0; i < 4; i++, p += 8) {
      _s6 ^= (aad[i] & 0xFF) << p;
    }
    for (int i = 4, p = 0; i < 8; i++, p += 8) {
      _s7 ^= (aad[i] & 0xFF) << p;
    }
  }

  reset();
}