encryptFile method

  1. @override
FutureOr<File> encryptFile({
  1. required File file,
  2. required String writeToDirectory,
  3. String moduleKey = defaultModuleKey,
})
override

Implementation

@override
FutureOr<File> encryptFile({required File file, required String writeToDirectory, String moduleKey  = defaultModuleKey}) async {

  bool isFileExist = await file.exists();
  if(!isFileExist){throw NUICryptoException(cause: NUICryptoError.FILE_TO_ENCRYPT_NOT_FOUND);}

  String baseFilename = file.path.split('/').last;
  const String encryptedExtension = ".encrypted";

  String writeTo = writeToDirectory + "${writeToDirectory.endsWith('/') ? "" : "/"}" + baseFilename + encryptedExtension;

  //Info to pass into isolate
  if(!module.containsKey(moduleKey)){
    throw NUICryptoException(cause: NUICryptoError.KEY_NOT_FOUND);
  }

  final sink = File(writeTo).openWrite();

  try{
    final streamData = file.openRead();
    final encryptedStream = await _doEncryptionFile(streamData, module[moduleKey]!);
    await sink.addStream(encryptedStream);

  }on Exception catch(e){
    logNUI("NUICryptography", "Encryption Exception ${e.toString()}");
    rethrow;
  }finally {
    sink.close();
    return File(writeTo);
  }


}