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

@nibyou/crypto

v1.1.7

Published

Nibyou crypto packages, wraps WebCrypto and specifically SubtleCrypto for AES and RSA encryption

Downloads

9

Readme

@nibyou/crypto

Nibyou's crypto package is a wrapper around the WebCrypto and specifically the SubtleCrypto APIs. This package proides easily reusable functions around the cryptography used by Nibyou.

For client support, see caniuse.

Zero dependencies, 100% test coverage!

Usage

Install the package using npm or yarn.

Import the crypto functions:

import * as ncrypto from '@nibyou/crypto';

Asymmetric encryption

This library uses RSA-OAEP with SHA-256 and 4096 bit keylength in accordance with the current recommendations by the BSI.

Keys are exchanged headless, so the differenciation between public and private keys has to be made in the business logic.

Key handling

Generate a new RSA Key Pair:

const keyPairAlice: CryptoKeyPair = await ncrypto.generateKeyPair();

generateKeyPair() accepts an optional parameter extractable: boolean (set to true by default) that controls whether or not the keys can be exported using below functions.

Export the Public and Private key into Base64 (SPKI and PKCS #8 respectively):

const spki: string = await ncrypto.exportPublicKey(keyPairAlice.publicKey);
const pkcs8: string = await ncrypto.exportPrivateKey(keyPairAlice.privateKey);

Import Public and Private keys into CryptoKey objects again:

const publicKey: CryptoKey = await ncrypto.importPublicKey(spki);
const privateKey: CryptoKey = await ncrypto.importPrivateKey(pkcs8);

These functions accept an optional parameter extractable: boolean (set to false by default) that controls whether or not the keys can be exported again.

Encryption

To encrypt a message you need another public key, lets call this one keyPairBob.

const keyPairBob: CryptoKeyPair = await ncrypto.generateKeyPair();

A message can either be a string or an object, during encryption, objects will be converted into JSON strings.

const message = 'Guten Tag, Bob!';

The encryption function encryptRSA expects the data (in this case message) and a public key (in this case keyPairBob.publicKey).

const cipherText = await ncrypto.encryptRSA(message, keyPairBob.publicKey);

The resulting value will be an RSAEncryptionResult object containing the cipher text in its data property.

console.log(cipherText.data); // => 'aenc:AKV9xVMHFSCa2yIwOjCwvpP1UkSbLaqqMu49Tg2CWDXk9xsi5MiArQwmJrrfdKqyfJ3Fag7/9AS+TX4RnHqIxKX26WKgH7EKwdnRTjB9X/PbKOOfNLmq4T/K2CSr+y9n1iJbIOyQpvfnCOFIaIwmQ8CKVTKyrcGcOF8GBdJpujlqSENaD3Q16B4yW4G5M6kSnImnRebHtqhayRk5o84Omj6l4wXGyhqoT/yxD7wlet1nSuZTqU2U3JTfOvoFjYferHPTnEpo38uUWq09fbOoEI3vNBn/UiPN7MoA7uWufNOECvtDxuJty6frbTwvmzj9ZHfwdhy55x21MLXzctJUbw=='

All data encrypted with encryptRSA starts with the aenc prefix.

Decryption

To decrypt the previous message, you need Bob's private key.

Similar to the encryption function, decryptRSA expects the data (in this case cipherText.data) and the corresponding private key (in this case keyPairBob.privateKey).

const secretMessage = await ncrypto.decryptRSA(cipherText.data, keyPairBob.privateKey);
console.log(secretMessage); // => 'Guten Tag, Bob!';

Symmetric encryption

@nibyou/crypto supports AES-GCM-256 with random, 96 bit initialization vectors.

Key handling

Generate a new AES key:

const key: CryptoKey = await ncrypto.generateKey();

generateKey() accepts an optional parameter extractable: boolean (set to true by default) that controls whether or not the key can be exported using below function.

Export the key into raw Base64 format:

const raw: string = await ncrypto.exportKey(key);

Import raw format keys into CryptoKeys

const reImport: CryptoKey = await ncrypto.importKey(raw);

Encryption

You can use the generated key to encrypt data the same way it's done with RSA:

const encrypted = await ncrypto.encryptAES("Hallo, wie geht's denn so?", key);
console.log(encrypted.data); // => 'senc:26AsDbeclttMdQUHTXumWrTrg3ZEb+en0qU=.pUE96B4D8mhYetFP'

Notice the format: the data is prefixed with senc, and contains the ciphertext and initialization vector, joined with a dot.

Decryption

The same key can then decrypt the encrypted message:

const decrypted: string = await ncrypto.decryptAES(encrypted, key);
console.log(decrypted); // => 'Hallo, wie geht's denn so?'

Key derivation

Lastly, this package also supports deriving PBKDF2 keys from strings with 64 bit salts:

const randomUserInput = 'ThisIsASuperSecurePassword';
const derivedKey: CryptoKey = await ncrypto.deriveKey(randomUserInput);

The function supports passing iterations: number = 10000 and salt: string as optional second and third parameter. This allows for deterministic key generation (e.g. encrypting a User's private key with their password as input and email as salt).

The key can then be used in other AES operations.

Testing

To test this library, you can run the tests with npm test or yarn test.

Test Coverage

| Statements | Branches | Functions | Lines | | --------------------------- | ----------------------- | ------------------------- | ----------------- | | Statements | Branches | Functions | Lines |

License

This library is licensed under the GPL Version 3 license. See LICENSE.