decrypt method

  1. @override
BuBytes decrypt(
  1. List<int> input, [
  2. List<int>? dst
])
override

Decrypts exactly one blockSize-byte block.

Implementation

@override
BuBytes decrypt(List<int> input, [List<int>? dst]) {
  final out = BuBytes.unsafeMutable(dst ?? List<int>.filled(blockSize, 0));

  if (input.length != blockSize) {
    throw CoreErrors.invalidOperationArguments(
      "decrypt",
      name: "input",
      reason: "Invalid input bytes length.",
    );
  }
  if (out.length != blockSize) {
    throw CoreErrors.invalidOperationArguments(
      "decrypt",
      name: "dst",
      reason: "Invalid destination bytes length.",
    );
  }

  // Always copy input first because decryptBlock may mutate the buffer
  final saved = input.clone();

  // Decrypt into tmpBlock (never into input!)
  _cipher.decryptBlock(saved, _tmpBlock);
  // XOR with previous block
  for (int i = 0; i < blockSize; i++) {
    out[i] = (_tmpBlock[i] ^ _prevBlock[i]).toU8;
  }

  // Update chaining value
  _prevBlock.setAll(0, saved);

  return out;
}