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

opticore-asymmetric-cryption

v1.0.1

Published

opticore asymmetric cryption module using RSA keys

Readme

opticore-asymmetric-cryption

RSA asymmetric encryption/decryption module for the OpticoreJS framework. Provides sign, verify, encrypt, and decrypt operations using RSA key pairs, with an interactive CLI to generate your keys.

Table of contents


Installation

npm install opticore-asymmetric-cryption

After installation, a welcome message appears automatically with the command to run to generate your RSA keys.


Generate RSA keys

The package ships an interactive CLI that guides you through generating a key pair:

npx opticore-gen-keys

Or, if npx is not available:

node node_modules/opticore-asymmetric-cryption/scripts/generate-rsa-keys.mjs

The script lets you:

  • Choose the generation method: Node.js crypto (built-in, no dependency) or OpenSSL (system tool)
  • Choose the key size: 2048 bits (recommended) or 4096 bits (maximum security)
  • Choose the output directory (default: keys/)
  • Automatically creates a .gitignore in the output folder to protect private.pem

The package does not store your keys. They are generated in the directory you choose and are your responsibility to secure.

Generated files

| File | Description | |---|---| | keys/private.pem | Private key — keep it secret | | keys/public.pem | Public key — can be shared | | keys/.gitignore | Prevents private.pem from being committed |


Quick start

import fs from "fs";
import {
  loaderTranslationFile,
  SAsymmetricCryptionDataWithPublicRSAKey,
  SAsymmetricCryptionDataWithPrivateRSAKey,
} from "opticore-asymmetric-cryption";

// 1. Load translations (call once at application startup)
loaderTranslationFile("en"); // "en" or "fr"

// 2. Read your RSA keys
const privateKey = fs.readFileSync("keys/private.pem", "utf8");
const publicKey  = fs.readFileSync("keys/public.pem",  "utf8");

// 3. Encrypt with the public key, decrypt with the private key
const servicePublic = new SAsymmetricCryptionDataWithPublicRSAKey("en", ".env");
const result = servicePublic.verifyPublicRSAKey(privateKey, publicKey, "my secret payload");
console.log(result); // "my secret payload"

// 4. Encrypt with the private key, decrypt with the public key
const servicePrivate = new SAsymmetricCryptionDataWithPrivateRSAKey("en", ".env");
const result2 = servicePrivate.verifyRSAKey(privateKey, publicKey, "my secret payload");
console.log(result2); // "my secret payload"

API reference

loaderTranslationFile

Loads the package translation files for the given locale. Must be called once before using any service.

import { loaderTranslationFile } from "opticore-asymmetric-cryption";

loaderTranslationFile("en"); // English
loaderTranslationFile("fr"); // French

The locale can also be set via the OPTICORE_LOCALE environment variable.


SAsymmetricCryptionDataWithPublicRSAKey

Handles the public-key-first flow: signs and verifies with the public key, encrypts with the public key, and decrypts with the private key.

Constructor

new SAsymmetricCryptionDataWithPublicRSAKey(localeLanguage: string, environmentPath: string)

| Parameter | Type | Description | |---|---|---| | localeLanguage | string | Locale for error messages ("en" or "fr") | | environmentPath | string | Path to your .env file (e.g. ".env") |

verifyPublicRSAKey(privateKey, publicKey, payload)

Signs the payload with the public key (SHA-256), verifies the signature, then encrypts with the public key and decrypts with the private key.

verifyPublicRSAKey(
  privateKey: string,
  publicKey:  string,
  payload:    any
): string | StackTraceError
const service = new SAsymmetricCryptionDataWithPublicRSAKey("en", ".env");
const decrypted = service.verifyPublicRSAKey(privateKey, publicKey, "payload");
// returns "payload" if signature is verified, StackTraceError otherwise

SAsymmetricCryptionDataWithPrivateRSAKey

Handles the private-key-first flow: signs with the private key, verifies with the public key, encrypts with the private key, and decrypts with the public key.

Constructor

new SAsymmetricCryptionDataWithPrivateRSAKey(localeLanguage: string, environmentPath: string)

