Skip to main content
Version: 0.32.1

Encryption

NitromelonDB can encrypt the SQLite database on disk, in the same file format as SQLCipher 4. Encryption is opt-in, at build time: apps that don't ask for it ship exactly what they shipped before, with no cryptography from NitromelonDB in them.

Using it takes two steps:

  1. Turn on the encryption build, so the code that can encrypt is compiled into your app.
  2. Give SQLiteAdapter a passphrase. For an app that already has users with plain databases, encrypt them in place.
Status

The encryption build and the passphrase API work on iOS, Android and Windows, and on Node.js and Electron with better-sqlite3-multiple-ciphers (#60). The web (LokiJS, wa-sqlite) refuses the encryption option with an error.

In this section​

At a glance​

Default buildEncryption build
How to get itnothing to do"nitromelondb": { "encryption": true } in your app's package.json
SQLite on iOSthe system libsqlite3bundled SQLite3 Multiple Ciphers (built on SQLite 3.54.1)
SQLite on Android and Windowsbundled SQLite 3.54.1bundled SQLite3 Multiple Ciphers (built on SQLite 3.54.1)
SQLite on Node.js and Electronbetter-sqlite3better-sqlite3-multiple-ciphers (no build switch)
Can encrypt a databaseno: a passphrase fails with encryption_unavailableyes
Cryptography in your app from NitromelonDBnoneAES-256, HMAC-SHA512, PBKDF2 (all standard)
App size (per arm64 slice)unchangedabout +1.2 MB on iOS, about +110 KB on Android
Databases without a keyplain SQLite filesplain SQLite files, byte for byte the same

Why it's a switch and not just an option​

We want moving from WatermelonDB to be as easy as possible: change the package, rebuild, and your app works and ships as before. Compiling encryption into every app would break that, even for apps that never set a key:

  1. The SQLite engine on iOS. Like WatermelonDB, NitromelonDB uses the system SQLite on iOS by default. The system one can't encrypt, so the encryption build bundles its own. It matches the settings of Apple's build (see What changes in the encryption build), but it is a different engine, and you should choose when to switch.
  2. App size. The cipher adds about 1.2 MB per arm64 slice on iOS and about 110 KB on Android (before App Store thinning and compression).
  3. Export compliance. An app that contains encryption has to say so to Apple, including encryption in the libraries it links. With the cipher in every build, some publishers would have to change their answers, and possibly file a French encryption declaration, just by updating. Details in App Store and export compliance.

The switch is a key in your app's package.json because CocoaPods, Gradle and MSBuild all read that file, so all three builds get the same answer.

Should you turn it on?​

Encryption at rest protects the database file. It helps when someone gets a copy of that file without the key:

  • a backup that includes the app's data,
  • a lost, stolen or seized device that is unlocked, rooted or jailbroken, or examined with forensic tools,
  • malware or another app that can read your app's files but not its memory or Keychain / Keystore entries,
  • an auditor or a customer contract that requires application-level encryption of stored data (common in health, finance and enterprise apps).

It does not protect against:

  • code running inside your app, or a debugger attached to it. While the database is open, the key is in memory.
  • a key stored somewhere an attacker can read. Where you keep the passphrase (Keychain, Android Keystore, derived from the user's password, fetched from your server) decides how much the encryption is worth.
  • data you send elsewhere: sync payloads, logs, crash reports, analytics.

Also keep in mind that iOS and Android already encrypt app storage while the device is locked (iOS Data Protection, Android file-based encryption). Database encryption adds protection on top of that, mostly while the device is unlocked, in backups, and on compromised devices. If none of the cases above apply to your app, you probably don't need it.

What's next​

The design is in the encryption plan. Still to come:

  1. iOS App Groups: a plaintext header, so that iOS doesn't kill an app that keeps an encrypted database open in a shared container (see More than one connection).

The web (LokiJS, wa-sqlite) is not part of this work.