1.0.0 • Published 11 months ago

@cepharum/web-crypto-revised v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

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" } ] } }
1.0.0

11 months ago

1.0.0-alpha.1

11 months ago