Git Product home page Git Product logo

browser-crypto-secure-store's Introduction

Store Crypto KeyPairs securely in the browser

GitHub Release Build and Test

A tiny library with no third party dependencies other than browser APIs that allows generating and storing CryptoKeyPairs without the private key material ever being exposed directly to the browser.

Key pairs are generated and then stored as objects within IndexedDb with optional TTLs.

All this library does is wrap the integration between the Web Crypto API and IndexedDb to make it easier to store and retrieve non-extractable keys for signing/encryption in the browser.

It also provides a ttl option to automatically remove keys after a certain amount of time.

Supports ESM, UMD and the browser directly.

** PRs and contributions are welcome! **

Usage

npm install browser-crypto-secure-store

import { SecureStore } from 'browser-crypto-secure-store';

// Create a new instance of the SecureStore
const secureStore = new SecureStore();

// this creates and stores a non extractable keypair in indexedDB. RS256 is used by default 
const keyPair = await secureStore.setKey({ key: "key" } );

// create a new keypair with a TTL of 1 hour
const keyPair = await secureStore.setKey({ ley: "key", ttl: 3600 * 1000});

// create a new keypair with custom crypto configuration options
const options = {
    algorithm: {
        name: "RSA-OAEP",
        modulusLength: 2048,
        publicExponent: new Uint8Array([1, 0, 1]),
        hash: "SHA-256",
    },
    extractable: false,
    keyUsages: ["encrypt", "decrypt"],
}

const keyPair = await secureStore.setKey({ key: "key", options: options });

// Retreive the keyPair object
const keyPair = await secureStore.getKey("key");
const message = "Hello, World!";

// sign the message with our new private key
const signature = await window.crypto.subtle.sign(
    {
        name: "ECDSA",
        hash: { name: "SHA-256" },
    },
    keyPair.privateKey,
    new TextEncoder().encode(message),
);

// Remove the key from persistent storage

await removeKey("key");

browser-crypto-secure-store's People

Contributors

dbfr3qs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.