cipherSingleFile function

Future<CipherResult> cipherSingleFile({
  1. required String filePath,
  2. required String outputFilePath,
  3. required Nonce nonce,
  4. required Uint32List password,
})

Encrypts or decrypts a single File using the ChaCha20 algorithm

Implementation

Future<CipherResult> cipherSingleFile({
  required String filePath,
  required String outputFilePath,
  required Nonce nonce,
  required Uint32List password,
}) async {
  final input = File(filePath).openRead();
  final output = File(outputFilePath).openWrite();
  final completer = Completer<CipherResult>();

  try {
    await for (var chunk in input) {
      var processedChunk = ChaCha20Algorithm(password, 0, nonce.intList)
          .processBytes(Uint8List.fromList(chunk));
      await output.addStream(Stream.fromIterable([processedChunk]));
    }

    await output.flush();
    await output.close();

    completer.complete(CipherResult(File(outputFilePath), true));
  } catch (e) {
    completer.completeError(e);
  }

  return completer.future;
}