| Parameter | Type | Description | |---|---|---| | localeLanguage | string | Locale for error messages ("en" or "fr") | | environmentPath | string | Path to your .env file (e.g. ".env") |

verifyRSAKey(privateKey, publicKey, payload)

Signs the payload with the private key (SHA-256), verifies the signature with the public key, then encrypts with the private key and decrypts with the public key.

verifyRSAKey(
  privateKey: string,
  publicKey:  string,
  payload:    any
): string | Error
const service = new SAsymmetricCryptionDataWithPrivateRSAKey("en", ".env");
const decrypted = service.verifyRSAKey(privateKey, publicKey, "payload");
// returns "payload" if signature is verified, Error otherwise

RSAKeyEncryption

Low-level static class for raw RSA encryption. Use the service classes above for a higher-level interface.

import { RSAKeyEncryption } from "opticore-asymmetric-cryption";

// Encrypt with private key
const encrypted = RSAKeyEncryption.privateEncrypt(privateKey, Buffer.from("data"));

// Encrypt with public key
const encrypted = RSAKeyEncryption.publicEncrypt(publicKey, Buffer.from("data"));

| Method | Parameters | Returns | |---|---|---| | privateEncrypt(key, data) | key: string, data: Buffer | Buffer | | publicEncrypt(key, data) | key: string, data: Buffer | Buffer |


RSAKeyDecryption

Low-level static class for raw RSA decryption.

import { RSAKeyDecryption } from "opticore-asymmetric-cryption";

// Decrypt with private key
const decrypted = RSAKeyDecryption.privateDecrypt(privateKey, encryptedBuffer);

// Decrypt with public key
const decrypted = RSAKeyDecryption.publicDecrypt(publicKey, encryptedBuffer);

| Method | Parameters | Returns | |---|---|---| | privateDecrypt(key, data) | key: string, data: Buffer | Buffer | | publicDecrypt(key, data) | key: string, data: Buffer | Buffer |


Constants

All constants are re-exported from CSignRSAKeyComponent or individually.

CAlgorithm

Supported signing algorithms.

import { CAlgorithm } from "opticore-asymmetric-cryption";

CAlgorithm.sha256          // "SHA256"
CAlgorithm.RSA_SHA256      // "RSA-SHA256"
CAlgorithm.RSA_SHA512      // "RSA-SHA512"
CAlgorithm.sha512          // "sha512"
// ... and many more (RSA-MD5, RSA-SHA384, SHA3 variants, BLAKE2, etc.)

CKeyType

import { CKeyType } from "opticore-asymmetric-cryption";

CKeyType.private  // "Private"
CKeyType.public   // "Public"

COutputFormat

import { COutputFormat } from "opticore-asymmetric-cryption";

COutputFormat.base64     // "base64"
COutputFormat.base64url  // "base64url"
COutputFormat.hex        // "hex"
COutputFormat.binary     // "binary"

CEncodingFormat

import { CEncodingFormat } from "opticore-asymmetric-cryption";

CEncodingFormat.utf8     // "utf8"
CEncodingFormat.utf_8    // "utf-8"
CEncodingFormat.ascii    // "ascii"
CEncodingFormat.base64   // "base64"
CEncodingFormat.hex      // "hex"
// ... and more (utf16le, ucs2, latin1, binary, etc.)

CSignRSAKeyComponent

Aggregates all four constants under a single object:

import { CSignRSAKeyComponent } from "opticore-asymmetric-cryption";

CSignRSAKeyComponent.algorithm      // → CAlgorithm
CSignRSAKeyComponent.keyType        // → CKeyType
CSignRSAKeyComponent.outputFormat   // → COutputFormat
CSignRSAKeyComponent.encodingFormat // → CEncodingFormat

Security

  • NEVER commit private.pem to your Git repository. The key generation script creates a .gitignore automatically.
  • In production, store the private key in an environment variable or a secrets manager (AWS Secrets Manager, HashiCorp Vault, Doppler, etc.).
  • Use 4096-bit keys for maximum security, or 2048-bit for a balance between security and performance.
  • The private key must remain confidential at all times. The public key can be distributed freely.

License

MIT — Guy-Serge Kouacou