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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@speresps/crypto-module

v1.0.0

Published

Reusable Node.js cryptographic module providing AES-256-GCM symmetric encryption and RSA-OAEP asymmetric encryption with SHA-256.

Readme

Crypto Module

A reusable Node.js cryptography module that provides:

  • Symmetric encryption: AES-256-GCM
  • Asymmetric encryption: RSA-OAEP with SHA-256
  • Key loading and validation: KeyManager loads the consuming project’s keys from its keys/ directory.

Requirements

  • Node.js 20 or later
  • OpenSSL, for generating and verifying the key files
  • A project that runs trusted server-side code

Installation

npm install @speresps/crypto-module

Generate the keys

Run these commands from your project’s root directory—the directory where you run your application.

1. Create the keys directory

mkdir -p keys

The directory must contain these three files:

your-project/
└── keys/
    ├── private.pem
    ├── public.pem
    └── secretKey

2. Generate the RSA private and public keys

Generate a 2048-bit RSA private key:

openssl genpkey -algorithm RSA \
  -pkeyopt rsa_keygen_bits:2048 \
  -out keys/private.pem

Derive the matching public key from the private key:

openssl pkey \
  -in keys/private.pem \
  -pubout \
  -out keys/public.pem

3. Generate the AES-256 secret key

Generate 32 random bytes and save them as Base64:

openssl rand -base64 32 > keys/secretKey

The module expects secretKey to contain a Base64-encoded 32-byte key. Do not replace it with a password or a key of a different length.

Verify the keys

Run these checks from the project root.

Verify the RSA private key

openssl pkey -in keys/private.pem -check -noout

OpenSSL should report that the key is valid.

Verify that the RSA public and private keys match

diff \
  <(openssl pkey -in keys/private.pem -pubout -outform DER) \
  <(openssl pkey -pubin -in keys/public.pem -outform DER)

If the command produces no output and exits successfully, the public key matches the private key. If diff reports differences, the keys do not match.

Verify the AES secret key

This Node.js command checks that the file contains valid Base64 representing exactly 32 bytes:

node --input-type=module -e '
import { readFileSync } from "node:fs";

const key = readFileSync("keys/secretKey", "utf8").trim();
const decoded = Buffer.from(key, "base64");

if (decoded.toString("base64") !== key || decoded.length !== 32) {
  throw new Error("Invalid AES-256 secret key");
}

console.log("AES-256 secret key is valid");
'

Usage

The module exports symmetric and asymmetric instances. KeyManager is used internally to load the key files from the consuming project’s keys/ directory.

import { symmetric, asymmetric } from "@speresps/crypto-module";

const data = {
  userId: 123,
  username: "test-user",
};

// AES-256-GCM
const encrypted = symmetric.encrypt(data);
const decrypted = symmetric.decrypt(encrypted);

// RSA-OAEP with SHA-256
const rsaEncrypted = asymmetric.encrypt(data);
const rsaDecrypted = asymmetric.decrypt(rsaEncrypted);

console.log(decrypted);
console.log(rsaDecrypted);

Key security

  • Never commit or publish keys/private.pem or keys/secretKey. Add the keys directory to your project’s .gitignore:

    /keys/
  • Keep private and secret keys on the server. Do not import this module into browser code or expose key contents to clients.

  • Back up keys securely. Data encrypted with a key generally cannot be decrypted if that key is lost.

  • Use the same key files to decrypt data that was encrypted with them. Replacing keys means previously encrypted data may no longer be decryptable.

  • RSA-OAEP is intended for relatively small inputs. For larger data, use symmetric encryption or a hybrid design that encrypts the data with AES and protects the AES key with RSA.

Notes

  • Each AES encryption uses a fresh random 12-byte initialization vector (IV). The encrypted payload includes the IV and authentication tag required for decryption.
  • RSA encryption uses OAEP padding with SHA-256.
  • The module requires the keys/ directory and all three key files to exist in the consuming project when the exported instances are initialized.

License

MIT