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

aes256gcm-bundle

v2.1.0

Published

AES256-GCM encrypt/decrypt text to/from a base64url-encoded bundle with the format: keyId.iv.authTag.cipherText

Downloads

13

Readme

aes256gcm-bundle

A simple way to encrypt/decrypt fields to/from a string bundle, using AES256-GCM with a randomly generated IV.


Installation


npm i aes256gcm-bundle

Encrypt / Decrypt


import CipherBundle from 'aes256gcm-bundle'


const cb = new CipherBundle(base64urlEncodedKey)

const bundle = cb.encrypt('Hello world')

// e.g.: "0.YFG9joOntVvgLLYQ.QL7k4BFI8ot8PJeqTXOyXQ.fQGNGGwvoKEQXg0"

const plainText = cb.decrypt(bundle)

// e.g.: "Hello world"


Creating a CipherBundle

use a randomly generated base64url-encoded key (very secure)


import crypto from 'node:crypto'
const key = crypto.randomBytes(32).toString('base64url')  
// example key: Bi-zMjDdm8ZuqbJQ5WstSwFUCLwVwsPcBQvah-pprSY

let cb = new CipherBundle(key)

or provide a key in another encoding (utf8, base64, hex, ...)


cb = new CipherBundle(veryStrongPassword, 'utf8')

Bundle format

Text passed to encrypt() creates a bundle in this dot-separated format: keyName.iv.authTag.cipherText.

Example bundle:

prod1.2N6zOPNuH-3C8vFY.ZGhXywwmEgf14YR2shaiqQ.TIK7rtKvcqrEjG4

Bundle components

  • The keyName is just a moniker that is set to 0 by default, but you can override it to denote (to you) which key was used to encrypt the bundle. It can be set to a meaningful value (e.g. prod0) and should be changed when rotating keys.

  • The iv and authTag components are opaque base64url-encoded values that are needed to successfully decrypt the bundle.

  • The cipherText component of the bundle is the AES256-GCM encryption of the plain text (with a random IV), encoded to a base64url string. The key used for encryption is the SHA-256 of the key that you provide when creating a CipherBundle (described below).


Key rotation / identification

To support key rotation/identification, you can pass an optional keyName parameter to encrypt(text, keyName). The default prefix is 0 when keyName is not specified. The keyName parameter changes the prefix on your bundle to a tag that allows you to later identify which key was used to encrypt the bundle, under your own naming conventions. The name should be short but meaningful (example prod1), cannot include the . character, and should be changed alongside a key rotation (e.g. to prod2).

It is up to you to determine an appropriate key rotation schedule - after a billion encryptions with the same key, it is probably time to choose a new key! Since the IV is randomly generated, there is a very small chance of a duplicate IV after many encryptions - this is significant known risk for AES256-GCM, but it can be mitigated by periodic key rotations.

Here is an example of setting a custom prefix for the bundle to identify the use of a new key:


cb = new CipherBundle(prod1Key)

const bundle = cb.encrypt('Hello world', "prod1")

// example bundle: prod1.VqUyV8dr33MRITD5.7LiH24gOXaoGHw9sfJMNKA.r00A78iuZWNd748

const plainText = cb.decrypt(bundle)

// example plainText: Hello world