Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if use sqlcipher_flutter_libs #3226

Open
andim27 opened this issue Sep 18, 2024 · 1 comment
Open

Error if use sqlcipher_flutter_libs #3226

andim27 opened this issue Sep 18, 2024 · 1 comment
Labels
question Further information is requested

Comments

@andim27
Copy link

andim27 commented Sep 18, 2024

Android, mobile
drift: ^2.20.2
drift_flutter: ^0.1.0
sqlite3: ^2.4.6
sqlcipher_flutter_libs: ^0.6.4

NativeDatabase.createInBackground(
      file,
      isolateSetup: () async {
        if (Platform.isAndroid) {
          setupSqlCipher();//code from docs
        }
      },
      setup: (rawDb) {
        final result = rawDb.select('pragma cipher_version');
        if (result.isEmpty) {
           throw UnsupportedError(
             'This database needs to run with SQLCipher, but that library is '
             'not available!',
           );

        }
        final escapedKey = _encryptionPassword.replaceAll("'", "''");
        rawDb.execute("PRAGMA key = '$escapedKey';");
        rawDb.config.doubleQuotedStringLiterals = false;
        final firstRes = rawDb.select('select count(*) from sqlite_master');
        //---Error:
       //.    SqliteException(26): while preparing statement, file is not a database, file is not a database (code 26)
        //.   I/flutter ( 5781):   Causing statement: select count(*) from sqlite_master
      },
    );
  });

Please answer because the documentation is not clear.
Do I need encrypted db-file before using(and call) sqlcipher_flutter_libs ?

@simolus3
Copy link
Owner

Unfortunately you can't reliably use drift_flutter and sqlcipher_flutter_libs in the same app, as drift_flutter depends on sqlite3_flutter_libs (Android might be the only platform where that doesn't immediately fail the build, you'll run into issues on other platforms for sure). There's a manual setup dropdown here which you can use as a reference for avoiding drift_flutter in this case.

Considering that pragma cipher_version works, the library should be set up correctly. So you're either using the wrong key (or you're using an existing unencrypted database, if that was your question?)

Do I need encrypted db-file before using(and call) sqlcipher_flutter_libs ?

You can't encrypt a previously unencrypted database with PRAGMA key alone, you'd have to do this.
Depending on your setup, you could make sure the existing database (if it's e.g. loaded from assets) is effectively unencrypted by using a static hardcoded key, and then use pragma rekey to encrypt the database with a secure key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants