rsa-compat

raw JSON →
2.0.8 verified Fri May 01 auth: no javascript

RSA utility library for Node.js that works on Windows, Mac, and Linux with or without a C compiler. Current version is 2.0.8, released under a permissive license. It wraps ursa (when available) or node-forge for key generation, import, export, signing, and CSR creation. Key differentiators include cross-platform support without native compilation, JWK export/import, and integration with ACME.js and Greenlock.js for Let's Encrypt certificate management.

error Error: RSA.generateKeypair is not a function
cause Incorrect import: using default import instead of named import.
fix
Use import { RSA } from 'rsa-compat' instead of import RSA from 'rsa-compat'.
error Cannot find module 'ursa-optional'
cause Optional dependency not installed or not found.
fix
Install ursa-optional: npm install ursa-optional or set NODE_OPTIONS to skip.
breaking RSA.generateKeypair(bitlen, exp, options, cb) is deprecated; use RSA.generateKeypair(options, cb) with options object.
fix Pass options object: RSA.generateKeypair({ bitlen: 2048, exp: 65537, ... }, cb).
deprecated RSA.import(keypair, options) signature changed; now RSA.import(options).
fix Use RSA.import({ keypair: keypair, ...options }).
deprecated RSA.signJws(keypair, payload, nonce) replaced with RSA.signJws(keypair, header, protect, payload).
fix Provide header and protect arguments: RSA.signJws(keypair, header, protect, payload).
gotcha ursa-optional is an optional dependency but may be required on older Node versions; without it, node-forge is used.
fix If you need ursa, install it explicitly: npm install ursa-optional.
npm install rsa-compat
yarn add rsa-compat
pnpm add rsa-compat

Generates an RSA keypair with PEM and JWK exports using async callback.

import { RSA } from 'rsa-compat';

const options = { bitlen: 2048, exp: 65537, public: true, pem: true };
RSA.generateKeypair(options, (err, keypair) => {
  if (err) throw err;
  console.log('Private Key PEM:', keypair.privateKeyPem);
  console.log('Public Key PEM:', keypair.publicKeyPem);
  console.log('Private Key JWK:', keypair.privateKeyJwk);
  console.log('Public Key JWK:', keypair.publicKeyJwk);
});