decrypt method

Iterable<int> decrypt(
  1. BlockCipher cipher,
  2. dynamic input, {
  3. Iterable<int>? iv,
  4. Padder padder = const PKCS7Padder(),
})

Implementation

Iterable<int> decrypt(BlockCipher cipher, /* String | Uint8List */ input,
    {Iterable<int>? iv, Padder padder = const PKCS7Padder()}) {
  Iterable<int> mangler;
  if (iv != null) {
    if (iv.length != cipher.blockSize) {
      throw Exception('Invalid initial vector length');
    }
    mangler = iv;
  } else {
    mangler = Uint8List(cipher.blockSize);
  }

  if (input is String) {
    input = base64Decode(input);
  }

  final numBlocks = (input.length / cipher.blockSize).ceil();
  final decrypted = Uint8List(numBlocks * cipher.blockSize);

  int offset = 0;
  for (int i = 0; i < numBlocks; i++) {
    final inputBlock = input.buffer.asByteData(offset, cipher.blockSize);
    final outputBlock = decrypted.buffer.asByteData(offset, cipher.blockSize);
    cipher.processBlock(inputBlock, outputBlock);
    ListOps.xorToByteData(outputBlock, mangler);
    mangler = input.skip(offset).take(cipher.blockSize);
    offset += cipher.blockSize;
  }

  final unpadded = padder.unpad(cipher.blockSize, decrypted).toList();

  return unpadded;
}