@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.
Maintainers
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:
KeyManagerloads the consuming project’s keys from itskeys/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-moduleGenerate 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 keysThe directory must contain these three files:
your-project/
└── keys/
├── private.pem
├── public.pem
└── secretKey2. 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.pemDerive the matching public key from the private key:
openssl pkey \
-in keys/private.pem \
-pubout \
-out keys/public.pem3. Generate the AES-256 secret key
Generate 32 random bytes and save them as Base64:
openssl rand -base64 32 > keys/secretKeyThe 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 -nooutOpenSSL 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.pemorkeys/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
