A complete implementation of SQLCipher (encrypted SQLite) for WebAssembly using OpenSSL as the crypto provider, designed to work in web browsers.
This project demonstrates:
- Building OpenSSL for WebAssembly
- Building SQLCipher for WebAssembly with OpenSSL crypto
- Creating encrypted databases with C++
- Opening and querying encrypted databases in the browser via WebAssembly
┌─────────────────┐
│ C++ Application │ Creates encrypted database
│ (Native) │ Encodes to base64
└────────┬────────┘
│
▼
┌──────────────┐
│ Base64 Data │
└──────┬───────┘
│
▼
┌──────────────────────┐
│ Browser (WebAssembly)│ Decodes base64
│ + SQLCipher WASM │ Opens with password
│ + OpenSSL WASM │ Queries data
└──────────────────────┘
This project uses Nix with direnv for dependency management:
# The environment is automatically loaded via direnv
# If not, run: direnv allow-
Build OpenSSL for WebAssembly:
./build-openssl.sh
-
Build SQLCipher amalgamation:
./build-sqlcipher.sh
-
Build native tools and create test database:
./build-native-tools.sh
This creates:
test/encrypted.db- Encrypted SQLite databasetest/encrypted_db_base64.txt- Base64 encoded database
-
Build WebAssembly module:
./build-wasm.sh
This creates:
www/sqlcipher.js- JavaScript loaderwww/sqlcipher.wasm- WebAssembly module
-
Start the HTTP server:
chmod +x serve.sh ./serve.sh
-
Open in browser: Navigate to
http://localhost:8000 -
Click "Load Encrypted Database" to:
- Fetch the base64 encoded database
- Decode and decrypt it with password
test_password_123 - Query the
userstable - Display results
The test database contains a users table:
| Column | Type | Description |
|---|---|---|
| id | INTEGER | Primary key |
| name | TEXT | User name |
| TEXT | ||
| age | INTEGER | Age |
Sample data:
- Alice Johnson, [email protected], 30
- Bob Smith, [email protected], 25
- Charlie Brown, [email protected], 35
.
├── build/
│ ├── openssl/ # OpenSSL built for WebAssembly
│ ├── sqlcipher/ # SQLCipher amalgamation
│ └── native/ # Native compiled tools
├── src/
│ ├── create_db.cpp # C++ program to create encrypted DB
│ └── sqlcipher_wasm.cpp # WebAssembly bindings
├── test/
│ ├── encrypted.db # Encrypted database file
│ └── encrypted_db_base64.txt # Base64 encoded database
├── vendor/
│ ├── openssl/ # OpenSSL source
│ └── sqlcipher/ # SQLCipher source
├── www/
│ ├── index.html # Test interface
│ ├── sqlcipher.js # WASM loader
│ ├── sqlcipher.wasm # WASM module
│ └── encrypted_db_base64.txt # Database file
├── build-openssl.sh # Build script for OpenSSL
├── build-sqlcipher.sh # Build script for SQLCipher
├── build-native-tools.sh # Build script for native tools
├── build-wasm.sh # Build script for WASM module
├── serve.sh # HTTP server for testing
└── flake.nix # Nix development environment
This implementation uses OpenSSL as the crypto provider for SQLCipher, compiled for WebAssembly. This provides:
- AES-256-CBC encryption
- HMAC-SHA512 for authentication
- PBKDF2 key derivation
- SQLITE_ENABLE_DESERIALIZE: Allows loading database from memory buffer
- No Node.js dependencies: Works in pure browser environment
- ES6 Modules: Modern JavaScript module system
- Emscripten Embind: Clean C++ to JavaScript bindings
Requires a modern browser with:
- WebAssembly support
- ES6 module support
- Fetch API
Tested on:
- Chrome/Edge 90+
- Firefox 88+
- Safari 14+
- Database password:
test_password_123(for testing only!) - In production, use strong passwords
- Consider additional encryption for base64 transport
- Validate and sanitize all SQL queries
This project uses:
- OpenSSL (Apache License 2.0)
- SQLCipher (BSD-style license)
- Ensure server is running (WASM files must be served via HTTP)
- Check browser console for CORS errors
- Verify
sqlcipher.wasmfile exists inwww/directory
- Verify password is correct:
test_password_123 - Check that database file was created correctly
- Ensure base64 file is not corrupted
- Make sure you're in the NixOS environment (
direnv allow) - Run build scripts in order (OpenSSL → SQLCipher → Native → WASM)
- Check that all dependencies are available
- WASM module size: ~2.4MB (includes SQLCipher + OpenSSL)
- Initial load time: ~100-500ms (depends on network)
- Query performance: Near-native speed thanks to WebAssembly
- Support for multiple databases
- Write operations (INSERT, UPDATE, DELETE)
- Database export to encrypted file
- Password change functionality
- TypeScript definitions
- NPM package