Generate RSA keys in PKCS#1, PKCS#8 or BER format.
Long-form PKCS#1 field names are used for parameter names, not their (rather terse) RSA counterparts.
rsaPrivateKey(options)
options
{Object}modulus
{BigInt|Buffer|Uint8Array} RSAn
parameter.publicExponent
{BigInt|Buffer|Uint8Array} RSAe
parameter.privateExponent
{BigInt|Buffer|Uint8Array} RSAd
parameter.prime1
{BigInt|Buffer|Uint8Array} RSAp
parameter.prime2
{BigInt|Buffer|Uint8Array} RSAq
parameter.exponent1
{BigInt|Buffer|Uint8Array} RSAdp
parameter,d % (p - 1)
.exponent2
{BigInt|Buffer|Uint8Array} RSAdq
parameter,d % (q - 1)
.coefficient
{BigInt|Buffer|Uint8Array} RSAinverseQ
parameter,q * inverseQ === 1 % p
.
- Returns: {Array} BER-encoded RSA private key.
Keep prime1
, prime2
and privateExponent
secure at all times; the security
of the private key depends on it.
Example:
const rsaPrivateKey = const modulus = 0xBAD5ECn // insecure; example onlyconst publicExponent = 0x2nconst privateExponent = 0xBAD5ECnconst prime1 = 0xBAD5ECnconst prime2 = 0xBAD5ECnconst exponent1 = 0xBAD5ECnconst exponent2 = 0xBAD5ECnconst coefficient = 0xBAD5ECnconst options = modulus publicExponent privateExponent prime1 prime2 exponent1 exponent2 coefficientconst key = console// <Buffer 30 30 02 01 00 02 04 00 ba d5 ec 02 01 02 02 04 00 ba d5 ec 02 04 00// ba d5 ec 02 04 00 ba d5 ec 02 04 00 ba d5 ec 02 04 00 ba d5 ec 02 04 00 ba d5// ec>
rsaPrivateKey.pkcs1(options)
Like rsaPrivateKey()
but the result is returned as a PKCS#1-encoded string.
Example:
const rsaPrivateKey = const modulus = 0xBAD5ECn // insecure; example onlyconst publicExponent = 0x2nconst privateExponent = 0xBAD5ECnconst prime1 = 0xBAD5ECnconst prime2 = 0xBAD5ECnconst exponent1 = 0xBAD5ECnconst exponent2 = 0xBAD5ECnconst coefficient = 0xBAD5ECnconst options = modulus publicExponent privateExponent prime1 prime2 exponent1 exponent2 coefficientconst key = rsaPrivateKeyconsole// -----BEGIN RSA PRIVATE KEY-----// MDACAQACBAC61ewCAQICBAC61ewCBAC61ewCBAC61ewCBAC61ewCBAC61ewCBAC6// 1ew=// -----END RSA PRIVATE KEY-----
rsaPrivateKey.pkcs8(options)
Like rsaPrivateKey()
but the result is returned as a PKCS#8-encoded string.
Example:
const rsaPrivateKey = const modulus = 0xBAD5ECn // insecure; example onlyconst publicExponent = 0x2nconst privateExponent = 0xBAD5ECnconst prime1 = 0xBAD5ECnconst prime2 = 0xBAD5ECnconst exponent1 = 0xBAD5ECnconst exponent2 = 0xBAD5ECnconst coefficient = 0xBAD5ECnconst options = modulus publicExponent privateExponent prime1 prime2 exponent1 exponent2 coefficientconst key = rsaPrivateKeyconsole// -----BEGIN PRIVATE KEY-----// MEYCAQAwDQYJKoZIhvcNAQEBBQAEMjAwAgEAAgQAutXsAgECAgQAutXsAgQAutXs// AgQAutXsAgQAutXsAgQAutXsAgQAutXs// -----END PRIVATE KEY-----
rsaPublicKey(options)
options
{Object}modulus
{BigInt|Buffer|Uint8Array} RSAn
parameter.publicExponent
{BigInt|Buffer|Uint8Array} RSAe
parameter.
- Returns: {Array} BER-encoded RSA public key.
Example:
const rsaPublicKey = const modulus = 0xBAD5ECn // insecure; example onlyconst publicExponent = 0x2nconst key = console// <Buffer 30 09 02 04 00 b4 d5 ec 02 01 02>
rsaPublicKey.pkcs1(options)
Like rsaPublicKey()
but the result is returned as a PKCS#1-encoded string.
Example:
const rsaPublicKey = const modulus = 0xBAD5ECn // insecure; example onlyconst publicExponent = 0x2nconst key = rsaPublicKeyconsole// -----BEGIN RSA PUBLIC KEY-----// MAkCBAC01ewCAQI=// -----END RSA PUBLIC KEY-----
rsaPublicKey.pkcs8(options)
Like rsaPublicKey()
but the result is returned as a PKCS#8-encoded string.
Example:
const rsaPublicKey = const modulus = 0xBAD5ECn // insecure; example onlyconst publicExponent = 0x2nconst key = rsaPublicKeyconsole// -----BEGIN PUBLIC KEY-----// MB0wDQYJKoZIhvcNAQEBBQADDAAwCQIEALTV7AIBAg==// -----END PUBLIC KEY-----
changelog
v0.0.3 05/01/2020
- add examples to README
v0.0.2 04/01/2020
- implement missing
rsaPrivateKey.pkcs8()
v0.0.1 04/01/2020
- initial release