Skip to content

7mind/sqlcipher-wasm-alt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLCipher WebAssembly Project

A complete implementation of SQLCipher (encrypted SQLite) for WebAssembly using OpenSSL as the crypto provider, designed to work in web browsers.

Project Overview

This project demonstrates:

  1. Building OpenSSL for WebAssembly
  2. Building SQLCipher for WebAssembly with OpenSSL crypto
  3. Creating encrypted databases with C++
  4. Opening and querying encrypted databases in the browser via WebAssembly

Architecture

┌─────────────────┐
│ C++ Application │ Creates encrypted database
│  (Native)       │ Encodes to base64
└────────┬────────┘
         │
         ▼
   ┌──────────────┐
   │ Base64 Data  │
   └──────┬───────┘
          │
          ▼
┌──────────────────────┐
│ Browser (WebAssembly)│ Decodes base64
│  + SQLCipher WASM    │ Opens with password
│  + OpenSSL WASM      │ Queries data
└──────────────────────┘

Build Instructions

Prerequisites

This project uses Nix with direnv for dependency management:

# The environment is automatically loaded via direnv
# If not, run: direnv allow

Build Steps

  1. Build OpenSSL for WebAssembly:

    ./build-openssl.sh
  2. Build SQLCipher amalgamation:

    ./build-sqlcipher.sh
  3. Build native tools and create test database:

    ./build-native-tools.sh

    This creates:

    • test/encrypted.db - Encrypted SQLite database
    • test/encrypted_db_base64.txt - Base64 encoded database
  4. Build WebAssembly module:

    ./build-wasm.sh

    This creates:

    • www/sqlcipher.js - JavaScript loader
    • www/sqlcipher.wasm - WebAssembly module

Testing

  1. Start the HTTP server:

    chmod +x serve.sh
    ./serve.sh
  2. Open in browser: Navigate to http://localhost:8000

  3. Click "Load Encrypted Database" to:

    • Fetch the base64 encoded database
    • Decode and decrypt it with password test_password_123
    • Query the users table
    • Display results

Database Schema

The test database contains a users table:

Column Type Description
id INTEGER Primary key
name TEXT User name
email TEXT Email
age INTEGER Age

Sample data:

Project Structure

.
├── 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

Technical Details

Crypto Provider

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

WebAssembly Features

  • 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

Browser Compatibility

Requires a modern browser with:

  • WebAssembly support
  • ES6 module support
  • Fetch API

Tested on:

  • Chrome/Edge 90+
  • Firefox 88+
  • Safari 14+

Security Considerations

  • 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

License

This project uses:

  • OpenSSL (Apache License 2.0)
  • SQLCipher (BSD-style license)

Troubleshooting

"Failed to load WebAssembly module"

  • Ensure server is running (WASM files must be served via HTTP)
  • Check browser console for CORS errors
  • Verify sqlcipher.wasm file exists in www/ directory

"Failed to open database"

  • Verify password is correct: test_password_123
  • Check that database file was created correctly
  • Ensure base64 file is not corrupted

Build errors

  • 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

Performance

  • WASM module size: ~2.4MB (includes SQLCipher + OpenSSL)
  • Initial load time: ~100-500ms (depends on network)
  • Query performance: Near-native speed thanks to WebAssembly

Future Enhancements

  • Support for multiple databases
  • Write operations (INSERT, UPDATE, DELETE)
  • Database export to encrypted file
  • Password change functionality
  • TypeScript definitions
  • NPM package

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published