openSQLCipherOnWindows function

DynamicLibrary openSQLCipherOnWindows({
  1. bool useOpenSSLEmbededDlls = true,
})

This function open SQLCipher in memory and return the associated DynamicLibrary. Return null if app fail to open SQLCipher. set useOpenSSLEmbededDlls to false if you prefer let Windows searching DLLs on the system

Implementation

DynamicLibrary openSQLCipherOnWindows({bool useOpenSSLEmbededDlls = true}) {
  late DynamicLibrary library;

  String exeDirPath = File(Platform.resolvedExecutable).parent.path;
  print('executableDirectoryPath: $exeDirPath');

  String packageAssetsDirPath = normalize(join(exeDirPath, assets_package_dir));
  print('packageAssetsDirectoryPath: $packageAssetsDirPath');

  //OpenSSL libcryptoxxx.dll FullPath  destination
  String libCryptoDllDestFullPath =
      normalize(join(exeDirPath, openssl_lib_crypto_dll));
  //OpenSSL libsslxxx.dll FullPath  destination
  String libSSLDllDestFullPath =
      normalize(join(exeDirPath, openssl_lib_ssl_dll));

  //OpenSSL libcryptoxxx.dll FullPath  source
  String libCyptoDllSourceFullPath =
      normalize(join(packageAssetsDirPath, openssl_lib_crypto_dll));
  //OpenSSL libsslxxx.dll FullPath  source
  String libSSLDllSourceFullPath =
      normalize(join(packageAssetsDirPath, openssl_lib_ssl_dll));

  //Chek if it is needed to copy DLLs in another directory that my_app.exe could use when executing
  if (useOpenSSLEmbededDlls) {
    bool needToCopy = false;
    //Check if one of destination libraries does not exists
    if (File(libCryptoDllDestFullPath).existsSync() == false ||
        File(libSSLDllDestFullPath).existsSync() == false) {
      //Re sync both libraries
      needToCopy = true;
    } else if (File(libCryptoDllDestFullPath).existsSync() == true ||
        File(libSSLDllDestFullPath).existsSync() == true) {
      //Check if sizes are differents
      needToCopy = (File(libCryptoDllDestFullPath).lengthSync() !=
              File(libCyptoDllSourceFullPath).lengthSync()) ||
          (File(libSSLDllDestFullPath).lengthSync() !=
              File(libSSLDllSourceFullPath).lengthSync());
    }
    //Copy DLLs
    if (needToCopy) {
      File(libCyptoDllSourceFullPath).copySync(libCryptoDllDestFullPath);
      print(_yellow(
          '$openssl_lib_crypto_dll: copied from $libCyptoDllSourceFullPath to $libCryptoDllDestFullPath'));
      File(libSSLDllSourceFullPath).copySync(libSSLDllDestFullPath);
      print(_yellow(
          '$openssl_lib_ssl_dll: copied from $libSSLDllSourceFullPath to $libSSLDllDestFullPath'));
    }
  }

  //Now load the SQLCipher DLL
  try {
    String sqliteLibraryPath =
        normalize(join(packageAssetsDirPath, sqlcipher_windows_dll));
    print('SQLCipherLibraryPath: $sqliteLibraryPath');

    library = DynamicLibrary.open(sqliteLibraryPath);

    print(_yellow("SQLCipher successfully loaded"));
  } catch (e) {
    try {
      print(e);
      print(_red("Failed to load SQLCipher from library file, "
          "trying loading from system..."));

      library = DynamicLibrary.open('sqlcipher.dll');

      print(_yellow("SQLCipher successfully loaded"));
    } catch (e) {
      print(e);
      print(_red("Fail to load SQLCipher."));
    }
  }
  return library;
}