Ionic 3 and Sql Cipher - Can't pass key to encrypt database

Hello,

Previously I have been using sqlite with the cordova-sqlcipher-adapter. When opening/creating a database I could pass in a key to encrypt the database like so…

   let db = new SQLite();
      return db.openDatabase({
        name: DB_NAME,
        location: 'default',
        key: pwd,
      })

With the new version, I can’t see any way of passing my key in to encrypt/open the database. Am I missing something or has this functionality been unintentionally removed?

EDIT: To confirm - I am using ionic 3.0.1

To add more detail: the db.create() now takes a SQLiteDatabaseConfig object as a parameter. This object does not have a property on it to set the key for the database.

WORK AROUND
I have gotten around this by creating my config as an anonymous object with my properties in and passing it in.

I.E

 let db = new SQLite();
      var config = {
        name: DB_NAME,
        location: 'default',
        key: pwd,
      };
      return db.create(config)

Ionic Info…

Cordova CLI: 6.5.0
Ionic Framework Version: 3.0.1
Ionic CLI Version: 2.2.2
Ionic App Lib Version: 2.2.1
Ionic App Scripts Version: 1.3.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v6.9.5
Xcode version: Not installed

New version of what?

I am using ionic 3.0.1 and sql lite via an angular injectable as per this change

“With Ionic Native 3, native functionality was moved from static methods to using Angular injectables.”

Ok, so you also changed from Ionic Native 2.x to 3.x? That’s something completely different than Ionic itself.

Yes - sorry I did.

I just followed the ionic upgrade steps as detailed in the change log (https://github.com/driftyco/ionic/blob/master/CHANGELOG.md)

so I assumed that Ionic Native 3x was a requirement of upgrading to Ionic 3 and it was implied.

Where does “SQLite” actually come from? Are you using https://ionicframework.com/docs/native/sqlite/? Or is there are native wrapper for https://github.com/litehelpers/Cordova-sqlcipher-adapter? I’m still confused.

1 Like

I have added the Cordova-sqlcipher-adapter plugin and in my storage class I import from import { SQLite } from ‘@ionic-native/sqlite’;.

I have found a work-around to my problem now though so I have edited my original post.

My problem stemmed from the fact that db.create now takes an object of type SQLiteDatabaseConfig as a parameter which didn’t have the property to set the key to unlock the database.

So instead I just created an anonymous object for the config with the name, location and key and passed it into the db.create and it works fine.

I have tested it and it is unlocking the encrypted database and won’t unlock if I give it the wrong key.

I just got confused when SQLiteDatabaseConfig didn’t have a property to set the key.

Is this an oversight in the Ionic Native implementation?
https://ionicframework.com/docs/native/sqlite/#SQLiteDatabaseConfig

I suggest listening to @Sujan12 and doing it as documented in the docs as opposed to explicitly instantiating SQLite as described in the first post. You really really rarely want to ever be typing “new” in an Angular app.

Hi Cherylhanlon,
Still i am facing same issues. Could you please share sample code how to pass the encrypt key.

The work around is in my original post…

let db = new SQLite();
var config = {
name: DB_NAME,
location: ‘default’,
key: pwd,
};
return db.create(config)

1 Like

Argh. No. Don’t explicitly instantiate SQLite objects.

I couldn’t see any other way as @Sujan12 mentions above there seems to have been an oversight in the ionic native implementation. it was the only way I could find to pass the key in to encrypt the database.

If there is a different way to do this though, I would love to know.

Inject SQLite in your constructor as described in the docs. You can cast your config variable as follows:

let config = {...} as SQLiteDatabaseConfig;

ah I understand now - I am already doing that in my production , the sample above was just brief to explain my solution. My production code is this…


constructor(public platform: Platform, private events: Events, private sqLite: SQLite) {
  }

  public tryInit(pwd: string): Promise<any> {
    if (win.sqlitePlugin) {
      var config = {
        name: DB_NAME,
        location: 'default',
        key: pwd,
      };
      return this.sqLite.create(config)
        .then(sqlite => this._db = sqlite)
        .then(() => this.createTables())
        .catch(err => {
          this.events.publish(EventsPublished.DatabaseOpenFailed);
          return Promise.reject('unable to open database: ' + err);
        });
    }
    else {
      this._db = win.openDatabase(DB_NAME, '1.0', 'database', 5 * 1024 * 1024);
      return this.createTables();
    }
  }

BTW, you may wish to monitor this PR.