decryptFile method

SealdClearFile decryptFile(
  1. Uint8List encryptedFile
)

Decrypts an encrypted file into the corresponding clear-text file.

encryptedFile - A Uint8List of the content of the encrypted file to decrypt. Returns a SealdClearFile instance, containing the decrypted file.

Implementation

SealdClearFile decryptFile(Uint8List encryptedFile) {
  // Dart FFI forces us to copy the data from Uint8List to a newly allocated Pointer<Uint8>
  final Pointer<Uint8> nativeEncryptedFile =
      calloc<Uint8>(encryptedFile.length);
  final pointerList = nativeEncryptedFile.asTypedList(encryptedFile.length);
  pointerList.setAll(0, encryptedFile);
  final Pointer<Pointer<NativeSealdClearFile>> result =
      calloc<Pointer<NativeSealdClearFile>>();
  final Pointer<Pointer<NativeSealdError>> err =
      calloc<Pointer<NativeSealdError>>();

  final int resultCode = _bindings.SealdEncryptionSession_DecryptFile(
      _ptr.pointer(), nativeEncryptedFile, encryptedFile.length, result, err);

  calloc.free(nativeEncryptedFile);

  if (resultCode != 0) {
    calloc.free(result);
    throw SealdException._fromCPtr(err);
  } else {
    final clearFile = SealdClearFile._fromC(result.value);
    calloc.free(result);
    calloc.free(err);
    return clearFile;
  }
}