npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fs-json-store-encryption-adapter

v3.0.4

Published

Encryption adapter for the "fs-json-store" module

Downloads

117

Readme

fs-json-store-encryption-adapter

is an encryption adapter for the fs-json-store module.

GitHub Actions CI

Features

  • Predefined presets.
  • Switching between built-in node's crypto and libsodium (like Argon2 key derivation function) implementations.
  • Keeping all the needed options in the produced buffer in along with the encrypted data itself.
  • Random salting is enabled for every key derivation and encryption execution, which helps against the lookup tables and rainbow tables hash cracking attacks.

Implementation Notes

  • Module executes a native libsodium code with help of the sodium-native bindings library. It's supposed to work faster than the WebAssembly versions.
  • Module can be used as a general purpose Buffer encryption library, adapter simply implements the following interface:
export interface Adapter {
    write(data: Buffer): Promise<Buffer>;
    read(data: Buffer): Promise<Buffer>;
}

Supported Presets

key derivation

  • type: sodium.crypto_pwhash
    • preset: mode:interactive|algorithm:default - default algorithm as for now is Argon2id.
    • preset: mode:moderate|algorithm:default
    • preset: mode:sensitive|algorithm:default
  • type: pbkdf2
    • preset: mode:interactive|digest:sha256
    • preset: mode:moderate|digest:sha256
    • preset: mode:sensitive|digest:sha256

encryption

  • type: sodium.crypto_secretbox_easy
    • preset: algorithm:default
  • type: crypto
    • preset: algorithm:aes-256-cbc
    • preset: algorithm:aes-256-cbc-hmac-sha256

You should not rely on the types / presets respective inner values, but only on the types / presets names listed above. Presets values can be changed in the code in any time (for example, increasing key derivation work factor), but that won't break the decryption of the previously encrypted data since adapter stores all the encryption options in the same buffer and so decryption can be reproduced even if values of the presets have been changed in the code with new module release.

Usage Examples

Using TypeScript and async/await:

import {Model, Store} from "fs-json-store";
import {EncryptionAdapter} from "fs-json-store-encryption-adapter";

interface DataModel extends Partial<Model.StoreEntity> {
    someProperty: string;
}

const password = process.env.STORE_PASSWORD;

if (!password) {
    throw new Error("Empty password is not allowed");
}

// data file example
(async () => {
    const store = new Store<DataModel>({
        file: "./data.bin",
        adapter: new EncryptionAdapter({
            password,
            preset: {
                keyDerivation: {type: "sodium.crypto_pwhash", preset: "mode:interactive|algorithm:default"},
                encryption: {type: "sodium.crypto_secretbox_easy", preset: "algorithm:default"},
            },
        }),
    });
    const data = await store.write({someProperty: "super secret data"}); // writes encrypted data into the `./data.bin` file
    console.log(data); // prints stored data
    console.log(await store.read()); // reads and prints stored data
})();

// standalone example using default options
(async () => {
    const adapter = EncryptionAdapter.default({password});
    const dataBuffer = Buffer.from("super secret data");
    const encryptedDataBuffer = await adapter.write(dataBuffer);
    const decryptedDataBuffer = await adapter.read(encryptedDataBuffer);
    console.log(decryptedDataBuffer.toString()); // prints `super secret data`
})();

Using JavaScript and Promises:

const {Store} = require("fs-json-store");
const {EncryptionAdapter} = require("fs-json-store-encryption-adapter");

const password = process.env.STORE_PASSWORD;

if (!password) {
    throw new Error("Empty password is not allowed");
}


// data file example
(() => {
    const store = new Store({
        file: "./data.bin",
        adapter: new EncryptionAdapter({
            password,
            preset: {
                keyDerivation: {type: "pbkdf2", preset: "mode:interactive|digest:sha256"},
                encryption: {type: "sodium.crypto_secretbox_easy", preset: "algorithm:default"},
            },
        }),
    });

    store
        .write({someProperty: "super secret data"}) // writes encrypted data into the `./data.bin` file
        .then((data) => console.log(data)) // prints stored data
        .then(() => store.read()) // reads stored data
        .then(console.log); // prints stored data
})();

// standalone example using default options
(() => {
    const adapter = EncryptionAdapter.default({password});

    adapter
        .write(Buffer.from("super secret data"))
        .then((encryptedDataBuffer) => adapter.read(encryptedDataBuffer))
        .then((decryptedDataBuffer) => console.log(decryptedDataBuffer.toString())); // prints `super secret data`
})();