rsa-compat.js
JavaScript RSA utils that work on Windows, Mac, and Linux with or without C compiler
In order to provide a module that "just works" everywhere, we mix and match methods
from node.js
core, ursa
, forge
, and others.
(in the future we'd like to provide the same API to the browser)
Examples
Generate an RSA Keypair:
var PromiseA = require('bluebird');
var RSA = PromiseA.promisify(require('rsa-compat').RSA);
var bitlen = 1024;
var exp = 65537;
var options = { public: true, pem: true, internal: true };
RSA.generateKeypair(bitlen, exp, options).then(function (keypair) {
console.log(keypair);
});
console.log(keypair)
:
{ publicKeyPem: '-----BEGIN RSA PUBLIC KEY-----\n/*base64 pem-encoded string*/'
, privateKeyPem: '-----BEGIN RSA PRIVATE KEY-----\n/*base64 pem-encoded string*/'
, privateKeyJwk: {
kty: "RSA"
, n: '/*base64 modulus n = pq*/'
, e: '/*base64 exponent (usually 65537)*/'
, d: '/*base64 private exponent (d = e^−1 (mod ϕ(n))/'
, p: '/*base64 first prime*/'
, q:
, dp:
, dq:
}
, publicKeyJwk: {
kty: "RSA"
, n:
, e: /base64 exponent (usually 65537)*/
}
, _ursa:
, _ursaPublic:
, _forge:
, _forgePublic:
}
API
RSA.generateKeypair(bitlen, exp, options, cb)
RSA.exportPrivatePem(keypair)
RSA.exportPublicPem(keypair)
RSA.exportPrivateJwk(keypair)
RSA.exportPublicJwk(keypair)
keypair
can be any object with any of these keys publicKeyPem, privateKeyPem, publicKeyJwk, privateKeyJwk
RSA.generateKeypair(bitlen, exp, options, cb)
Create a private keypair and export it as PEM, JWK, and/or internal formats
RSA.generateKeypair(null, null, null, function (keypair) { });
RSA.generateKeypair(1024, 65537, { pem: false, public: false, internal: false }, function (keypair) { });
bitlen
: 1024 (default), 2048, or 4096
exp
: 65537 (default)
options
:
{ public: false
, pem: false
, jwk: true
, internal: false
, thumbprint: false
, fingerprint: false
}