A library to decompose and compose crypto keys of different types and formats.
Installation
$ npm install crypto-key-composer
This library is written in modern JavaScript and is published in both CommonJS and ES module transpiled variants. If you target older browsers please make sure to transpile accordingly.
Moreover, some of this library's dependencies use the native Node Buffer module. This means that you must compile your app through a bundler that automatically injects a Buffer compatible implementation for the browser, such as Webpack.
The inputKey may be a TypedArray (including Node's Buffer), an ArrayBuffer or a binary string.
⚠️ Do not use the keyAlgorithm.id to identify the key type. The reason is that several identifiers map to the same key type. As an example, rsa-encryption, sha512-with-rsa-encryption, rsa-oaep and rsassa-pss are all RSA keys. Instead, use getKeyTypeFromAlgorithm to properly get the key type.
Meaningful errors with codes are thrown if something went wrong. When options.format is an array, this function will attempt to decompose the key for the specified formats, in order and one by one. It will succeed if the key is using one of the formats or fail if it's using another format, throwing an AggregatedError containing a errors property with the errors indexed by format.
The inputKey may be a TypedArray (including Node's Buffer), an ArrayBuffer or a binary string.
⚠️ Do not use the keyAlgorithm.id to identify the key type. The reason is that several identifiers map to the same key type. As an example, rsa-encryption, rsaes-oaep and rsassa-pss are all RSA keys. Instead, use getKeyTypeFromAlgorithm to properly get the key type.
Available options:
name
type
default
description
format
string/Array
['raw-pem', 'spki-pem']
Limit the parsing to one or more formats
Meaningful errors with codes are thrown if something went wrong. When options.format is an array, this function will attempt to decompose the key for the specified formats, in order and one by one. It will succeed if the key is using one of the formats or fail if it's using another format, throwing an AggregatedError containing a errors property with the errors indexed by format.
composePublicKey(decomposedKey)
Composes a public key from its parts: format, keyAlgorithm and keyData. This function is the inverse of decomposePublicKey.
Below you will find the list of supported formats for private and public keys.
raw-der (public & private)
The raw-der is the DER encoded ASN1 format defined in RFC 8017 for RSA keys and in RFC5915 for EC keys.
Supported public key algorithms:
Just the standard rsa-encryption RSA algorithm (or the rsa alias)
Supported private key algorithms:
Just the standard rsa-encryption RSA algorithm (or the rsa alias)
Just the standard ec-public-key EC algorithm (or the ec alias)
Supported encryption algorithms: none
⚠️ It's recommended to use the newer PKCS8 & SPKI formats for private and public keys respectively because they are able to store more types of keys. Moreover, PKCS8 keys may be encrypted.
raw-pem (public & private)
The raw-pem is the PEM encoded version of raw-der and is defined in RFC 1421.
Supported public key algorithms:
Just the standard rsa-encryption RSA algorithm (or the rsa alias)
Supported private key algorithms:
Just the standard rsa-encryption RSA algorithm (or the rsa alias)
Just the standard ec-public-key RSA algorithm (or the ec alias)
⚠️ It's recommended to use the newer PKCS8 & SPKI formats for private and public keys respectively because they are able to store more types of keys. Moreover, PKCS8 keys have stronger encryption.
pkcs1-der (private)
The pkcs1-der is the DER encoded ASN1 format defined in RFC 8017. It's a subset of the raw-der format, supporting only RSA keys.
Supported private key algorithms:
Just the standard rsa-encryption RSA algorithm (or the rsa alias)
Supported encryption algorithms: none
⚠️ It's recommended to use the newer PKCS8 format for private keys because it's able to store more types of keys and support encryption.
pkcs1-pem (private)
The pkcs1-pem is the PEM encoded version of pkcs1-der and is defined in RFC 1421. It's a subset of the raw-pem format, supporting only RSA keys.
Supported private key algorithms:
Just the standard rsa-encryption RSA algorithm (or the rsa alias)
⚠️ It's recommended to use the newer PKCS8 format for private keys because it's able to store more types of keys and support stronger encryption algorithms.
pkcs8-der (private)
The pkcs8-der is the DER encoded ASN1 format defined in RFC 5208 and RFC 5985.
The spki-der is a format to represent various types of public keys and is defined in RFC 5280.
Supported public key algorithms:
RSA keys
EC keys
ED25519 keys
Supported encryption algorithms: does not apply
spki-pem (public)
The spki-pem is the PEM encoded version of spki-der and is defined in RFC 1421.
Supported public key algorithms:
RSA keys
EC keys
ED25519 keys
Supported encryption algorithms: does not apply
Key Algorithms
Below you will find the list of supported key algorithms. Because the actual supported key algorithms vary from format to format, be sure to also check the Formats section.
RSA keys
The following RSA key algorithms are supported:
rsa-encryption
md2-with-rsa-encryption
md4-with-rsa-encryption
md5-with-rsa-encryption
sha1-with-rsa-encryption
sha224-with-rsa-encryption
sha256-with-rsa-encryption
sha384-with-rsa-encryption
sha512-with-rsa-encryption
sha512-224-with-rsa-encryption
sha512-256-with-rsa-encryption
⚠️ At the moment, rsaes-oaep and rsassa-pss are not yet supported (see issue #4).
All of them are expressed like so:
{ keyAlgorithm: { id: 'rsa-encryption' }}
Because they have no parameters, the example above may also be expressed like so:
{ keyAlgorithm: 'rsa-encryption'}
You may use the rsa alias in the key algorithm id, which maps to rsa-encryption.
EC keys
The following EC (elliptic curve) algorithms are supported:
ec-public-key
ec-dh
ec-mqv
Only named curves may be used. The following curves are supported:
sect163k1
sect163r1
sect239k1
sect113r1
sect113r2
secp112r1
secp112r2
secp160r1
secp160k1
secp256k1
sect163r2
sect283k1
sect283r1
sect131r1
sect131r2
sect193r1
sect193r2
sect233k1
sect233r1
secp128r1
secp128r2
secp160r2
secp192k1
secp224k1
secp224r1
secp384r1
secp521r1
sect409k1
sect409r1
sect571k1
sect571r1
secp192r1
secp256r1
The combination of the key algorithm and the named curve are expressed like so:
The seed is composed of 32 bytes which serves as the basis to derive the 64 bytes private key and the 32 bytes public key. This closely follows what is defined in RFC 8032.
The encryption algorithm only apply for private keys and is composed by two parts: Key Derivation Function and the Encryption Scheme. Below you will find the supported algorithms for these parts. Because the actual supported encryption algorithms vary from format to format, be sure to also check the Formats section.
Key Derivation Function
OpenSSL derive bytes
The openssl-derive-bytes is used when encrypting PKCS#1 PEM keys and was pionereed by OpenSSL to derive a key from the password.
The pbkdf2 is used when encrypting PKCS#8 keys and is part of PKCS#5 defined by RFC 8018.
{ encryptionAlgorithm: { keyDerivationFunc: { id: 'pbkdf2', iterationCount: 10000, // The number of iterations keyLength: 32, // Automatic, based on the `encryptionScheme` prf: 'hmac-with-sha512' // The pseudo-random function } encryptionScheme: ... }}
The parameters above are the default ones and may be omited if you don't need to tweak them. In that case, you may express the example above like so: