decryptMutableBytes function

Future<Uint8List> decryptMutableBytes(
  1. Uint8List data,
  2. Uint8List key, {
  3. required CryptoImplementation crypto,
})

Implementation

Future<Uint8List> decryptMutableBytes(
  Uint8List data,
  Uint8List key, {
  required CryptoImplementation crypto,
}) async {
  if (key.length != encryptionKeyLength) {
    throw 'wrong encryptionKeyLength (${key.length} != $encryptionKeyLength)';
  }

  // Validate that the size of the data corresponds to a padded block.
  if (!checkPaddedBlock(data.length)) {
    throw "Expected parameter 'data' to be padded encrypted data, length was '${data.length}', nearest padded block is '${padFileSizeDefault(data.length)}'";
  }

  final version = data[1];
  if (version != 0x01) {
    throw 'Invalid version';
  }

  // Extract the nonce.
  final nonce = data.sublist(2, encryptionNonceLength + 2);

  var decryptedBytes = await crypto.decryptXChaCha20Poly1305(
    key: key,
    nonce: nonce,
    ciphertext: data.sublist(
      encryptionNonceLength + 2,
    ),
  );

  final lengthInBytes = decryptedBytes.sublist(0, 4);

  final length = decodeEndian(lengthInBytes);

  return decryptedBytes.sublist(4, length + 4);
}