3.0.0 • Published 8 days ago

@pvermeer/dexie-encrypted-addon v3.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 days ago

Dexie Encrypted Addon (rc)

NPM Version Build Status semantic-release Conventional Commits

Install over npm

npm install @pvermeer/dexie-encrypted-addon

Encrypt your data the Dexie way!

Plugin is written to be as easy to use as Dexie.js itself.

Added Schema Syntax

SymbolDescription
$Encrypt this value (does not create an index)
#Only as first key: Hash the original document and create an index
const friend = '#id, $name, age';
{
    id: 'hashed id', // Indexed
    name: 'some encryption string', // Not indexed
    age: 34, // Indexed
    shoeSize: 42 // Not indexed
}

Using $ on your keys will encrypt these keys.

Using # on the first key will hash this key with the document on creation. This will create an unique primary key based on the document itself and will update or create the key on the document itself.

Wait for open

Always open the database yourself. Dexie does not wait for all hooks to be subscribed (bug?).

await db.open();

To help with this, the option 'autoOpen' has been disabled.

Indices

Encrypted values will not be indexed. IndexedDB does not support index based on encryption. Doing where() calls would mean the whole collection has to be read and decrypted (unless someone has a better idea? PR's are always welcome :D). Implementing this yourself would be more performend when also modeling the database to support this.

Immutable

By default immutablity is applied to all creation and update methods via overrides. Dexie does not do this by default. This is recommended so your original input object does not change after encrypting values or hashing the primary index key. This behavior can be disabled via the options object provided to the addon:

interface EncryptedOptions {
    secretKey?: string;
    immutable?: boolean; // Default true
}

Setting this to false can lead to unexpected / weird behavior in your application

Example (ES6)

import Dexie from 'dexie';
import { encrypted, Encryption } from '@pvermeer/dexie-encrypted-addon';

// Generate a random key
const secret = Encryption.createRandomEncryptionKey();

// Declare Database
const db = new Dexie("FriendDatabase", {
    addons: [encrypted.setOptions({ secretKey: secret })]
});
db.version(1).stores({ friends: "#id, $name, $shoeSize, age" });

// Open the database
db.open()
    .then(() => {
        console.log('DB loaded! :D')
        // Use Dexie
    });

Example (Typescript)

import Dexie from 'dexie';
import { encrypted, Encryption } from '@pvermeer/dexie-encrypted-addon';

interface Friend {
    id?: number;
    name?: string;
    shoeSize?: number;
    age?: number;
}

// Generate a random key
const secret = Encryption.createRandomEncryptionKey();

// Declare Database
class FriendsDatabase extends Dexie {
    public friends: Dexie.Table<Friend, string>;
    constructor(name: string, secret?: string) {
        super(name);
        encrypted(this, { secretKey: secret });
        this.version(1).stores({
            friends: '#id, $name, $shoeSize, age'
        });
    }
}

const db = new FriendDatabase('FriendsDatabase', secret);

// Open the database
db.open()
    .then(() => {
        console.log('DB loaded! :D')
        // Use Dexie
    });

Example (HTML import)

Bundled & minified package: https://unpkg.com/@pvermeer/dexie-encrypted-addon@latest/dist/dexie-encrypted-addon.min.js.

Addon is export as namespace DexieEncryptedAddon

<!doctype html>
<html>
    <head>
        <!-- Include Dexie -->
        <script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>

        <!-- Include DexieEncryptedAddon (always after Dexie, it's a dependency) -->
        <script src="https://unpkg.com/@pvermeer/dexie-encrypted-addon@latest/dist/dexie-encrypted-addon.min.js"></script>

        <script>
            // Generate a random key
            const secret = Encryption.createRandomEncryptionKey();

            // Define your database
            const db = new Dexie("FriendDatabase", {
                addons: [DexieEncryptedAddon.encrypted.setOptions({ secretKey: secret })]
            });
            db.version(1).stores({ friends: "#id, $name, $shoeSize, age" });

            // Open the database
            db.open()
                .then(() => {
                    console.log('DB loaded! :D')
                    // Do Dexie stuff
                });
        </script>
    </head>
</html>

API

The packet exposes two exports:

encrypted - addon function

/**
 * @secretKey Your previously saved secret.
 * @immutable Set to false to disable immutable state on document creation and updates.
 */
interface EncryptedOptions {
    secretKey?: string;
    immutable?: boolean;
}
/** 
 * @method setOptions(string) Set options and return the addon.
 * @param options Set secret key and / or immutable create methods.
 * @returns The secret key (provided or generated)
 */ 
function encrypted(db: Dexie, options?: EncryptedOptions): string;
/**
 * Namespace to set options and return the addon function when used in (ES2016 / ES7)
 */ 
namespace encrypted {
    function setOptions(options: EncryptedOptions): string;
}

Encryption - class

/**
 * Class with cryptic methods
 */
class Encryption {
    readonly secret: string;
    readonly secretUint8Array: Uint8Array;
    /**
     * Create a random key.
     */
    static createRandomEncryptionKey(): string;
    /**
     * Create a base64 hash string of the provided input.
     * @param input Any non-circulair value.
     */
    static hash(input: any): string;
    
    constructor(secret?: string);
}

Dexie.js

Dexie.js is a wrapper library for indexedDB - the standard database in the browser. http://dexie.org

3.0.1-beta.0

8 days ago

4.0.0-beta.0

8 days ago

3.0.0-beta.3

12 months ago

3.0.0

12 months ago

3.0.0-beta.1

1 year ago

3.0.0-beta.2

1 year ago

3.0.0-beta.0

1 year ago

2.0.6-beta.0

1 year ago

2.0.5

1 year ago

2.0.5-beta.0

1 year ago

2.0.3

1 year ago

2.0.4

1 year ago

2.0.3-beta.4

1 year ago

2.0.3-beta.3

1 year ago

2.0.3-beta.2

1 year ago

2.0.3-beta.1

1 year ago

2.0.3-beta.0

1 year ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0-beta.17

2 years ago

2.0.0-beta.15

2 years ago

2.0.0-beta.16

2 years ago

2.0.0-beta.9

2 years ago

2.0.0-beta.8

2 years ago

2.0.0-beta.11

2 years ago

2.0.0-beta.10

2 years ago

2.0.0-beta.14

2 years ago

2.0.0-beta.13

2 years ago

2.0.0-beta.12

2 years ago

2.0.0-beta.7

2 years ago

2.0.0-beta.6

3 years ago

2.0.0-beta.5

4 years ago

2.0.0-beta.4

4 years ago

2.0.0-beta.3

4 years ago

2.0.0-beta.2

4 years ago

2.0.0-beta.1

4 years ago

1.1.0-beta.2

4 years ago

1.1.0-beta.1

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago