crypto-keys
Node.js javascript tool to generate ,encrypt and decrypt RSA and EC keys formated as PEM / DER.
Requires: module:NPM:asn1.js, module:NPM:des.js, module:NPM:elliptic, module:NPM:lodash.clonedeep, module:./jsbn.js:jsbn
Author: Ademar Arvati
License: MIT
Example
const cryptoKeys = require('crypto-keys')
- crypto-keys
- Key ⏏
- new Key(format, key)
- .keyType ⇒
string - .jwkThumbprint ⇒
- .export([format], [options]) ⇒
string|Uint8Array|jwk - .encrypt(passphrase) ⇒
Class - .decrypt(passphrase) ⇒
Class - .getJwkThumbprint([alg], [output]) ⇒
Uint8Array|string
- Key ⏏
Key ⏏
new Key(format, key)
Import or Create a Key.
| Param | Type | Default | Description |
|---|---|---|---|
| format | string |
Format of key to import (‘der’, ‘pem’ or ‘jwk’) or ‘create’ to create a new private key | |
| key | string | Uint8Array | jwk | Object |
String for pem key, Uint8Array for der key, {jwk} for jwk key or to create new key. | |
| [key.type] | string |
"ec" |
‘rsa’ or ‘ec’ for key type to be created |
| [key.namedCurve] | string |
"P-256K" |
Curve for EC type key creation ‘P-256’, ‘P-384’, ‘P-521’, ‘P-256K’ |
| [key.modulusLength] | integer |
2048 |
Modulus Length for RSA type key creation |
| [key.publicExponent] | integer |
65537 |
Public Exponent for RSA type key creation |
Example
Creating a new private key
privateKey = new cryptoKeys('create', {type:'rsa', modulusLength:2048, publicExponent:65537});
Importing a pem public key (string)
key = new cryptoKeys('pem', publicKey);
key.keyType ⇒ string
Get Jwk key type of decrypted keys
Kind: instance property of Key
Returns: string - - key type ‘EC’ or ‘RSA’
key.jwkThumbprint ⇒
Get Jwk Thumbprint of the decrypted key with default parameters alg=’SHA-256’, output=’binary’
Kind: instance property of Key
Returns: Jwk Thumbprint of the key
key.export([format], [options]) ⇒ string | Uint8Array | jwk
Export Key as format.
Kind: instance method of Key
Returns: string | Uint8Array | jwk - - Key in ‘der’, ‘pem’ or ‘jwk’ format
| Param | Type | Default | Description |
|---|---|---|---|
| [format] | string |
"jwk" |
Format of key to export (‘der’, ‘pem’ or ‘jwk’) |
| [options] | Object |
{} |
Options to export key into format only with decrypted keys |
| [options.encryptParams] | Object |
{} |
Options to export encrypted prvate key for ‘pem’ and ‘der’ formats |
| [options.encryptParams.passphrase] | string |
Passphrase to encrypt private key | |
| [options.encryptParams.algorithm] | string |
"pbes2" |
if ‘pbes2’ only pbkdf2 and salt length of 8 is available, choose from ‘pbeWithMD5AndDES-CBC’, ‘pbeWithSHA1AndDES-CBC’, ‘pbes2’ |
| [options.encryptParams.iterationCount] | integer |
2048 |
Iteration count to use for salt algorithm |
| [options.encryptParams.cipher] | string |
"aes256-cbc" |
Cipher when algorithm is ‘pbes2’, choose from ‘des-ede3-cbc’, ‘aes128-cbc’, ‘aes192-cbc’, ‘aes256-cbc’ |
| [options.encryptParams.prf] | string |
"hmacWithSHA256" |
Prf when algorithm is ‘pbes2’, choose from ‘hmacWithSHA1’, ‘hmacWithSHA256’, ‘hmacWithSHA384’, ‘hmacWithSHA512’ |
| [options.outputPublic] | boolean |
True to Export public key from private Key or undefined/False to maintain actual format | |
| [options.compact] | boolean |
false |
Export compact key for ‘EC’ type keys |
key.encrypt(passphrase) ⇒ Class
Encrypt Private Key using default parameters.
Kind: instance method of Key
Chainable
Returns: Class - this - Key for channing
| Param | Type | Description |
|---|---|---|
| passphrase | string |
Passphrase to encrypt private key |
key.decrypt(passphrase) ⇒ Class
Decrypt Private Key using default parameters.
Kind: instance method of Key
Chainable
Returns: Class - this - Key for channing
| Param | Type | Description |
|---|---|---|
| passphrase | string |
Passphrase to decrypt private key |
key.getJwkThumbprint([alg], [output]) ⇒ Uint8Array | string
Get Jwk Thumbprint of decrypted keys
Kind: instance method of Key
Returns: Uint8Array | string - - Jwk Thumbprint of the key in format
| Param | Type | Default | Description |
|---|---|---|---|
| [alg] | string |
"SHA-256" |
Hash algorithm, choose from ‘SHA-256’,’SHA-384’,’SHA-512’ and ‘SHA-1, ‘MD5’ that SHOULD NOT USE |
| [output] | string |
"binary" |
Output Format ‘binary’, ‘hex’, ‘base64’ |