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

@cepharum/web-crypto-revised

v1.0.0

Published

higher-level crypto API for client and server

Downloads

36

Readme

A universal higher-level crypto library

License

MIT

About

This library provides an API for conveniently encrypting and decrypting data in a browser or on a server (using Node v18 or later).

Installation

npm i @cepharum/web-crypto-revised

Usage

The library is meant to work in the same way in both contexts. The only difference is the way it is imported. The following two examples are illustrating that in context of invoking a testDrive() method to check whether current runtime properly supports WebCrypto API or not.

Client

Assuming ECMA module syntax for frontend implementation, the integration looks like this:

import { testDrive } from "@cepharum/web-crypto-revised";

try {
	await testDrive();
} catch {
	// runtime does not support webcrypto API
}

// runtime supports webcrypto API

Server

Assuming CommonJS syntax for backend implementation, the integration looks like this:

const { testDrive } = await import( "@cepharum/web-crypto-revised" );

try {
	await testDrive();
} catch {
	// runtime does not support webcrypto API
}

// runtime supports webcrypto API

If your server code is based on ES module syntax instead, the integration works identical to the example given for the client above.

More examples

The library itself has been implemented using ES module syntax, hence it is the preferred way of using it in the following examples. Adopting these examples for the CommonJS syntax should be a straightforward task focusing one the import line.

Symmetric encryption

import { aesEncrypt } from "@cepharum/web-crypto-revised";

const data = { some: { data: [ "serializable", { to: "JSON" } ] } };
const password = "som3User.ProvidedPassworD";

const cipher = await aesEncrypt( data, password );

The resulting cipher is a JSON-serializable object.

Symmetric decryption

import { aesDecrypt } from "@cepharum/web-crypto-revised";

const password = "som3User.ProvidedPassworD";

const data = await aesDecrypt( cipher, password );

console.log( data ); // { some: { data: [ "serializable", { to: "JSON" } ] } }

Prepare for asymmetric encryption

For asymmetric encryption, a key pair has to be generated at some point.

import { createAsymmetricKeys } from "@cepharum/web-crypto-revised";

const keys = await createAsymmetricKeys();

After that, it's common to deploy and store those keys persistently in a less secure context. So, the keys have to be exported first:

import { exportKeyPair } from "@cepharum/web-crypto-revised";

const passphrase = "som3SecretPassPhrase.ForEncryptingThePrivateKey";

const { publicKey, privateKey } = await exportKeyPair( keys, passphrase );

Both exported keys are objects consisting of JSON-serializable properties. They are suitable e.g. for transmitting them over the network or for storing them in a database.

Asymmetric encryption

Using the public key exported before, the asymmetric encryption of data looks like this:

import { importPublicKey, rsaEncrypt } from "@cepharum/web-crypto-revised";

const data = { some: { data: [ "serializable", { to: "JSON" } ] } };

const publicKey = await importPublicKey( storedPublicKey );
const cipher = await rsaEncrypt( data, publicKey );

The resulting cipher is a JSON-serializable object.

Asymmetric decryption

Using the private key exported and wrapped before, the asymmetric decryption of data looks like this:

import { importPrivateKey, rsaDecrypt } from "@cepharum/web-crypto-revised";

const passphrase = "som3SecretPassPhrase.ForEncryptingThePrivateKey";

const privateKey = await importPrivateKey( storedPrivateKey, passphrase );
const data = await rsaDecrypt( cipher, privateKey );

console.log( data ); // { some: { data: [ "serializable", { to: "JSON" } ] } }