2012-02-27 20:08:02 +01:00
|
|
|
# Crypto
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
<!--introduced_in=v0.3.6-->
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `crypto` module provides cryptographic functionality that includes a set of
|
2017-11-29 03:53:24 +01:00
|
|
|
wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
Use `require('crypto')` to access this module.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
const secret = 'abcdefg';
|
|
|
|
const hash = crypto.createHmac('sha256', secret)
|
|
|
|
.update('I love cupcakes')
|
|
|
|
.digest('hex');
|
|
|
|
console.log(hash);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-09 00:31:31 +01:00
|
|
|
## Determining if crypto support is unavailable
|
|
|
|
|
|
|
|
It is possible for Node.js to be built without including support for the
|
|
|
|
`crypto` module. In such cases, calling `require('crypto')` will result in an
|
|
|
|
error being thrown.
|
|
|
|
|
|
|
|
```js
|
2017-01-20 02:42:50 +01:00
|
|
|
let crypto;
|
2016-03-09 00:31:31 +01:00
|
|
|
try {
|
|
|
|
crypto = require('crypto');
|
|
|
|
} catch (err) {
|
|
|
|
console.log('crypto support is disabled!');
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `Certificate`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.8
|
|
|
|
-->
|
2012-10-13 01:26:14 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
SPKAC is a Certificate Signing Request mechanism originally implemented by
|
2018-02-11 20:55:16 +01:00
|
|
|
Netscape and was specified formally as part of [HTML5's `keygen` element][].
|
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
`<keygen>` is deprecated since [HTML 5.2][] and new projects
|
2018-02-11 20:55:16 +01:00
|
|
|
should not use this element anymore.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
The `crypto` module provides the `Certificate` class for working with SPKAC
|
|
|
|
data. The most common usage is handling output generated by the HTML5
|
|
|
|
`<keygen>` element. Node.js uses [OpenSSL's SPKAC implementation][] internally.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `Certificate.exportChallenge(spkac)`
|
2017-09-06 17:10:34 +02:00
|
|
|
<!-- YAML
|
2017-09-01 18:50:47 +02:00
|
|
|
added: v9.0.0
|
2017-09-06 17:10:34 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `spkac` {string | Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Buffer} The challenge component of the `spkac` data structure, which
|
2018-04-06 17:28:20 +02:00
|
|
|
includes a public key and a challenge.
|
2017-09-06 17:10:34 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { Certificate } = require('crypto');
|
|
|
|
const spkac = getSpkacSomehow();
|
|
|
|
const challenge = Certificate.exportChallenge(spkac);
|
|
|
|
console.log(challenge.toString('utf8'));
|
|
|
|
// Prints: the challenge as a UTF8 string
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `Certificate.exportPublicKey(spkac[, encoding])`
|
2017-09-06 17:10:34 +02:00
|
|
|
<!-- YAML
|
2017-09-01 18:50:47 +02:00
|
|
|
added: v9.0.0
|
2017-09-06 17:10:34 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `spkac` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the `spkac` string.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer} The public key component of the `spkac` data structure,
|
2018-04-06 17:28:20 +02:00
|
|
|
which includes a public key and a challenge.
|
2017-09-06 17:10:34 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { Certificate } = require('crypto');
|
|
|
|
const spkac = getSpkacSomehow();
|
|
|
|
const publicKey = Certificate.exportPublicKey(spkac);
|
|
|
|
console.log(publicKey);
|
|
|
|
// Prints: the public key as <Buffer ...>
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `Certificate.verifySpkac(spkac)`
|
2017-09-06 17:10:34 +02:00
|
|
|
<!-- YAML
|
2017-09-01 18:50:47 +02:00
|
|
|
added: v9.0.0
|
2017-09-06 17:10:34 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `spkac` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {boolean} `true` if the given `spkac` data structure is valid,
|
2018-04-06 17:28:20 +02:00
|
|
|
`false` otherwise.
|
2017-09-06 17:10:34 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { Certificate } = require('crypto');
|
|
|
|
const spkac = getSpkacSomehow();
|
|
|
|
console.log(Certificate.verifySpkac(Buffer.from(spkac)));
|
|
|
|
// Prints: true or false
|
|
|
|
```
|
|
|
|
|
|
|
|
### Legacy API
|
|
|
|
|
|
|
|
As a still supported legacy interface, it is possible (but not recommended) to
|
|
|
|
create new instances of the `crypto.Certificate` class as illustrated in the
|
|
|
|
examples below.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
#### `new crypto.Certificate()`
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Instances of the `Certificate` class can be created using the `new` keyword
|
|
|
|
or by calling `crypto.Certificate()` as a function:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const cert1 = new crypto.Certificate();
|
|
|
|
const cert2 = crypto.Certificate();
|
|
|
|
```
|
2012-10-13 01:26:14 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
#### `certificate.exportChallenge(spkac)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.8
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `spkac` {string | Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Buffer} The challenge component of the `spkac` data structure, which
|
2018-04-06 17:28:20 +02:00
|
|
|
includes a public key and a challenge.
|
2012-10-13 01:26:14 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const cert = require('crypto').Certificate();
|
|
|
|
const spkac = getSpkacSomehow();
|
|
|
|
const challenge = cert.exportChallenge(spkac);
|
|
|
|
console.log(challenge.toString('utf8'));
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: the challenge as a UTF8 string
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-10-13 01:26:14 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
#### `certificate.exportPublicKey(spkac)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.8
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `spkac` {string | Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Buffer} The public key component of the `spkac` data structure,
|
2018-04-06 17:28:20 +02:00
|
|
|
which includes a public key and a challenge.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const cert = require('crypto').Certificate();
|
|
|
|
const spkac = getSpkacSomehow();
|
|
|
|
const publicKey = cert.exportPublicKey(spkac);
|
|
|
|
console.log(publicKey);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: the public key as <Buffer ...>
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-10-13 02:44:11 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
#### `certificate.verifySpkac(spkac)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.8
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `spkac` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {boolean} `true` if the given `spkac` data structure is valid,
|
2018-04-06 17:28:20 +02:00
|
|
|
`false` otherwise.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const cert = require('crypto').Certificate();
|
|
|
|
const spkac = getSpkacSomehow();
|
2016-04-25 04:36:57 +02:00
|
|
|
console.log(cert.verifySpkac(Buffer.from(spkac)));
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: true or false
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-10-13 02:44:11 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `Cipher`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
|
|
|
-->
|
2012-10-13 02:44:11 +02:00
|
|
|
|
2019-08-25 03:16:48 +02:00
|
|
|
* Extends: {stream.Transform}
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Instances of the `Cipher` class are used to encrypt data. The class can be
|
|
|
|
used in one of two ways:
|
2012-10-13 02:44:11 +02:00
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* As a [stream][] that is both readable and writable, where plain unencrypted
|
2015-12-27 05:54:01 +01:00
|
|
|
data is written to produce encrypted data on the readable side, or
|
2019-09-13 06:22:29 +02:00
|
|
|
* Using the [`cipher.update()`][] and [`cipher.final()`][] methods to produce
|
2016-02-15 01:40:53 +01:00
|
|
|
the encrypted data.
|
2015-06-08 18:26:16 +02:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The [`crypto.createCipher()`][] or [`crypto.createCipheriv()`][] methods are
|
|
|
|
used to create `Cipher` instances. `Cipher` objects are not to be created
|
|
|
|
directly using the `new` keyword.
|
2015-06-08 18:26:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Example: Using `Cipher` objects as streams:
|
2015-06-08 18:26:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2018-11-05 16:25:47 +01:00
|
|
|
|
|
|
|
const algorithm = 'aes-192-cbc';
|
|
|
|
const password = 'Password used to generate key';
|
|
|
|
// Key length is dependent on the algorithm. In this case for aes192, it is
|
|
|
|
// 24 bytes (192 bits).
|
|
|
|
// Use async `crypto.scrypt()` instead.
|
|
|
|
const key = crypto.scryptSync(password, 'salt', 24);
|
|
|
|
// Use `crypto.randomBytes()` to generate a random iv instead of the static iv
|
|
|
|
// shown here.
|
|
|
|
const iv = Buffer.alloc(16, 0); // Initialization vector.
|
|
|
|
|
|
|
|
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
2015-06-08 18:26:16 +02:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
let encrypted = '';
|
2016-01-17 18:39:07 +01:00
|
|
|
cipher.on('readable', () => {
|
2019-01-07 15:37:34 +01:00
|
|
|
let chunk;
|
|
|
|
while (null !== (chunk = cipher.read())) {
|
|
|
|
encrypted += chunk.toString('hex');
|
|
|
|
}
|
2016-01-22 03:04:39 +01:00
|
|
|
});
|
|
|
|
cipher.on('end', () => {
|
|
|
|
console.log(encrypted);
|
2018-11-05 16:25:47 +01:00
|
|
|
// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
2015-06-08 18:26:16 +02:00
|
|
|
|
2016-01-22 03:04:39 +01:00
|
|
|
cipher.write('some clear text data');
|
2016-01-17 18:39:07 +01:00
|
|
|
cipher.end();
|
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Example: Using `Cipher` and piped streams:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
2018-11-05 16:25:47 +01:00
|
|
|
|
|
|
|
const algorithm = 'aes-192-cbc';
|
|
|
|
const password = 'Password used to generate key';
|
|
|
|
// Use the async `crypto.scrypt()` instead.
|
|
|
|
const key = crypto.scryptSync(password, 'salt', 24);
|
|
|
|
// Use `crypto.randomBytes()` to generate a random iv instead of the static iv
|
|
|
|
// shown here.
|
|
|
|
const iv = Buffer.alloc(16, 0); // Initialization vector.
|
|
|
|
|
|
|
|
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const input = fs.createReadStream('test.js');
|
|
|
|
const output = fs.createWriteStream('test.enc');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
input.pipe(cipher).pipe(output);
|
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2018-11-05 16:25:47 +01:00
|
|
|
|
|
|
|
const algorithm = 'aes-192-cbc';
|
|
|
|
const password = 'Password used to generate key';
|
|
|
|
// Use the async `crypto.scrypt()` instead.
|
|
|
|
const key = crypto.scryptSync(password, 'salt', 24);
|
|
|
|
// Use `crypto.randomBytes` to generate a random iv instead of the static iv
|
|
|
|
// shown here.
|
|
|
|
const iv = Buffer.alloc(16, 0); // Initialization vector.
|
|
|
|
|
|
|
|
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
|
2016-01-22 03:04:39 +01:00
|
|
|
encrypted += cipher.final('hex');
|
|
|
|
console.log(encrypted);
|
2018-11-05 16:25:47 +01:00
|
|
|
// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `cipher.final([outputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string} Any remaining enciphered contents.
|
2018-11-07 21:27:47 +01:00
|
|
|
If `outputEncoding` is specified, a string is
|
2018-04-11 15:15:25 +02:00
|
|
|
returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Once the `cipher.final()` method has been called, the `Cipher` object can no
|
|
|
|
longer be used to encrypt data. Attempts to call `cipher.final()` more than
|
|
|
|
once will result in an error being thrown.
|
2014-06-25 12:11:09 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `cipher.setAAD(buffer[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.0.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2019-12-09 06:23:44 +01:00
|
|
|
* `buffer` {Buffer | TypedArray | DataView}
|
2018-07-12 19:48:11 +02:00
|
|
|
* `options` {Object} [`stream.transform` options][]
|
2019-09-13 06:22:29 +02:00
|
|
|
* `plaintextLength` {number}
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Cipher} for method chaining.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-06-14 15:18:14 +02:00
|
|
|
When using an authenticated encryption mode (`GCM`, `CCM` and `OCB` are
|
|
|
|
currently supported), the `cipher.setAAD()` method sets the value used for the
|
2015-12-27 05:54:01 +01:00
|
|
|
_additional authenticated data_ (AAD) input parameter.
|
|
|
|
|
2018-06-14 15:18:14 +02:00
|
|
|
The `options` argument is optional for `GCM` and `OCB`. When using `CCM`, the
|
2017-12-18 13:22:08 +01:00
|
|
|
`plaintextLength` option must be specified and its value must match the length
|
|
|
|
of the plaintext in bytes. See [CCM mode][].
|
|
|
|
|
2016-11-01 21:02:20 +01:00
|
|
|
The `cipher.setAAD()` method must be called before [`cipher.update()`][].
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `cipher.getAuthTag()`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.0.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-06-14 15:18:14 +02:00
|
|
|
* Returns: {Buffer} When using an authenticated encryption mode (`GCM`, `CCM`
|
|
|
|
and `OCB` are currently supported), the `cipher.getAuthTag()` method returns a
|
2018-04-11 14:58:15 +02:00
|
|
|
[`Buffer`][] containing the _authentication tag_ that has been computed from
|
|
|
|
the given data.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
The `cipher.getAuthTag()` method should only be called after encryption has
|
2016-02-15 01:40:53 +01:00
|
|
|
been completed using the [`cipher.final()`][] method.
|
2012-10-22 19:37:20 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `cipher.setAutoPadding([autoPadding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.1
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `autoPadding` {boolean} **Default:** `true`
|
|
|
|
* Returns: {Cipher} for method chaining.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
When using block encryption algorithms, the `Cipher` class will automatically
|
|
|
|
add padding to the input data to the appropriate block size. To disable the
|
|
|
|
default padding call `cipher.setAutoPadding(false)`.
|
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
When `autoPadding` is `false`, the length of the entire input data must be a
|
2018-04-29 19:46:41 +02:00
|
|
|
multiple of the cipher's block size or [`cipher.final()`][] will throw an error.
|
2015-12-27 05:54:01 +01:00
|
|
|
Disabling automatic padding is useful for non-standard padding, for instance
|
|
|
|
using `0x0` instead of PKCS padding.
|
|
|
|
|
2016-11-01 21:02:20 +01:00
|
|
|
The `cipher.setAutoPadding()` method must be called before
|
|
|
|
[`cipher.final()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `cipher.update(data[, inputEncoding][, outputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
2017-06-06 11:00:32 +02:00
|
|
|
description: The default `inputEncoding` changed from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `data` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the data.
|
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
Updates the cipher with `data`. If the `inputEncoding` argument is given,
|
2018-11-07 21:27:47 +01:00
|
|
|
the `data`
|
2017-06-06 11:00:32 +02:00
|
|
|
argument is a string using the specified encoding. If the `inputEncoding`
|
2017-04-05 00:59:30 +02:00
|
|
|
argument is not given, `data` must be a [`Buffer`][], `TypedArray`, or
|
2018-04-02 07:38:48 +02:00
|
|
|
`DataView`. If `data` is a [`Buffer`][], `TypedArray`, or `DataView`, then
|
2017-06-06 11:00:32 +02:00
|
|
|
`inputEncoding` is ignored.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
The `outputEncoding` specifies the output format of the enciphered
|
2018-11-07 21:27:47 +01:00
|
|
|
data. If the `outputEncoding`
|
2015-12-27 05:54:01 +01:00
|
|
|
is specified, a string using the specified encoding is returned. If no
|
2017-06-06 11:00:32 +02:00
|
|
|
`outputEncoding` is provided, a [`Buffer`][] is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `cipher.update()` method can be called multiple times with new data until
|
2016-02-15 01:40:53 +01:00
|
|
|
[`cipher.final()`][] is called. Calling `cipher.update()` after
|
|
|
|
[`cipher.final()`][] will result in an error being thrown.
|
2011-04-03 10:09:00 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `Decipher`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
|
|
|
-->
|
2011-04-03 10:09:00 +02:00
|
|
|
|
2019-08-25 03:16:48 +02:00
|
|
|
* Extends: {stream.Transform}
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Instances of the `Decipher` class are used to decrypt data. The class can be
|
|
|
|
used in one of two ways:
|
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* As a [stream][] that is both readable and writable, where plain encrypted
|
2015-12-27 05:54:01 +01:00
|
|
|
data is written to produce unencrypted data on the readable side, or
|
2019-09-13 06:22:29 +02:00
|
|
|
* Using the [`decipher.update()`][] and [`decipher.final()`][] methods to
|
2016-02-15 01:40:53 +01:00
|
|
|
produce the unencrypted data.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The [`crypto.createDecipher()`][] or [`crypto.createDecipheriv()`][] methods are
|
|
|
|
used to create `Decipher` instances. `Decipher` objects are not to be created
|
2015-12-27 05:54:01 +01:00
|
|
|
directly using the `new` keyword.
|
|
|
|
|
|
|
|
Example: Using `Decipher` objects as streams:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2018-11-05 16:25:47 +01:00
|
|
|
|
|
|
|
const algorithm = 'aes-192-cbc';
|
|
|
|
const password = 'Password used to generate key';
|
|
|
|
// Key length is dependent on the algorithm. In this case for aes192, it is
|
|
|
|
// 24 bytes (192 bits).
|
|
|
|
// Use the async `crypto.scrypt()` instead.
|
|
|
|
const key = crypto.scryptSync(password, 'salt', 24);
|
|
|
|
// The IV is usually passed along with the ciphertext.
|
|
|
|
const iv = Buffer.alloc(16, 0); // Initialization vector.
|
|
|
|
|
|
|
|
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
let decrypted = '';
|
2016-01-17 18:39:07 +01:00
|
|
|
decipher.on('readable', () => {
|
2019-01-07 15:37:34 +01:00
|
|
|
while (null !== (chunk = decipher.read())) {
|
|
|
|
decrypted += chunk.toString('utf8');
|
|
|
|
}
|
2016-01-22 03:04:39 +01:00
|
|
|
});
|
|
|
|
decipher.on('end', () => {
|
|
|
|
console.log(decrypted);
|
|
|
|
// Prints: some clear text data
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2018-11-05 16:25:47 +01:00
|
|
|
// Encrypted with same algorithm, key and iv.
|
2017-04-21 16:38:31 +02:00
|
|
|
const encrypted =
|
2018-11-05 16:25:47 +01:00
|
|
|
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
|
2016-01-22 03:04:39 +01:00
|
|
|
decipher.write(encrypted, 'hex');
|
2016-01-17 18:39:07 +01:00
|
|
|
decipher.end();
|
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Example: Using `Decipher` and piped streams:
|
|
|
|
|
2016-01-25 15:28:43 +01:00
|
|
|
```js
|
2016-01-17 18:39:07 +01:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
2018-11-05 16:25:47 +01:00
|
|
|
|
|
|
|
const algorithm = 'aes-192-cbc';
|
|
|
|
const password = 'Password used to generate key';
|
|
|
|
// Use the async `crypto.scrypt()` instead.
|
|
|
|
const key = crypto.scryptSync(password, 'salt', 24);
|
|
|
|
// The IV is usually passed along with the ciphertext.
|
|
|
|
const iv = Buffer.alloc(16, 0); // Initialization vector.
|
|
|
|
|
|
|
|
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const input = fs.createReadStream('test.enc');
|
|
|
|
const output = fs.createWriteStream('test.js');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
input.pipe(decipher).pipe(output);
|
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
|
2011-04-03 10:09:00 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2011-04-03 10:09:00 +02:00
|
|
|
|
2018-11-05 16:25:47 +01:00
|
|
|
const algorithm = 'aes-192-cbc';
|
|
|
|
const password = 'Password used to generate key';
|
|
|
|
// Use the async `crypto.scrypt()` instead.
|
|
|
|
const key = crypto.scryptSync(password, 'salt', 24);
|
|
|
|
// The IV is usually passed along with the ciphertext.
|
|
|
|
const iv = Buffer.alloc(16, 0); // Initialization vector.
|
|
|
|
|
|
|
|
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
|
|
|
|
|
|
// Encrypted using same algorithm, key and iv.
|
2017-04-21 16:38:31 +02:00
|
|
|
const encrypted =
|
2018-11-05 16:25:47 +01:00
|
|
|
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
|
2017-01-20 02:42:50 +01:00
|
|
|
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
2016-01-22 03:04:39 +01:00
|
|
|
decrypted += decipher.final('utf8');
|
|
|
|
console.log(decrypted);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: some clear text data
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2011-04-03 10:09:00 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `decipher.final([outputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string} Any remaining deciphered contents.
|
2018-11-07 21:27:47 +01:00
|
|
|
If `outputEncoding` is specified, a string is
|
2018-04-11 15:15:25 +02:00
|
|
|
returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Once the `decipher.final()` method has been called, the `Decipher` object can
|
|
|
|
no longer be used to decrypt data. Attempts to call `decipher.final()` more
|
|
|
|
than once will result in an error being thrown.
|
2012-10-30 23:46:43 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `decipher.setAAD(buffer[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.0.0
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v7.2.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/9398
|
|
|
|
description: This method now returns a reference to `decipher`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `buffer` {Buffer | TypedArray | DataView}
|
|
|
|
* `options` {Object} [`stream.transform` options][]
|
2019-09-13 06:22:29 +02:00
|
|
|
* `plaintextLength` {number}
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Decipher} for method chaining.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2018-06-14 15:18:14 +02:00
|
|
|
When using an authenticated encryption mode (`GCM`, `CCM` and `OCB` are
|
|
|
|
currently supported), the `decipher.setAAD()` method sets the value used for the
|
2015-12-27 05:54:01 +01:00
|
|
|
_additional authenticated data_ (AAD) input parameter.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-06-20 12:24:30 +02:00
|
|
|
The `options` argument is optional for `GCM`. When using `CCM`, the
|
|
|
|
`plaintextLength` option must be specified and its value must match the length
|
2020-04-27 17:30:42 +02:00
|
|
|
of the ciphertext in bytes. See [CCM mode][].
|
2018-06-20 12:24:30 +02:00
|
|
|
|
2016-11-01 21:02:20 +01:00
|
|
|
The `decipher.setAAD()` method must be called before [`decipher.update()`][].
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `decipher.setAuthTag(buffer)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.0.0
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
2018-10-03 01:01:19 +02:00
|
|
|
- version: v11.0.0
|
2017-12-28 14:33:19 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/17825
|
|
|
|
description: This method now throws if the GCM tag length is invalid.
|
2017-02-21 23:38:44 +01:00
|
|
|
- version: v7.2.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/9398
|
|
|
|
description: This method now returns a reference to `decipher`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `buffer` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Decipher} for method chaining.
|
2012-10-11 00:44:47 +02:00
|
|
|
|
2018-06-14 15:18:14 +02:00
|
|
|
When using an authenticated encryption mode (`GCM`, `CCM` and `OCB` are
|
|
|
|
currently supported), the `decipher.setAuthTag()` method is used to pass in the
|
2016-02-15 01:40:53 +01:00
|
|
|
received _authentication tag_. If no tag is provided, or if the cipher text
|
2017-12-28 13:00:03 +01:00
|
|
|
has been tampered with, [`decipher.final()`][] will throw, indicating that the
|
2017-12-28 14:33:19 +01:00
|
|
|
cipher text should be discarded due to failed authentication. If the tag length
|
2018-06-21 17:59:49 +02:00
|
|
|
is invalid according to [NIST SP 800-38D][] or does not match the value of the
|
|
|
|
`authTagLength` option, `decipher.setAuthTag()` will throw an error.
|
2018-01-25 16:50:05 +01:00
|
|
|
|
2020-04-27 18:19:30 +02:00
|
|
|
The `decipher.setAuthTag()` method must be called before [`decipher.update()`][]
|
|
|
|
for `CCM` mode or before [`decipher.final()`][] for `GCM` and `OCB` modes.
|
|
|
|
`decipher.setAuthTag()` can only be called once.
|
2016-11-01 21:02:20 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `decipher.setAutoPadding([autoPadding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.1
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `autoPadding` {boolean} **Default:** `true`
|
|
|
|
* Returns: {Decipher} for method chaining.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
When data has been encrypted without standard block padding, calling
|
2016-04-04 20:43:35 +02:00
|
|
|
`decipher.setAutoPadding(false)` will disable automatic padding to prevent
|
2016-02-15 01:40:53 +01:00
|
|
|
[`decipher.final()`][] from checking for and removing padding.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Turning auto padding off will only work if the input data's length is a
|
|
|
|
multiple of the ciphers block size.
|
|
|
|
|
|
|
|
The `decipher.setAutoPadding()` method must be called before
|
2016-11-01 21:02:20 +01:00
|
|
|
[`decipher.final()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `decipher.update(data[, inputEncoding][, outputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
2017-06-06 11:00:32 +02:00
|
|
|
description: The default `inputEncoding` changed from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `data` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the `data` string.
|
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2011-07-29 20:03:58 +02:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
Updates the decipher with `data`. If the `inputEncoding` argument is given,
|
2018-11-07 21:27:47 +01:00
|
|
|
the `data`
|
2017-06-06 11:00:32 +02:00
|
|
|
argument is a string using the specified encoding. If the `inputEncoding`
|
2015-12-27 05:54:01 +01:00
|
|
|
argument is not given, `data` must be a [`Buffer`][]. If `data` is a
|
2017-06-06 11:00:32 +02:00
|
|
|
[`Buffer`][] then `inputEncoding` is ignored.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
The `outputEncoding` specifies the output format of the enciphered
|
2018-11-07 21:27:47 +01:00
|
|
|
data. If the `outputEncoding`
|
2015-12-27 05:54:01 +01:00
|
|
|
is specified, a string using the specified encoding is returned. If no
|
2017-06-06 11:00:32 +02:00
|
|
|
`outputEncoding` is provided, a [`Buffer`][] is returned.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
The `decipher.update()` method can be called multiple times with new data until
|
2016-02-15 01:40:53 +01:00
|
|
|
[`decipher.final()`][] is called. Calling `decipher.update()` after
|
|
|
|
[`decipher.final()`][] will result in an error being thrown.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `DiffieHellman`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `DiffieHellman` class is a utility for creating Diffie-Hellman key
|
|
|
|
exchanges.
|
|
|
|
|
|
|
|
Instances of the `DiffieHellman` class can be created using the
|
2016-02-15 01:40:53 +01:00
|
|
|
[`crypto.createDiffieHellman()`][] function.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const assert = require('assert');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Generate Alice's keys...
|
2016-03-01 18:26:32 +01:00
|
|
|
const alice = crypto.createDiffieHellman(2048);
|
2017-01-20 02:42:50 +01:00
|
|
|
const aliceKey = alice.generateKeys();
|
2012-10-30 23:46:43 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Generate Bob's keys...
|
2016-03-01 18:26:32 +01:00
|
|
|
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
|
2017-01-20 02:42:50 +01:00
|
|
|
const bobKey = bob.generateKeys();
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Exchange and generate the secret...
|
2017-01-20 02:42:50 +01:00
|
|
|
const aliceSecret = alice.computeSecret(bobKey);
|
|
|
|
const bobSecret = bob.computeSecret(aliceKey);
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-03-01 18:26:32 +01:00
|
|
|
// OK
|
2017-01-20 02:42:50 +01:00
|
|
|
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `otherPublicKey` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of an `otherPublicKey` string.
|
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
Computes the shared secret using `otherPublicKey` as the other
|
2015-12-27 05:54:01 +01:00
|
|
|
party's public key and returns the computed shared secret. The supplied
|
2017-06-06 11:00:32 +02:00
|
|
|
key is interpreted using the specified `inputEncoding`, and secret is
|
2018-11-07 21:27:47 +01:00
|
|
|
encoded using specified `outputEncoding`.
|
|
|
|
If the `inputEncoding` is not
|
2017-06-06 11:00:32 +02:00
|
|
|
provided, `otherPublicKey` is expected to be a [`Buffer`][],
|
2017-04-05 00:59:30 +02:00
|
|
|
`TypedArray`, or `DataView`.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
If `outputEncoding` is given a string is returned; otherwise, a
|
2015-12-27 05:54:01 +01:00
|
|
|
[`Buffer`][] is returned.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.generateKeys([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
Generates private and public Diffie-Hellman key values, and returns
|
2015-12-27 05:54:01 +01:00
|
|
|
the public key in the specified `encoding`. This key should be
|
2018-11-07 21:27:47 +01:00
|
|
|
transferred to the other party.
|
|
|
|
If `encoding` is provided a string is returned; otherwise a
|
2015-12-27 05:54:01 +01:00
|
|
|
[`Buffer`][] is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.getGenerator([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Returns the Diffie-Hellman generator in the specified `encoding`.
|
|
|
|
If `encoding` is provided a string is
|
2015-12-27 05:54:01 +01:00
|
|
|
returned; otherwise a [`Buffer`][] is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.getPrime([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2011-07-29 20:03:58 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Returns the Diffie-Hellman prime in the specified `encoding`.
|
|
|
|
If `encoding` is provided a string is
|
2015-12-27 05:54:01 +01:00
|
|
|
returned; otherwise a [`Buffer`][] is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.getPrivateKey([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Returns the Diffie-Hellman private key in the specified `encoding`.
|
|
|
|
If `encoding` is provided a
|
2015-12-27 05:54:01 +01:00
|
|
|
string is returned; otherwise a [`Buffer`][] is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.getPublicKey([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2011-07-24 08:56:23 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Returns the Diffie-Hellman public key in the specified `encoding`.
|
|
|
|
If `encoding` is provided a
|
2015-12-27 05:54:01 +01:00
|
|
|
string is returned; otherwise a [`Buffer`][] is returned.
|
2012-10-30 23:46:43 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.setPrivateKey(privateKey[, encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `privateKey` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the `privateKey` string.
|
2014-10-19 16:31:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Sets the Diffie-Hellman private key. If the `encoding` argument is provided,
|
|
|
|
`privateKey` is expected
|
2017-06-06 11:00:32 +02:00
|
|
|
to be a string. If no `encoding` is provided, `privateKey` is expected
|
2017-04-05 00:59:30 +02:00
|
|
|
to be a [`Buffer`][], `TypedArray`, or `DataView`.
|
2014-10-19 16:31:22 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.setPublicKey(publicKey[, encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `publicKey` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the `publicKey` string.
|
2011-07-24 08:56:23 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Sets the Diffie-Hellman public key. If the `encoding` argument is provided,
|
|
|
|
`publicKey` is expected
|
2017-06-06 11:00:32 +02:00
|
|
|
to be a string. If no `encoding` is provided, `publicKey` is expected
|
2017-04-05 00:59:30 +02:00
|
|
|
to be a [`Buffer`][], `TypedArray`, or `DataView`.
|
2011-07-24 08:56:23 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `diffieHellman.verifyError`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.12
|
|
|
|
-->
|
2012-06-12 22:02:35 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
A bit field containing any warnings and/or errors resulting from a check
|
|
|
|
performed during initialization of the `DiffieHellman` object.
|
|
|
|
|
|
|
|
The following values are valid for this property (as defined in `constants`
|
|
|
|
module):
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
* `DH_CHECK_P_NOT_SAFE_PRIME`
|
|
|
|
* `DH_CHECK_P_NOT_PRIME`
|
|
|
|
* `DH_UNABLE_TO_CHECK_GENERATOR`
|
|
|
|
* `DH_NOT_SUITABLE_GENERATOR`
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `DiffieHellmanGroup`
|
2019-07-07 09:33:18 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.5
|
|
|
|
-->
|
|
|
|
|
|
|
|
The `DiffieHellmanGroup` class takes a well-known modp group as its argument but
|
|
|
|
otherwise works the same as `DiffieHellman`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const name = 'modp1';
|
|
|
|
const dh = crypto.createDiffieHellmanGroup(name);
|
|
|
|
```
|
|
|
|
|
|
|
|
`name` is taken from [RFC 2412][] (modp1 and 2) and [RFC 3526][]:
|
2019-08-29 15:28:03 +02:00
|
|
|
|
2019-07-07 09:33:18 +02:00
|
|
|
```console
|
|
|
|
$ perl -ne 'print "$1\n" if /"(modp\d+)"/' src/node_crypto_groups.h
|
|
|
|
modp1 # 768 bits
|
|
|
|
modp2 # 1024 bits
|
|
|
|
modp5 # 1536 bits
|
|
|
|
modp14 # 2048 bits
|
|
|
|
modp15 # etc.
|
|
|
|
modp16
|
|
|
|
modp17
|
|
|
|
modp18
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `ECDH`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
-->
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `ECDH` class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
|
|
|
|
key exchanges.
|
|
|
|
|
|
|
|
Instances of the `ECDH` class can be created using the
|
2016-02-15 01:40:53 +01:00
|
|
|
[`crypto.createECDH()`][] function.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const assert = require('assert');
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Generate Alice's keys...
|
|
|
|
const alice = crypto.createECDH('secp521r1');
|
2017-01-20 02:42:50 +01:00
|
|
|
const aliceKey = alice.generateKeys();
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Generate Bob's keys...
|
|
|
|
const bob = crypto.createECDH('secp521r1');
|
2017-01-20 02:42:50 +01:00
|
|
|
const bobKey = bob.generateKeys();
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Exchange and generate the secret...
|
2017-01-20 02:42:50 +01:00
|
|
|
const aliceSecret = alice.computeSecret(bobKey);
|
|
|
|
const bobSecret = bob.computeSecret(aliceKey);
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
|
2017-06-27 22:32:32 +02:00
|
|
|
// OK
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-10-30 23:46:43 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### Class Method: `ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])`
|
2018-03-02 02:13:12 +01:00
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
added: v10.0.0
|
2018-03-02 02:13:12 +01:00
|
|
|
-->
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `key` {string | Buffer | TypedArray | DataView}
|
|
|
|
* `curve` {string}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the `key` string.
|
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `format` {string} **Default:** `'uncompressed'`
|
|
|
|
* Returns: {Buffer | string}
|
2018-03-02 02:13:12 +01:00
|
|
|
|
|
|
|
Converts the EC Diffie-Hellman public key specified by `key` and `curve` to the
|
|
|
|
format specified by `format`. The `format` argument specifies point encoding
|
|
|
|
and can be `'compressed'`, `'uncompressed'` or `'hybrid'`. The supplied key is
|
|
|
|
interpreted using the specified `inputEncoding`, and the returned key is encoded
|
2018-11-07 21:27:47 +01:00
|
|
|
using the specified `outputEncoding`.
|
2018-03-02 02:13:12 +01:00
|
|
|
|
|
|
|
Use [`crypto.getCurves()`][] to obtain a list of available curve names.
|
|
|
|
On recent OpenSSL releases, `openssl ecparam -list_curves` will also display
|
|
|
|
the name and description of each available elliptic curve.
|
|
|
|
|
|
|
|
If `format` is not specified the point will be returned in `'uncompressed'`
|
|
|
|
format.
|
|
|
|
|
|
|
|
If the `inputEncoding` is not provided, `key` is expected to be a [`Buffer`][],
|
|
|
|
`TypedArray`, or `DataView`.
|
|
|
|
|
|
|
|
Example (uncompressing a key):
|
|
|
|
|
|
|
|
```js
|
2018-09-03 10:51:13 +02:00
|
|
|
const { createECDH, ECDH } = require('crypto');
|
2018-03-02 02:13:12 +01:00
|
|
|
|
2018-09-03 10:51:13 +02:00
|
|
|
const ecdh = createECDH('secp256k1');
|
2018-03-02 02:13:12 +01:00
|
|
|
ecdh.generateKeys();
|
|
|
|
|
|
|
|
const compressedKey = ecdh.getPublicKey('hex', 'compressed');
|
|
|
|
|
|
|
|
const uncompressedKey = ECDH.convertKey(compressedKey,
|
|
|
|
'secp256k1',
|
|
|
|
'hex',
|
|
|
|
'hex',
|
|
|
|
'uncompressed');
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// The converted key and the uncompressed public key should be the same
|
2018-03-02 02:13:12 +01:00
|
|
|
console.log(uncompressedKey === ecdh.getPublicKey('hex'));
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
2017-11-06 23:22:42 +01:00
|
|
|
description: The default `inputEncoding` changed from `binary` to `utf8`
|
2018-03-02 18:53:46 +01:00
|
|
|
- version: v10.0.0
|
2017-11-06 23:22:42 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/16849
|
|
|
|
description: Changed error format to better support invalid public key
|
|
|
|
error
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `otherPublicKey` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the `otherPublicKey` string.
|
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
Computes the shared secret using `otherPublicKey` as the other
|
2015-12-27 05:54:01 +01:00
|
|
|
party's public key and returns the computed shared secret. The supplied
|
2017-06-06 11:00:32 +02:00
|
|
|
key is interpreted using specified `inputEncoding`, and the returned secret
|
2018-11-07 21:27:47 +01:00
|
|
|
is encoded using the specified `outputEncoding`.
|
|
|
|
If the `inputEncoding` is not
|
2017-06-06 11:00:32 +02:00
|
|
|
provided, `otherPublicKey` is expected to be a [`Buffer`][], `TypedArray`, or
|
2017-04-05 00:59:30 +02:00
|
|
|
`DataView`.
|
2011-12-27 09:43:58 +01:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
If `outputEncoding` is given a string will be returned; otherwise a
|
2015-12-27 05:54:01 +01:00
|
|
|
[`Buffer`][] is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-11-06 23:22:42 +01:00
|
|
|
`ecdh.computeSecret` will throw an
|
|
|
|
`ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY` error when `otherPublicKey`
|
|
|
|
lies outside of the elliptic curve. Since `otherPublicKey` is
|
|
|
|
usually supplied from a remote user over an insecure network,
|
|
|
|
its recommended for developers to handle this exception accordingly.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `ecdh.generateKeys([encoding[, format]])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `format` {string} **Default:** `'uncompressed'`
|
|
|
|
* Returns: {Buffer | string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
Generates private and public EC Diffie-Hellman key values, and returns
|
2015-12-27 05:54:01 +01:00
|
|
|
the public key in the specified `format` and `encoding`. This key should be
|
2015-11-04 17:23:03 +01:00
|
|
|
transferred to the other party.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-27 23:38:07 +01:00
|
|
|
The `format` argument specifies point encoding and can be `'compressed'` or
|
|
|
|
`'uncompressed'`. If `format` is not specified, the point will be returned in
|
|
|
|
`'uncompressed'` format.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
If `encoding` is provided a string is returned; otherwise a [`Buffer`][]
|
2015-12-27 05:54:01 +01:00
|
|
|
is returned.
|
2011-07-29 20:03:58 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `ecdh.getPrivateKey([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
|
|
|
* Returns: {Buffer | string} The EC Diffie-Hellman in the specified `encoding`.
|
|
|
|
|
|
|
|
If `encoding` is specified, a string is returned; otherwise a [`Buffer`][] is
|
|
|
|
returned.
|
2011-12-02 21:04:13 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `ecdh.getPublicKey([encoding][, format])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `format` {string} **Default:** `'uncompressed'`
|
|
|
|
* Returns: {Buffer | string} The EC Diffie-Hellman public key in the specified
|
2018-04-06 17:28:20 +02:00
|
|
|
`encoding` and `format`.
|
2013-11-19 22:38:15 +01:00
|
|
|
|
2016-01-27 23:38:07 +01:00
|
|
|
The `format` argument specifies point encoding and can be `'compressed'` or
|
|
|
|
`'uncompressed'`. If `format` is not specified the point will be returned in
|
|
|
|
`'uncompressed'` format.
|
2014-03-04 06:05:23 +01:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
If `encoding` is specified, a string is returned; otherwise a [`Buffer`][] is
|
2015-12-27 05:54:01 +01:00
|
|
|
returned.
|
2014-03-04 06:05:23 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `ecdh.setPrivateKey(privateKey[, encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `privateKey` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the `privateKey` string.
|
2011-07-29 20:03:58 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Sets the EC Diffie-Hellman private key.
|
|
|
|
If `encoding` is provided, `privateKey` is expected
|
2017-06-06 11:00:32 +02:00
|
|
|
to be a string; otherwise `privateKey` is expected to be a [`Buffer`][],
|
2017-04-05 00:59:30 +02:00
|
|
|
`TypedArray`, or `DataView`.
|
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
If `privateKey` is not valid for the curve specified when the `ECDH` object was
|
2015-12-27 05:54:01 +01:00
|
|
|
created, an error is thrown. Upon setting the private key, the associated
|
2018-04-29 19:46:41 +02:00
|
|
|
public point (key) is also generated and set in the `ECDH` object.
|
2015-11-19 20:00:00 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `ecdh.setPublicKey(publicKey[, encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
deprecated: v5.2.0
|
|
|
|
-->
|
2015-11-19 20:00:00 +01:00
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 0 - Deprecated
|
2015-11-19 20:00:00 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `publicKey` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the `publicKey` string.
|
2017-03-11 07:30:48 +01:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
Sets the EC Diffie-Hellman public key.
|
|
|
|
If `encoding` is provided `publicKey` is expected to
|
2017-04-05 00:59:30 +02:00
|
|
|
be a string; otherwise a [`Buffer`][], `TypedArray`, or `DataView` is expected.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
There is not normally a reason to call this method because `ECDH`
|
2015-12-27 05:54:01 +01:00
|
|
|
only requires a private key and the other party's public key to compute the
|
2016-02-15 01:40:53 +01:00
|
|
|
shared secret. Typically either [`ecdh.generateKeys()`][] or
|
|
|
|
[`ecdh.setPrivateKey()`][] will be called. The [`ecdh.setPrivateKey()`][] method
|
|
|
|
attempts to generate the public point/key associated with the private key being
|
|
|
|
set.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
Example (obtaining a shared secret):
|
2011-07-24 08:56:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const alice = crypto.createECDH('secp256k1');
|
|
|
|
const bob = crypto.createECDH('secp256k1');
|
2011-07-24 08:56:23 +02:00
|
|
|
|
2018-10-26 18:27:40 +02:00
|
|
|
// This is a shortcut way of specifying one of Alice's previous private
|
2016-01-17 18:39:07 +01:00
|
|
|
// keys. It would be unwise to use such a predictable private key in a real
|
|
|
|
// application.
|
|
|
|
alice.setPrivateKey(
|
|
|
|
crypto.createHash('sha256').update('alice', 'utf8').digest()
|
|
|
|
);
|
2015-11-19 20:00:00 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Bob uses a newly generated cryptographically strong
|
2017-01-20 02:42:50 +01:00
|
|
|
// pseudorandom key pair
|
|
|
|
bob.generateKeys();
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
|
|
|
|
const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
// aliceSecret and bobSecret should be the same shared secret value
|
|
|
|
console.log(aliceSecret === bobSecret);
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-10-30 23:46:43 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `Hash`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
|
|
|
-->
|
2011-12-27 09:43:58 +01:00
|
|
|
|
2019-08-25 03:16:48 +02:00
|
|
|
* Extends: {stream.Transform}
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `Hash` class is a utility for creating hash digests of data. It can be
|
|
|
|
used in one of two ways:
|
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* As a [stream][] that is both readable and writable, where data is written
|
2015-12-27 05:54:01 +01:00
|
|
|
to produce a computed hash digest on the readable side, or
|
2019-09-13 06:22:29 +02:00
|
|
|
* Using the [`hash.update()`][] and [`hash.digest()`][] methods to produce the
|
2015-12-27 05:54:01 +01:00
|
|
|
computed hash.
|
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The [`crypto.createHash()`][] method is used to create `Hash` instances. `Hash`
|
2015-12-27 05:54:01 +01:00
|
|
|
objects are not to be created directly using the `new` keyword.
|
|
|
|
|
|
|
|
Example: Using `Hash` objects as streams:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const hash = crypto.createHash('sha256');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
hash.on('readable', () => {
|
2019-01-07 15:37:34 +01:00
|
|
|
// Only one element is going to be produced by the
|
|
|
|
// hash stream.
|
2017-01-20 02:42:50 +01:00
|
|
|
const data = hash.read();
|
2017-06-27 22:32:32 +02:00
|
|
|
if (data) {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log(data.toString('hex'));
|
|
|
|
// Prints:
|
|
|
|
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
|
2017-06-27 22:32:32 +02:00
|
|
|
}
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
hash.write('some data to hash');
|
|
|
|
hash.end();
|
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Example: Using `Hash` and piped streams:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
|
|
|
const hash = crypto.createHash('sha256');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const input = fs.createReadStream('test.js');
|
|
|
|
input.pipe(hash).pipe(process.stdout);
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
Example: Using the [`hash.update()`][] and [`hash.digest()`][] methods:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const hash = crypto.createHash('sha256');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
hash.update('some data to hash');
|
|
|
|
console.log(hash.digest('hex'));
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `hash.copy([options])`
|
2019-10-10 00:33:15 +02:00
|
|
|
<!-- YAML
|
2019-11-05 10:55:13 +01:00
|
|
|
added: v13.1.0
|
2019-10-10 00:33:15 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `options` {Object} [`stream.transform` options][]
|
|
|
|
* Returns: {Hash}
|
|
|
|
|
|
|
|
Creates a new `Hash` object that contains a deep copy of the internal state
|
|
|
|
of the current `Hash` object.
|
|
|
|
|
|
|
|
The optional `options` argument controls stream behavior. For XOF hash
|
|
|
|
functions such as `'shake256'`, the `outputLength` option can be used to
|
|
|
|
specify the desired output length in bytes.
|
|
|
|
|
|
|
|
An error is thrown when an attempt is made to copy the `Hash` object after
|
|
|
|
its [`hash.digest()`][] method has been called.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Calculate a rolling hash.
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const hash = crypto.createHash('sha256');
|
|
|
|
|
|
|
|
hash.update('one');
|
|
|
|
console.log(hash.copy().digest('hex'));
|
|
|
|
|
|
|
|
hash.update('two');
|
|
|
|
console.log(hash.copy().digest('hex'));
|
|
|
|
|
|
|
|
hash.update('three');
|
|
|
|
console.log(hash.copy().digest('hex'));
|
|
|
|
|
|
|
|
// Etc.
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `hash.digest([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Calculates the digest of all of the data passed to be hashed (using the
|
2018-11-07 21:27:47 +01:00
|
|
|
[`hash.update()`][] method).
|
|
|
|
If `encoding` is provided a string will be returned; otherwise
|
2015-12-27 05:54:01 +01:00
|
|
|
a [`Buffer`][] is returned.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `Hash` object can not be used again after `hash.digest()` method has been
|
|
|
|
called. Multiple calls will cause an error to be thrown.
|
2011-07-29 20:03:58 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `hash.update(data[, inputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
2017-06-06 11:00:32 +02:00
|
|
|
description: The default `inputEncoding` changed from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `data` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the `data` string.
|
2011-12-02 21:04:13 +01:00
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
Updates the hash content with the given `data`, the encoding of which
|
2018-11-07 21:27:47 +01:00
|
|
|
is given in `inputEncoding`.
|
|
|
|
If `encoding` is not provided, and the `data` is a string, an
|
2017-04-05 00:59:30 +02:00
|
|
|
encoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or
|
2017-06-06 11:00:32 +02:00
|
|
|
`DataView`, then `inputEncoding` is ignored.
|
2011-12-02 21:04:13 +01:00
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
This can be called many times with new data as it is streamed.
|
2013-11-19 22:38:15 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `Hmac`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
|
|
|
-->
|
2013-11-19 22:38:15 +01:00
|
|
|
|
2019-08-25 03:16:48 +02:00
|
|
|
* Extends: {stream.Transform}
|
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
The `Hmac` class is a utility for creating cryptographic HMAC digests. It can
|
2015-12-27 05:54:01 +01:00
|
|
|
be used in one of two ways:
|
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* As a [stream][] that is both readable and writable, where data is written
|
2015-12-27 05:54:01 +01:00
|
|
|
to produce a computed HMAC digest on the readable side, or
|
2019-09-13 06:22:29 +02:00
|
|
|
* Using the [`hmac.update()`][] and [`hmac.digest()`][] methods to produce the
|
2015-12-27 05:54:01 +01:00
|
|
|
computed HMAC digest.
|
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The [`crypto.createHmac()`][] method is used to create `Hmac` instances. `Hmac`
|
2015-12-27 05:54:01 +01:00
|
|
|
objects are not to be created directly using the `new` keyword.
|
|
|
|
|
|
|
|
Example: Using `Hmac` objects as streams:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const hmac = crypto.createHmac('sha256', 'a secret');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
hmac.on('readable', () => {
|
2019-01-07 15:37:34 +01:00
|
|
|
// Only one element is going to be produced by the
|
|
|
|
// hash stream.
|
2017-01-20 02:42:50 +01:00
|
|
|
const data = hmac.read();
|
2017-06-27 22:32:32 +02:00
|
|
|
if (data) {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log(data.toString('hex'));
|
|
|
|
// Prints:
|
|
|
|
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
|
2017-06-27 22:32:32 +02:00
|
|
|
}
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
hmac.write('some data to hash');
|
|
|
|
hmac.end();
|
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Example: Using `Hmac` and piped streams:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
|
|
|
const hmac = crypto.createHmac('sha256', 'a secret');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const input = fs.createReadStream('test.js');
|
|
|
|
input.pipe(hmac).pipe(process.stdout);
|
|
|
|
```
|
2014-03-04 06:05:23 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
Example: Using the [`hmac.update()`][] and [`hmac.digest()`][] methods:
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const hmac = crypto.createHmac('sha256', 'a secret');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
hmac.update('some data to hash');
|
|
|
|
console.log(hmac.digest('hex'));
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2014-03-04 06:05:23 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `hmac.digest([encoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
* `encoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2013-11-19 22:38:15 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
Calculates the HMAC digest of all of the data passed using [`hmac.update()`][].
|
2018-11-07 21:27:47 +01:00
|
|
|
If `encoding` is
|
2016-02-15 01:40:53 +01:00
|
|
|
provided a string is returned; otherwise a [`Buffer`][] is returned;
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `Hmac` object can not be used again after `hmac.digest()` has been
|
|
|
|
called. Multiple calls to `hmac.digest()` will result in an error being thrown.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `hmac.update(data[, inputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
2017-06-06 11:00:32 +02:00
|
|
|
description: The default `inputEncoding` changed from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `data` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the `data` string.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2016-03-01 03:03:14 +01:00
|
|
|
Updates the `Hmac` content with the given `data`, the encoding of which
|
2018-11-07 21:27:47 +01:00
|
|
|
is given in `inputEncoding`.
|
|
|
|
If `encoding` is not provided, and the `data` is a string, an
|
2017-04-05 00:59:30 +02:00
|
|
|
encoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or
|
2017-06-06 11:00:32 +02:00
|
|
|
`DataView`, then `inputEncoding` is ignored.
|
2016-03-01 03:03:14 +01:00
|
|
|
|
|
|
|
This can be called many times with new data as it is streamed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `KeyObject`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2019-03-11 21:26:22 +01:00
|
|
|
changes:
|
2020-05-11 19:55:37 +02:00
|
|
|
- version: REPLACEME
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33360
|
|
|
|
description: Instances of this class can now be passed to worker threads
|
|
|
|
using `postMessage`.
|
2019-03-27 22:43:03 +01:00
|
|
|
- version: v11.13.0
|
2019-03-11 21:26:22 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26438
|
|
|
|
description: This class is now exported.
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
|
|
|
|
2019-03-11 21:26:22 +01:00
|
|
|
Node.js uses a `KeyObject` class to represent a symmetric or asymmetric key,
|
|
|
|
and each kind of key exposes different functions. The
|
|
|
|
[`crypto.createSecretKey()`][], [`crypto.createPublicKey()`][] and
|
|
|
|
[`crypto.createPrivateKey()`][] methods are used to create `KeyObject`
|
|
|
|
instances. `KeyObject` objects are not to be created directly using the `new`
|
|
|
|
keyword.
|
2018-09-20 19:53:44 +02:00
|
|
|
|
|
|
|
Most applications should consider using the new `KeyObject` API instead of
|
|
|
|
passing keys as strings or `Buffer`s due to improved security features.
|
|
|
|
|
2020-05-11 19:55:37 +02:00
|
|
|
`KeyObject` instances can be passed to other threads via [`postMessage()`][].
|
|
|
|
The receiver obtains a cloned `KeyObject`, and the `KeyObject` does not need to
|
|
|
|
be listed in the `transferList` argument.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `keyObject.asymmetricKeyType`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2019-02-26 11:24:38 +01:00
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
- version:
|
|
|
|
- v13.9.0
|
|
|
|
- v12.17.0
|
2019-12-29 15:52:01 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/31178
|
|
|
|
description: Added support for `'dh'`.
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-16 23:51:26 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26960
|
|
|
|
description: Added support for `'rsa-pss'`
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-19 21:38:23 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26786
|
|
|
|
description: This property now returns `undefined` for KeyObject
|
|
|
|
instances of unrecognized type instead of aborting.
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-19 12:09:01 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26774
|
|
|
|
description: Added support for `'x25519'` and `'x448'`
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-02-26 11:24:38 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26319
|
2019-03-19 21:38:23 +01:00
|
|
|
description: Added support for `'ed25519'` and `'ed448'`.
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* {string}
|
|
|
|
|
2019-03-16 23:51:26 +01:00
|
|
|
For asymmetric keys, this property represents the type of the key. Supported key
|
|
|
|
types are:
|
|
|
|
|
|
|
|
* `'rsa'` (OID 1.2.840.113549.1.1.1)
|
|
|
|
* `'rsa-pss'` (OID 1.2.840.113549.1.1.10)
|
|
|
|
* `'dsa'` (OID 1.2.840.10040.4.1)
|
|
|
|
* `'ec'` (OID 1.2.840.10045.2.1)
|
|
|
|
* `'x25519'` (OID 1.3.101.110)
|
|
|
|
* `'x448'` (OID 1.3.101.111)
|
|
|
|
* `'ed25519'` (OID 1.3.101.112)
|
|
|
|
* `'ed448'` (OID 1.3.101.113)
|
2019-12-29 15:52:01 +01:00
|
|
|
* `'dh'` (OID 1.2.840.113549.1.3.1)
|
2019-03-16 23:51:26 +01:00
|
|
|
|
2019-03-19 21:38:23 +01:00
|
|
|
This property is `undefined` for unrecognized `KeyObject` types and symmetric
|
|
|
|
keys.
|
2018-09-20 19:53:44 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `keyObject.export([options])`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `options`: {Object}
|
|
|
|
* Returns: {string | Buffer}
|
|
|
|
|
|
|
|
For symmetric keys, this function allocates a `Buffer` containing the key
|
|
|
|
material and ignores any options.
|
|
|
|
|
|
|
|
For asymmetric keys, the `options` parameter is used to determine the export
|
|
|
|
format.
|
|
|
|
|
|
|
|
For public keys, the following encoding options can be used:
|
|
|
|
|
|
|
|
* `type`: {string} Must be one of `'pkcs1'` (RSA only) or `'spki'`.
|
|
|
|
* `format`: {string} Must be `'pem'` or `'der'`.
|
|
|
|
|
|
|
|
For private keys, the following encoding options can be used:
|
|
|
|
|
|
|
|
* `type`: {string} Must be one of `'pkcs1'` (RSA only), `'pkcs8'` or
|
|
|
|
`'sec1'` (EC only).
|
|
|
|
* `format`: {string} Must be `'pem'` or `'der'`.
|
|
|
|
* `cipher`: {string} If specified, the private key will be encrypted with
|
|
|
|
the given `cipher` and `passphrase` using PKCS#5 v2.0 password based
|
|
|
|
encryption.
|
|
|
|
* `passphrase`: {string | Buffer} The passphrase to use for encryption, see
|
|
|
|
`cipher`.
|
|
|
|
|
|
|
|
When PEM encoding was selected, the result will be a string, otherwise it will
|
|
|
|
be a buffer containing the data encoded as DER.
|
|
|
|
|
2018-10-13 01:29:46 +02:00
|
|
|
PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of
|
|
|
|
the `cipher` and `format` options. The PKCS#8 `type` can be used with any
|
|
|
|
`format` to encrypt any key algorithm (RSA, EC, or DH) by specifying a
|
|
|
|
`cipher`. PKCS#1 and SEC1 can only be encrypted by specifying a `cipher`
|
|
|
|
when the PEM `format` is used. For maximum compatibility, use PKCS#8 for
|
|
|
|
encrypted private keys. Since PKCS#8 defines its own
|
|
|
|
encryption mechanism, PEM-level encryption is not supported when encrypting
|
|
|
|
a PKCS#8 key. See [RFC 5208][] for PKCS#8 encryption and [RFC 1421][] for
|
|
|
|
PKCS#1 and SEC1 encryption.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `keyObject.symmetricKeySize`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* {number}
|
|
|
|
|
|
|
|
For secret keys, this property represents the size of the key in bytes. This
|
|
|
|
property is `undefined` for asymmetric keys.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `keyObject.type`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* {string}
|
|
|
|
|
|
|
|
Depending on the type of this `KeyObject`, this property is either
|
|
|
|
`'secret'` for secret (symmetric) keys, `'public'` for public (asymmetric) keys
|
|
|
|
or `'private'` for private (asymmetric) keys.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `Sign`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
|
|
|
-->
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2019-08-25 03:16:48 +02:00
|
|
|
* Extends: {stream.Writable}
|
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
The `Sign` class is a utility for generating signatures. It can be used in one
|
2015-12-27 05:54:01 +01:00
|
|
|
of two ways:
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* As a writable [stream][], where data to be signed is written and the
|
2016-02-15 01:40:53 +01:00
|
|
|
[`sign.sign()`][] method is used to generate and return the signature, or
|
2019-09-13 06:22:29 +02:00
|
|
|
* Using the [`sign.update()`][] and [`sign.sign()`][] methods to produce the
|
2015-12-27 05:54:01 +01:00
|
|
|
signature.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2017-09-10 00:41:56 +02:00
|
|
|
The [`crypto.createSign()`][] method is used to create `Sign` instances. The
|
|
|
|
argument is the string name of the hash function to use. `Sign` objects are not
|
|
|
|
to be created directly using the `new` keyword.
|
2012-10-30 23:46:43 +01:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
Example: Using `Sign` and [`Verify`][] objects as streams:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2013-10-04 13:59:38 +02:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
const { privateKey, publicKey } = crypto.generateKeyPairSync('ec', {
|
|
|
|
namedCurve: 'sect239k1'
|
|
|
|
});
|
|
|
|
|
|
|
|
const sign = crypto.createSign('SHA256');
|
2016-01-17 18:39:07 +01:00
|
|
|
sign.write('some data to sign');
|
|
|
|
sign.end();
|
2019-01-11 20:43:58 +01:00
|
|
|
const signature = sign.sign(privateKey, 'hex');
|
2013-10-04 13:59:38 +02:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
const verify = crypto.createVerify('SHA256');
|
|
|
|
verify.write('some data to sign');
|
|
|
|
verify.end();
|
2020-01-11 08:07:49 +01:00
|
|
|
console.log(verify.verify(publicKey, signature, 'hex'));
|
|
|
|
// Prints: true
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
Example: Using the [`sign.update()`][] and [`verify.update()`][] methods:
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2016-04-08 00:38:00 +02:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
|
|
|
|
modulusLength: 2048,
|
|
|
|
});
|
2016-04-08 00:38:00 +02:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
const sign = crypto.createSign('SHA256');
|
2016-04-08 00:38:00 +02:00
|
|
|
sign.update('some data to sign');
|
2019-01-11 20:43:58 +01:00
|
|
|
sign.end();
|
|
|
|
const signature = sign.sign(privateKey);
|
2016-04-08 00:38:00 +02:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
const verify = crypto.createVerify('SHA256');
|
|
|
|
verify.update('some data to sign');
|
|
|
|
verify.end();
|
|
|
|
console.log(verify.verify(publicKey, signature));
|
|
|
|
// Prints: true
|
2016-04-08 00:38:00 +02:00
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `sign.sign(privateKey[, outputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
2017-03-06 00:41:26 +01:00
|
|
|
changes:
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-16 23:51:26 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26960
|
|
|
|
description: This function now supports RSA-PSS keys.
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: This function now supports key objects.
|
2017-03-16 04:26:14 +01:00
|
|
|
- version: v8.0.0
|
2017-03-06 00:41:26 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/11705
|
|
|
|
description: Support for RSASSA-PSS and additional options was added.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `privateKey` {Object | string | Buffer | KeyObject}
|
2019-08-21 00:05:55 +02:00
|
|
|
* `dsaEncoding` {string}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `padding` {integer}
|
|
|
|
* `saltLength` {integer}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `outputEncoding` {string} The [encoding][] of the return value.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer | string}
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Calculates the signature on all the data passed through using either
|
2016-02-15 01:40:53 +01:00
|
|
|
[`sign.update()`][] or [`sign.write()`][stream-writable-write].
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
If `privateKey` is not a [`KeyObject`][], this function behaves as if
|
|
|
|
`privateKey` had been passed to [`crypto.createPrivateKey()`][]. If it is an
|
|
|
|
object, the following additional properties can be passed:
|
2013-10-04 13:59:38 +02:00
|
|
|
|
2019-08-21 00:05:55 +02:00
|
|
|
* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the
|
|
|
|
format of the generated signature. It can be one of the following:
|
|
|
|
* `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`.
|
|
|
|
* `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363.
|
2019-10-24 06:28:42 +02:00
|
|
|
* `padding` {integer} Optional padding value for RSA, one of the following:
|
2017-03-06 00:41:26 +01:00
|
|
|
* `crypto.constants.RSA_PKCS1_PADDING` (default)
|
|
|
|
* `crypto.constants.RSA_PKCS1_PSS_PADDING`
|
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
`RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function
|
2019-03-16 23:51:26 +01:00
|
|
|
used to sign the message as specified in section 3.1 of [RFC 4055][], unless
|
|
|
|
an MGF1 hash function has been specified as part of the key in compliance with
|
|
|
|
section 3.3 of [RFC 4055][].
|
2019-10-24 06:28:42 +02:00
|
|
|
* `saltLength` {integer} Salt length for when padding is
|
2017-03-06 00:41:26 +01:00
|
|
|
`RSA_PKCS1_PSS_PADDING`. The special value
|
|
|
|
`crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest
|
|
|
|
size, `crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN` (default) sets it to the
|
|
|
|
maximum permissible value.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-11-07 21:27:47 +01:00
|
|
|
If `outputEncoding` is provided a string is returned; otherwise a [`Buffer`][]
|
|
|
|
is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `Sign` object can not be again used after `sign.sign()` method has been
|
|
|
|
called. Multiple calls to `sign.sign()` will result in an error being thrown.
|
2011-07-29 20:03:58 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `sign.update(data[, inputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
2017-06-06 11:00:32 +02:00
|
|
|
description: The default `inputEncoding` changed from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `data` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the `data` string.
|
2016-03-01 03:03:14 +01:00
|
|
|
|
|
|
|
Updates the `Sign` content with the given `data`, the encoding of which
|
2018-11-07 21:27:47 +01:00
|
|
|
is given in `inputEncoding`.
|
|
|
|
If `encoding` is not provided, and the `data` is a string, an
|
2017-04-05 00:59:30 +02:00
|
|
|
encoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or
|
2017-06-06 11:00:32 +02:00
|
|
|
`DataView`, then `inputEncoding` is ignored.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-01 03:03:14 +01:00
|
|
|
This can be called many times with new data as it is streamed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
## Class: `Verify`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
|
|
|
-->
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2019-08-25 03:16:48 +02:00
|
|
|
* Extends: {stream.Writable}
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `Verify` class is a utility for verifying signatures. It can be used in one
|
|
|
|
of two ways:
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* As a writable [stream][] where written data is used to validate against the
|
2015-12-27 05:54:01 +01:00
|
|
|
supplied signature, or
|
2019-09-13 06:22:29 +02:00
|
|
|
* Using the [`verify.update()`][] and [`verify.verify()`][] methods to verify
|
2016-02-15 01:40:53 +01:00
|
|
|
the signature.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2016-11-25 08:07:24 +01:00
|
|
|
The [`crypto.createVerify()`][] method is used to create `Verify` instances.
|
|
|
|
`Verify` objects are not to be created directly using the `new` keyword.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
See [`Sign`][] for examples.
|
2012-10-30 23:46:43 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `verify.update(data[, inputEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
2017-06-06 11:00:32 +02:00
|
|
|
description: The default `inputEncoding` changed from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `data` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `inputEncoding` {string} The [encoding][] of the `data` string.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-01 03:03:14 +01:00
|
|
|
Updates the `Verify` content with the given `data`, the encoding of which
|
2018-11-07 21:27:47 +01:00
|
|
|
is given in `inputEncoding`.
|
|
|
|
If `inputEncoding` is not provided, and the `data` is a string, an
|
2017-04-05 00:59:30 +02:00
|
|
|
encoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or
|
2017-06-06 11:00:32 +02:00
|
|
|
`DataView`, then `inputEncoding` is ignored.
|
2016-03-01 03:03:14 +01:00
|
|
|
|
|
|
|
This can be called many times with new data as it is streamed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `verify.verify(object, signature[, signatureEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
2017-03-06 00:41:26 +01:00
|
|
|
changes:
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-16 23:51:26 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26960
|
|
|
|
description: This function now supports RSA-PSS keys.
|
2019-01-16 15:55:55 +01:00
|
|
|
- version: v11.7.0
|
2018-12-25 13:13:52 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/25217
|
|
|
|
description: The key can now be a private key.
|
2017-03-16 04:26:14 +01:00
|
|
|
- version: v8.0.0
|
2017-03-06 00:41:26 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/11705
|
|
|
|
description: Support for RSASSA-PSS and additional options was added.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `object` {Object | string | Buffer | KeyObject}
|
2019-08-21 00:05:55 +02:00
|
|
|
* `dsaEncoding` {string}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `padding` {integer}
|
|
|
|
* `saltLength` {integer}
|
2018-07-12 19:48:11 +02:00
|
|
|
* `signature` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `signatureEncoding` {string} The [encoding][] of the `signature` string.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {boolean} `true` or `false` depending on the validity of the
|
2018-04-06 17:28:20 +02:00
|
|
|
signature for the data and public key.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Verifies the provided data using the given `object` and `signature`.
|
2017-03-06 00:41:26 +01:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
If `object` is not a [`KeyObject`][], this function behaves as if
|
|
|
|
`object` had been passed to [`crypto.createPublicKey()`][]. If it is an
|
|
|
|
object, the following additional properties can be passed:
|
|
|
|
|
2019-08-21 00:05:55 +02:00
|
|
|
* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the
|
|
|
|
format of the generated signature. It can be one of the following:
|
|
|
|
* `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`.
|
|
|
|
* `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363.
|
2019-10-24 06:28:42 +02:00
|
|
|
* `padding` {integer} Optional padding value for RSA, one of the following:
|
2017-03-06 00:41:26 +01:00
|
|
|
* `crypto.constants.RSA_PKCS1_PADDING` (default)
|
|
|
|
* `crypto.constants.RSA_PKCS1_PSS_PADDING`
|
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
`RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function
|
2019-03-16 23:51:26 +01:00
|
|
|
used to verify the message as specified in section 3.1 of [RFC 4055][], unless
|
|
|
|
an MGF1 hash function has been specified as part of the key in compliance with
|
|
|
|
section 3.3 of [RFC 4055][].
|
2019-10-24 06:28:42 +02:00
|
|
|
* `saltLength` {integer} Salt length for when padding is
|
2017-03-06 00:41:26 +01:00
|
|
|
`RSA_PKCS1_PSS_PADDING`. The special value
|
|
|
|
`crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest
|
|
|
|
size, `crypto.constants.RSA_PSS_SALTLEN_AUTO` (default) causes it to be
|
|
|
|
determined automatically.
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `signature` argument is the previously calculated signature for the data, in
|
2018-11-07 21:27:47 +01:00
|
|
|
the `signatureEncoding`.
|
|
|
|
If a `signatureEncoding` is specified, the `signature` is expected to be a
|
2017-04-05 00:59:30 +02:00
|
|
|
string; otherwise `signature` is expected to be a [`Buffer`][],
|
|
|
|
`TypedArray`, or `DataView`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-07-25 09:16:01 +02:00
|
|
|
The `verify` object can not be used again after `verify.verify()` has been
|
2015-12-27 05:54:01 +01:00
|
|
|
called. Multiple calls to `verify.verify()` will result in an error being
|
|
|
|
thrown.
|
2011-07-29 20:03:58 +02:00
|
|
|
|
2018-12-25 13:13:52 +01:00
|
|
|
Because public keys can be derived from private keys, a private key may
|
|
|
|
be passed instead of a public key.
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
## `crypto` module methods and properties
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.constants`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.3.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Object} An object containing commonly used constants for crypto and
|
2018-04-06 17:28:20 +02:00
|
|
|
security related operations. The specific constants currently defined are
|
2020-06-14 23:49:34 +02:00
|
|
|
described in [Crypto constants][].
|
2016-05-02 19:27:12 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.DEFAULT_ENCODING`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
2018-03-02 18:53:46 +01:00
|
|
|
deprecated: v10.0.0
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2018-10-19 20:46:09 +02:00
|
|
|
> Stability: 0 - Deprecated
|
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
The default encoding to use for functions that can take either strings
|
2016-02-15 01:40:53 +01:00
|
|
|
or [buffers][`Buffer`]. The default value is `'buffer'`, which makes methods
|
|
|
|
default to [`Buffer`][] objects.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
The `crypto.DEFAULT_ENCODING` mechanism is provided for backwards compatibility
|
2016-06-02 18:55:36 +02:00
|
|
|
with legacy programs that expect `'latin1'` to be the default encoding.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2018-01-24 00:58:14 +01:00
|
|
|
New applications should expect the default to be `'buffer'`.
|
|
|
|
|
|
|
|
This property is deprecated.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.fips`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.0.0
|
2018-03-02 18:53:46 +01:00
|
|
|
deprecated: v10.0.0
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2016-01-23 00:10:09 +01:00
|
|
|
|
2018-10-19 20:46:09 +02:00
|
|
|
> Stability: 0 - Deprecated
|
|
|
|
|
2018-02-12 08:31:55 +01:00
|
|
|
Property for checking and controlling whether a FIPS compliant crypto provider
|
|
|
|
is currently in use. Setting to true requires a FIPS build of Node.js.
|
2016-01-23 00:10:09 +01:00
|
|
|
|
2018-01-24 01:32:19 +01:00
|
|
|
This property is deprecated. Please use `crypto.setFips()` and
|
|
|
|
`crypto.getFips()` instead.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createCipher(algorithm, password[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2018-03-02 18:53:46 +01:00
|
|
|
deprecated: v10.0.0
|
2018-04-15 14:51:24 +02:00
|
|
|
changes:
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
- version: v10.10.0
|
2018-06-14 15:18:14 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/21447
|
|
|
|
description: Ciphers in OCB mode are now supported.
|
2018-05-14 20:01:36 +02:00
|
|
|
- version: v10.2.0
|
2018-04-15 14:51:24 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/20235
|
|
|
|
description: The `authTagLength` option can now be used to produce shorter
|
|
|
|
authentication tags in GCM mode and defaults to 16 bytes.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2018-03-14 01:54:42 +01:00
|
|
|
|
|
|
|
> Stability: 0 - Deprecated: Use [`crypto.createCipheriv()`][] instead.
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
|
|
|
* `password` {string | Buffer | TypedArray | DataView}
|
|
|
|
* `options` {Object} [`stream.transform` options][]
|
|
|
|
* Returns: {Cipher}
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Creates and returns a `Cipher` object that uses the given `algorithm` and
|
2017-12-18 13:22:08 +01:00
|
|
|
`password`.
|
|
|
|
|
|
|
|
The `options` argument controls stream behavior and is optional except when a
|
2018-06-14 15:18:14 +02:00
|
|
|
cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the
|
2017-12-18 13:22:08 +01:00
|
|
|
`authTagLength` option is required and specifies the length of the
|
2018-04-15 14:51:24 +02:00
|
|
|
authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength`
|
|
|
|
option is not required but can be used to set the length of the authentication
|
|
|
|
tag that will be returned by `getAuthTag()` and defaults to 16 bytes.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
|
2018-05-03 17:07:19 +02:00
|
|
|
recent OpenSSL releases, `openssl list -cipher-algorithms`
|
|
|
|
(`openssl list-cipher-algorithms` for older versions of OpenSSL) will
|
|
|
|
display the available cipher algorithms.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `password` is used to derive the cipher key and initialization vector (IV).
|
2017-04-05 00:59:30 +02:00
|
|
|
The value must be either a `'latin1'` encoded string, a [`Buffer`][], a
|
|
|
|
`TypedArray`, or a `DataView`.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The implementation of `crypto.createCipher()` derives keys using the OpenSSL
|
|
|
|
function [`EVP_BytesToKey`][] with the digest algorithm set to MD5, one
|
|
|
|
iteration, and no salt. The lack of salt allows dictionary attacks as the same
|
|
|
|
password always creates the same key. The low iteration count and
|
|
|
|
non-cryptographically secure hash algorithm allow passwords to be tested very
|
|
|
|
rapidly.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2018-05-18 11:05:20 +02:00
|
|
|
In line with OpenSSL's recommendation to use a more modern algorithm instead of
|
2015-12-27 05:54:01 +01:00
|
|
|
[`EVP_BytesToKey`][] it is recommended that developers derive a key and IV on
|
2018-05-18 11:05:20 +02:00
|
|
|
their own using [`crypto.scrypt()`][] and to use [`crypto.createCipheriv()`][]
|
2017-06-20 16:44:53 +02:00
|
|
|
to create the `Cipher` object. Users should not use ciphers with counter mode
|
2017-11-29 03:53:24 +01:00
|
|
|
(e.g. CTR, GCM, or CCM) in `crypto.createCipher()`. A warning is emitted when
|
2017-06-20 16:44:53 +02:00
|
|
|
they are used in order to avoid the risk of IV reuse that causes
|
|
|
|
vulnerabilities. For the case when IV is reused in GCM, see [Nonce-Disrespecting
|
|
|
|
Adversaries][] for details.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createCipheriv(algorithm, key, iv[, options])`
|
2018-02-08 19:53:22 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2018-02-07 16:20:21 +01:00
|
|
|
changes:
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: The `key` argument can now be a `KeyObject`.
|
2020-04-24 18:43:06 +02:00
|
|
|
- version:
|
|
|
|
- v11.2.0
|
|
|
|
- v10.17.0
|
2018-11-04 05:04:57 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24081
|
|
|
|
description: The cipher `chacha20-poly1305` is now supported.
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
- version: v10.10.0
|
2018-06-14 15:18:14 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/21447
|
|
|
|
description: Ciphers in OCB mode are now supported.
|
2018-05-14 20:01:36 +02:00
|
|
|
- version: v10.2.0
|
2018-04-15 14:51:24 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/20235
|
|
|
|
description: The `authTagLength` option can now be used to produce shorter
|
|
|
|
authentication tags in GCM mode and defaults to 16 bytes.
|
2018-03-18 14:45:41 +01:00
|
|
|
- version: v9.9.0
|
2018-02-07 16:20:21 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/18644
|
|
|
|
description: The `iv` parameter may now be `null` for ciphers which do not
|
|
|
|
need an initialization vector.
|
2018-02-08 19:53:22 +01:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
2018-09-20 19:53:44 +02:00
|
|
|
* `key` {string | Buffer | TypedArray | DataView | KeyObject}
|
2019-09-24 01:28:17 +02:00
|
|
|
* `iv` {string | Buffer | TypedArray | DataView | null}
|
2018-07-12 19:48:11 +02:00
|
|
|
* `options` {Object} [`stream.transform` options][]
|
|
|
|
* Returns: {Cipher}
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Creates and returns a `Cipher` object, with the given `algorithm`, `key` and
|
2018-04-06 17:28:20 +02:00
|
|
|
initialization vector (`iv`).
|
|
|
|
|
2017-12-18 13:22:08 +01:00
|
|
|
The `options` argument controls stream behavior and is optional except when a
|
2018-06-14 15:18:14 +02:00
|
|
|
cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the
|
2017-12-18 13:22:08 +01:00
|
|
|
`authTagLength` option is required and specifies the length of the
|
2018-04-15 14:51:24 +02:00
|
|
|
authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength`
|
|
|
|
option is not required but can be used to set the length of the authentication
|
|
|
|
tag that will be returned by `getAuthTag()` and defaults to 16 bytes.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
|
2018-05-03 17:07:19 +02:00
|
|
|
recent OpenSSL releases, `openssl list -cipher-algorithms`
|
|
|
|
(`openssl list-cipher-algorithms` for older versions of OpenSSL) will
|
|
|
|
display the available cipher algorithms.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `key` is the raw key used by the `algorithm` and `iv` is an
|
2017-03-22 07:33:31 +01:00
|
|
|
[initialization vector][]. Both arguments must be `'utf8'` encoded strings,
|
2018-09-20 19:53:44 +02:00
|
|
|
[Buffers][`Buffer`], `TypedArray`, or `DataView`s. The `key` may optionally be
|
|
|
|
a [`KeyObject`][] of type `secret`. If the cipher does not need
|
2018-02-07 16:20:21 +01:00
|
|
|
an initialization vector, `iv` may be `null`.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2018-04-04 22:47:56 +02:00
|
|
|
Initialization vectors should be unpredictable and unique; ideally, they will be
|
|
|
|
cryptographically random. They do not have to be secret: IVs are typically just
|
|
|
|
added to ciphertext messages unencrypted. It may sound contradictory that
|
|
|
|
something has to be unpredictable and unique, but does not have to be secret;
|
2019-10-25 00:19:07 +02:00
|
|
|
remember that an attacker must not be able to predict ahead of time what a
|
|
|
|
given IV will be.
|
2018-04-04 22:47:56 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createDecipher(algorithm, password[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2018-03-02 18:53:46 +01:00
|
|
|
deprecated: v10.0.0
|
2018-06-14 15:18:14 +02:00
|
|
|
changes:
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
- version: v10.10.0
|
2018-06-14 15:18:14 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/21447
|
|
|
|
description: Ciphers in OCB mode are now supported.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2018-03-14 01:54:42 +01:00
|
|
|
|
|
|
|
> Stability: 0 - Deprecated: Use [`crypto.createDecipheriv()`][] instead.
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
|
|
|
* `password` {string | Buffer | TypedArray | DataView}
|
|
|
|
* `options` {Object} [`stream.transform` options][]
|
|
|
|
* Returns: {Decipher}
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Creates and returns a `Decipher` object that uses the given `algorithm` and
|
2017-12-18 13:22:08 +01:00
|
|
|
`password` (key).
|
|
|
|
|
|
|
|
The `options` argument controls stream behavior and is optional except when a
|
2018-06-14 15:18:14 +02:00
|
|
|
cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the
|
2017-12-18 13:22:08 +01:00
|
|
|
`authTagLength` option is required and specifies the length of the
|
|
|
|
authentication tag in bytes, see [CCM mode][].
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The implementation of `crypto.createDecipher()` derives keys using the OpenSSL
|
|
|
|
function [`EVP_BytesToKey`][] with the digest algorithm set to MD5, one
|
|
|
|
iteration, and no salt. The lack of salt allows dictionary attacks as the same
|
|
|
|
password always creates the same key. The low iteration count and
|
|
|
|
non-cryptographically secure hash algorithm allow passwords to be tested very
|
|
|
|
rapidly.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2018-05-18 11:05:20 +02:00
|
|
|
In line with OpenSSL's recommendation to use a more modern algorithm instead of
|
2015-12-27 05:54:01 +01:00
|
|
|
[`EVP_BytesToKey`][] it is recommended that developers derive a key and IV on
|
2018-05-18 11:05:20 +02:00
|
|
|
their own using [`crypto.scrypt()`][] and to use [`crypto.createDecipheriv()`][]
|
2015-12-27 05:54:01 +01:00
|
|
|
to create the `Decipher` object.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createDecipheriv(algorithm, key, iv[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2018-02-07 16:20:21 +01:00
|
|
|
changes:
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: The `key` argument can now be a `KeyObject`.
|
2020-04-24 18:43:06 +02:00
|
|
|
- version:
|
|
|
|
- v11.2.0
|
|
|
|
- v10.17.0
|
2018-11-04 05:04:57 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24081
|
|
|
|
description: The cipher `chacha20-poly1305` is now supported.
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
- version: v10.10.0
|
2018-06-14 15:18:14 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/21447
|
|
|
|
description: Ciphers in OCB mode are now supported.
|
2018-05-14 20:01:36 +02:00
|
|
|
- version: v10.2.0
|
2018-04-13 18:02:46 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/20039
|
|
|
|
description: The `authTagLength` option can now be used to restrict accepted
|
|
|
|
GCM authentication tag lengths.
|
2018-03-18 14:45:41 +01:00
|
|
|
- version: v9.9.0
|
2018-02-07 16:20:21 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/18644
|
|
|
|
description: The `iv` parameter may now be `null` for ciphers which do not
|
|
|
|
need an initialization vector.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
2019-09-24 09:50:36 +02:00
|
|
|
* `key` {string | Buffer | TypedArray | DataView | KeyObject}
|
2019-09-24 01:28:17 +02:00
|
|
|
* `iv` {string | Buffer | TypedArray | DataView | null}
|
2018-07-12 19:48:11 +02:00
|
|
|
* `options` {Object} [`stream.transform` options][]
|
|
|
|
* Returns: {Decipher}
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Creates and returns a `Decipher` object that uses the given `algorithm`, `key`
|
2017-12-18 13:22:08 +01:00
|
|
|
and initialization vector (`iv`).
|
|
|
|
|
|
|
|
The `options` argument controls stream behavior and is optional except when a
|
2018-06-14 15:18:14 +02:00
|
|
|
cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the
|
2017-12-18 13:22:08 +01:00
|
|
|
`authTagLength` option is required and specifies the length of the
|
2018-04-13 18:02:46 +02:00
|
|
|
authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength`
|
|
|
|
option is not required but can be used to restrict accepted authentication tags
|
|
|
|
to those with the specified length.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
|
2018-05-03 17:07:19 +02:00
|
|
|
recent OpenSSL releases, `openssl list -cipher-algorithms`
|
|
|
|
(`openssl list-cipher-algorithms` for older versions of OpenSSL) will
|
|
|
|
display the available cipher algorithms.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
The `key` is the raw key used by the `algorithm` and `iv` is an
|
2018-02-08 19:43:05 +01:00
|
|
|
[initialization vector][]. Both arguments must be `'utf8'` encoded strings,
|
2018-09-20 19:53:44 +02:00
|
|
|
[Buffers][`Buffer`], `TypedArray`, or `DataView`s. The `key` may optionally be
|
|
|
|
a [`KeyObject`][] of type `secret`. If the cipher does not need
|
2018-02-07 16:20:21 +01:00
|
|
|
an initialization vector, `iv` may be `null`.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2018-04-04 22:47:56 +02:00
|
|
|
Initialization vectors should be unpredictable and unique; ideally, they will be
|
|
|
|
cryptographically random. They do not have to be secret: IVs are typically just
|
|
|
|
added to ciphertext messages unencrypted. It may sound contradictory that
|
|
|
|
something has to be unpredictable and unique, but does not have to be secret;
|
2019-10-25 00:19:07 +02:00
|
|
|
remember that an attacker must not be able to predict ahead of time what a given
|
|
|
|
IV will be.
|
2018-04-04 22:47:56 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.12
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
2017-03-16 04:26:14 +01:00
|
|
|
- version: v8.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12223
|
2017-04-05 00:59:30 +02:00
|
|
|
description: The `prime` argument can be any `TypedArray` or `DataView` now.
|
2017-03-16 04:26:14 +01:00
|
|
|
- version: v8.0.0
|
2017-03-22 07:34:55 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/11983
|
|
|
|
description: The `prime` argument can be a `Uint8Array` now.
|
2017-02-21 23:38:44 +01:00
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
|
|
|
description: The default for the encoding parameters changed
|
|
|
|
from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `prime` {string | Buffer | TypedArray | DataView}
|
2018-11-07 21:27:47 +01:00
|
|
|
* `primeEncoding` {string} The [encoding][] of the `prime` string.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `generator` {number | string | Buffer | TypedArray | DataView} **Default:**
|
2018-04-02 03:44:32 +02:00
|
|
|
`2`
|
2018-11-07 21:27:47 +01:00
|
|
|
* `generatorEncoding` {string} The [encoding][] of the `generator` string.
|
2018-10-31 09:53:38 +01:00
|
|
|
* Returns: {DiffieHellman}
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Creates a `DiffieHellman` key exchange object using the supplied `prime` and an
|
2014-02-18 02:57:08 +01:00
|
|
|
optional specific `generator`.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `generator` argument can be a number, string, or [`Buffer`][]. If
|
|
|
|
`generator` is not specified, the value `2` is used.
|
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
If `primeEncoding` is specified, `prime` is expected to be a string; otherwise
|
2017-04-05 00:59:30 +02:00
|
|
|
a [`Buffer`][], `TypedArray`, or `DataView` is expected.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2017-06-06 11:00:32 +02:00
|
|
|
If `generatorEncoding` is specified, `generator` is expected to be a string;
|
2017-04-05 00:59:30 +02:00
|
|
|
otherwise a number, [`Buffer`][], `TypedArray`, or `DataView` is expected.
|
2012-02-27 20:08:02 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createDiffieHellman(primeLength[, generator])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `primeLength` {number}
|
2019-12-28 16:27:56 +01:00
|
|
|
* `generator` {number} **Default:** `2`
|
2018-10-31 09:53:38 +01:00
|
|
|
* Returns: {DiffieHellman}
|
2014-02-18 02:57:08 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Creates a `DiffieHellman` key exchange object and generates a prime of
|
2017-06-06 11:00:32 +02:00
|
|
|
`primeLength` bits using an optional specific numeric `generator`.
|
2015-12-27 05:54:01 +01:00
|
|
|
If `generator` is not specified, the value `2` is used.
|
2014-02-18 02:57:08 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createDiffieHellmanGroup(name)`
|
2019-07-07 09:33:18 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
|
|
|
-->
|
|
|
|
|
|
|
|
* `name` {string}
|
2019-09-24 22:27:37 +02:00
|
|
|
* Returns: {DiffieHellmanGroup}
|
2019-07-07 09:33:18 +02:00
|
|
|
|
|
|
|
An alias for [`crypto.getDiffieHellman()`][]
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createECDH(curveName)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `curveName` {string}
|
2018-10-31 09:53:38 +01:00
|
|
|
* Returns: {ECDH}
|
2014-02-18 02:57:08 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Creates an Elliptic Curve Diffie-Hellman (`ECDH`) key exchange object using a
|
2017-06-06 11:00:32 +02:00
|
|
|
predefined curve specified by the `curveName` string. Use
|
2015-12-27 05:54:01 +01:00
|
|
|
[`crypto.getCurves()`][] to obtain a list of available curve names. On recent
|
|
|
|
OpenSSL releases, `openssl ecparam -list_curves` will also display the name
|
|
|
|
and description of each available elliptic curve.
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createHash(algorithm[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
2019-07-19 02:44:31 +02:00
|
|
|
changes:
|
2019-08-06 15:06:33 +02:00
|
|
|
- version: v12.8.0
|
2019-07-19 02:44:31 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/28805
|
|
|
|
description: The `outputLength` option was added for XOF hash functions.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
|
|
|
* `options` {Object} [`stream.transform` options][]
|
|
|
|
* Returns: {Hash}
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Creates and returns a `Hash` object that can be used to generate hash digests
|
2017-08-15 22:16:14 +02:00
|
|
|
using the given `algorithm`. Optional `options` argument controls stream
|
2019-07-19 02:44:31 +02:00
|
|
|
behavior. For XOF hash functions such as `'shake256'`, the `outputLength` option
|
|
|
|
can be used to specify the desired output length in bytes.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
The `algorithm` is dependent on the available algorithms supported by the
|
|
|
|
version of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc.
|
2018-04-29 12:23:29 +02:00
|
|
|
On recent releases of OpenSSL, `openssl list -digest-algorithms`
|
|
|
|
(`openssl list-message-digest-algorithms` for older versions of OpenSSL) will
|
2015-12-27 05:54:01 +01:00
|
|
|
display the available digest algorithms.
|
|
|
|
|
|
|
|
Example: generating the sha256 sum of a file
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const filename = process.argv[2];
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const hash = crypto.createHash('sha256');
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const input = fs.createReadStream(filename);
|
|
|
|
input.on('readable', () => {
|
2019-01-07 15:37:34 +01:00
|
|
|
// Only one element is going to be produced by the
|
|
|
|
// hash stream.
|
2017-01-20 02:42:50 +01:00
|
|
|
const data = input.read();
|
2016-01-17 18:39:07 +01:00
|
|
|
if (data)
|
|
|
|
hash.update(data);
|
|
|
|
else {
|
|
|
|
console.log(`${hash.digest('hex')} ${filename}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2015-11-04 17:23:03 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createHmac(algorithm, key[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2018-09-20 19:53:44 +02:00
|
|
|
changes:
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: The `key` argument can now be a `KeyObject`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
2018-09-20 19:53:44 +02:00
|
|
|
* `key` {string | Buffer | TypedArray | DataView | KeyObject}
|
2018-07-12 19:48:11 +02:00
|
|
|
* `options` {Object} [`stream.transform` options][]
|
|
|
|
* Returns: {Hmac}
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Creates and returns an `Hmac` object that uses the given `algorithm` and `key`.
|
2017-08-15 22:16:14 +02:00
|
|
|
Optional `options` argument controls stream behavior.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `algorithm` is dependent on the available algorithms supported by the
|
|
|
|
version of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc.
|
2018-05-03 16:46:08 +02:00
|
|
|
On recent releases of OpenSSL, `openssl list -digest-algorithms`
|
|
|
|
(`openssl list-message-digest-algorithms` for older versions of OpenSSL) will
|
2015-12-27 05:54:01 +01:00
|
|
|
display the available digest algorithms.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
The `key` is the HMAC key used to generate the cryptographic HMAC hash. If it is
|
|
|
|
a [`KeyObject`][], its type must be `secret`.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Example: generating the sha256 HMAC of a file
|
2012-10-22 19:37:20 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const filename = process.argv[2];
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const hmac = crypto.createHmac('sha256', 'a secret');
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const input = fs.createReadStream(filename);
|
|
|
|
input.on('readable', () => {
|
2019-01-07 15:37:34 +01:00
|
|
|
// Only one element is going to be produced by the
|
|
|
|
// hash stream.
|
2017-01-20 02:42:50 +01:00
|
|
|
const data = input.read();
|
2016-01-17 18:39:07 +01:00
|
|
|
if (data)
|
|
|
|
hmac.update(data);
|
|
|
|
else {
|
|
|
|
console.log(`${hmac.digest('hex')} ${filename}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createPrivateKey(key)`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `key` {Object | string | Buffer}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `key`: {string | Buffer} The key material, either in PEM or DER format.
|
|
|
|
* `format`: {string} Must be `'pem'` or `'der'`. **Default:** `'pem'`.
|
|
|
|
* `type`: {string} Must be `'pkcs1'`, `'pkcs8'` or `'sec1'`. This option is
|
2018-09-20 19:53:44 +02:00
|
|
|
required only if the `format` is `'der'` and ignored if it is `'pem'`.
|
2019-09-13 06:22:29 +02:00
|
|
|
* `passphrase`: {string | Buffer} The passphrase to use for decryption.
|
2018-09-20 19:53:44 +02:00
|
|
|
* Returns: {KeyObject}
|
|
|
|
|
|
|
|
Creates and returns a new key object containing a private key. If `key` is a
|
|
|
|
string or `Buffer`, `format` is assumed to be `'pem'`; otherwise, `key`
|
|
|
|
must be an object with the properties described above.
|
|
|
|
|
2019-03-30 00:19:39 +01:00
|
|
|
If the private key is encrypted, a `passphrase` must be specified. The length
|
|
|
|
of the passphrase is limited to 1024 bytes.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createPublicKey(key)`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2018-12-25 13:13:52 +01:00
|
|
|
changes:
|
2019-03-27 22:43:03 +01:00
|
|
|
- version: v11.13.0
|
2019-01-26 13:28:55 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26278
|
|
|
|
description: The `key` argument can now be a `KeyObject` with type
|
|
|
|
`private`.
|
2019-01-16 15:55:55 +01:00
|
|
|
- version: v11.7.0
|
2018-12-25 13:13:52 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/25217
|
|
|
|
description: The `key` argument can now be a private key.
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2019-01-26 13:28:55 +01:00
|
|
|
* `key` {Object | string | Buffer | KeyObject}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `key`: {string | Buffer}
|
|
|
|
* `format`: {string} Must be `'pem'` or `'der'`. **Default:** `'pem'`.
|
|
|
|
* `type`: {string} Must be `'pkcs1'` or `'spki'`. This option is required
|
2018-09-20 19:53:44 +02:00
|
|
|
only if the `format` is `'der'`.
|
|
|
|
* Returns: {KeyObject}
|
|
|
|
|
|
|
|
Creates and returns a new key object containing a public key. If `key` is a
|
2019-01-26 13:28:55 +01:00
|
|
|
string or `Buffer`, `format` is assumed to be `'pem'`; if `key` is a `KeyObject`
|
|
|
|
with type `'private'`, the public key is derived from the given private key;
|
|
|
|
otherwise, `key` must be an object with the properties described above.
|
2018-09-20 19:53:44 +02:00
|
|
|
|
2018-12-20 00:11:57 +01:00
|
|
|
If the format is `'pem'`, the `'key'` may also be an X.509 certificate.
|
|
|
|
|
2018-12-25 13:13:52 +01:00
|
|
|
Because public keys can be derived from private keys, a private key may be
|
|
|
|
passed instead of a public key. In that case, this function behaves as if
|
|
|
|
[`crypto.createPrivateKey()`][] had been called, except that the type of the
|
2019-01-26 13:28:55 +01:00
|
|
|
returned `KeyObject` will be `'public'` and that the private key cannot be
|
|
|
|
extracted from the returned `KeyObject`. Similarly, if a `KeyObject` with type
|
|
|
|
`'private'` is given, a new `KeyObject` with type `'public'` will be returned
|
|
|
|
and it will be impossible to extract the private key from the returned object.
|
2018-12-25 13:13:52 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createSecretKey(key)`
|
2018-09-20 19:53:44 +02:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `key` {Buffer}
|
|
|
|
* Returns: {KeyObject}
|
|
|
|
|
|
|
|
Creates and returns a new key object containing a secret key for symmetric
|
|
|
|
encryption or `Hmac`.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createSign(algorithm[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
|
|
|
* `options` {Object} [`stream.Writable` options][]
|
|
|
|
* Returns: {Sign}
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2020-06-21 01:46:33 +02:00
|
|
|
Creates and returns a `Sign` object that uses the given `algorithm`. Use
|
2019-01-11 20:43:58 +01:00
|
|
|
[`crypto.getHashes()`][] to obtain the names of the available digest algorithms.
|
|
|
|
Optional `options` argument controls the `stream.Writable` behavior.
|
|
|
|
|
|
|
|
In some cases, a `Sign` instance can be created using the name of a signature
|
|
|
|
algorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use
|
|
|
|
the corresponding digest algorithm. This does not work for all signature
|
|
|
|
algorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest
|
|
|
|
algorithm names.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.createVerify(algorithm[, options])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `algorithm` {string}
|
|
|
|
* `options` {Object} [`stream.Writable` options][]
|
|
|
|
* Returns: {Verify}
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2016-10-12 01:20:51 +02:00
|
|
|
Creates and returns a `Verify` object that uses the given algorithm.
|
|
|
|
Use [`crypto.getHashes()`][] to obtain an array of names of the available
|
2017-08-15 22:16:14 +02:00
|
|
|
signing algorithms. Optional `options` argument controls the
|
|
|
|
`stream.Writable` behavior.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2019-01-11 20:43:58 +01:00
|
|
|
In some cases, a `Verify` instance can be created using the name of a signature
|
|
|
|
algorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use
|
|
|
|
the corresponding digest algorithm. This does not work for all signature
|
|
|
|
algorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest
|
|
|
|
algorithm names.
|
|
|
|
|
2020-01-03 12:01:34 +01:00
|
|
|
### `crypto.diffieHellman(options)`
|
|
|
|
<!-- YAML
|
2020-05-01 14:43:14 +02:00
|
|
|
added:
|
|
|
|
- v13.9.0
|
|
|
|
- v12.17.0
|
2020-01-03 12:01:34 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `options`: {Object}
|
|
|
|
* `privateKey`: {KeyObject}
|
|
|
|
* `publicKey`: {KeyObject}
|
|
|
|
* Returns: {Buffer}
|
|
|
|
|
|
|
|
Computes the Diffie-Hellman secret based on a `privateKey` and a `publicKey`.
|
|
|
|
Both keys must have the same `asymmetricKeyType`, which must be one of `'dh'`
|
|
|
|
(for Diffie-Hellman), `'ec'` (for ECDH), `'x448'`, or `'x25519'` (for ECDH-ES).
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.generateKeyPair(type, options, callback)`
|
2018-09-02 17:00:01 +02:00
|
|
|
<!-- YAML
|
2018-10-07 14:09:45 +02:00
|
|
|
added: v10.12.0
|
2018-09-20 19:53:44 +02:00
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
- version:
|
|
|
|
- v13.9.0
|
|
|
|
- v12.17.0
|
2019-12-31 02:12:36 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/31178
|
|
|
|
description: Add support for Diffie-Hellman.
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-19 12:09:01 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26774
|
|
|
|
description: Add ability to generate X25519 and X448 key pairs.
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-10 00:51:56 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26554
|
|
|
|
description: Add ability to generate Ed25519 and Ed448 key pairs.
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: The `generateKeyPair` and `generateKeyPairSync` functions now
|
|
|
|
produce key objects if no encoding was specified.
|
2018-09-02 17:00:01 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2019-03-19 12:09:01 +01:00
|
|
|
* `type`: {string} Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`,
|
2019-12-31 02:12:36 +01:00
|
|
|
`'x25519'`, `'x448'`, or `'dh'`.
|
2018-09-02 17:00:01 +02:00
|
|
|
* `options`: {Object}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `modulusLength`: {number} Key size in bits (RSA, DSA).
|
|
|
|
* `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`.
|
|
|
|
* `divisorLength`: {number} Size of `q` in bits (DSA).
|
|
|
|
* `namedCurve`: {string} Name of the curve to use (EC).
|
2019-12-31 02:12:36 +01:00
|
|
|
* `prime`: {Buffer} The prime parameter (DH).
|
|
|
|
* `primeLength`: {number} Prime length in bits (DH).
|
|
|
|
* `generator`: {number} Custom generator (DH). **Default:** `2`.
|
|
|
|
* `groupName`: {string} Diffie-Hellman group name (DH). See
|
|
|
|
[`crypto.getDiffieHellman()`][].
|
2019-09-13 06:22:29 +02:00
|
|
|
* `publicKeyEncoding`: {Object} See [`keyObject.export()`][].
|
|
|
|
* `privateKeyEncoding`: {Object} See [`keyObject.export()`][].
|
2018-09-02 17:00:01 +02:00
|
|
|
* `callback`: {Function}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `err`: {Error}
|
|
|
|
* `publicKey`: {string | Buffer | KeyObject}
|
|
|
|
* `privateKey`: {string | Buffer | KeyObject}
|
2018-09-02 17:00:01 +02:00
|
|
|
|
2019-12-31 02:12:36 +01:00
|
|
|
Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519,
|
|
|
|
Ed448, X25519, X448, and DH are currently supported.
|
2018-09-02 17:00:01 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function
|
|
|
|
behaves as if [`keyObject.export()`][] had been called on its result. Otherwise,
|
2019-10-02 06:31:57 +02:00
|
|
|
the respective part of the key is returned as a [`KeyObject`][].
|
2018-09-20 19:53:44 +02:00
|
|
|
|
2018-09-02 17:00:01 +02:00
|
|
|
It is recommended to encode public keys as `'spki'` and private keys as
|
2018-09-20 19:53:44 +02:00
|
|
|
`'pkcs8'` with encryption for long-term storage:
|
2018-09-02 17:00:01 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { generateKeyPair } = require('crypto');
|
|
|
|
generateKeyPair('rsa', {
|
|
|
|
modulusLength: 4096,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
|
|
|
format: 'pem'
|
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: 'aes-256-cbc',
|
|
|
|
passphrase: 'top secret'
|
|
|
|
}
|
|
|
|
}, (err, publicKey, privateKey) => {
|
|
|
|
// Handle errors and use the generated key pair.
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
On completion, `callback` will be called with `err` set to `undefined` and
|
2018-09-20 19:53:44 +02:00
|
|
|
`publicKey` / `privateKey` representing the generated key pair.
|
2018-09-02 17:00:01 +02:00
|
|
|
|
2018-09-10 22:36:14 +02:00
|
|
|
If this method is invoked as its [`util.promisify()`][]ed version, it returns
|
|
|
|
a `Promise` for an `Object` with `publicKey` and `privateKey` properties.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.generateKeyPairSync(type, options)`
|
2018-09-02 17:00:01 +02:00
|
|
|
<!-- YAML
|
2018-10-07 14:09:45 +02:00
|
|
|
added: v10.12.0
|
2018-09-20 19:53:44 +02:00
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
- version:
|
|
|
|
- v13.9.0
|
|
|
|
- v12.17.0
|
2019-12-31 02:12:36 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/31178
|
|
|
|
description: Add support for Diffie-Hellman.
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-03-10 00:51:56 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/26554
|
|
|
|
description: Add ability to generate Ed25519 and Ed448 key pairs.
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: The `generateKeyPair` and `generateKeyPairSync` functions now
|
|
|
|
produce key objects if no encoding was specified.
|
2018-09-02 17:00:01 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2019-12-31 02:12:36 +01:00
|
|
|
* `type`: {string} Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`,
|
|
|
|
`'x25519'`, `'x448'`, or `'dh'`.
|
2018-09-02 17:00:01 +02:00
|
|
|
* `options`: {Object}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `modulusLength`: {number} Key size in bits (RSA, DSA).
|
|
|
|
* `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`.
|
|
|
|
* `divisorLength`: {number} Size of `q` in bits (DSA).
|
|
|
|
* `namedCurve`: {string} Name of the curve to use (EC).
|
2019-12-31 02:12:36 +01:00
|
|
|
* `prime`: {Buffer} The prime parameter (DH).
|
|
|
|
* `primeLength`: {number} Prime length in bits (DH).
|
|
|
|
* `generator`: {number} Custom generator (DH). **Default:** `2`.
|
|
|
|
* `groupName`: {string} Diffie-Hellman group name (DH). See
|
|
|
|
[`crypto.getDiffieHellman()`][].
|
2019-09-13 06:22:29 +02:00
|
|
|
* `publicKeyEncoding`: {Object} See [`keyObject.export()`][].
|
|
|
|
* `privateKeyEncoding`: {Object} See [`keyObject.export()`][].
|
2018-09-02 17:00:01 +02:00
|
|
|
* Returns: {Object}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `publicKey`: {string | Buffer | KeyObject}
|
|
|
|
* `privateKey`: {string | Buffer | KeyObject}
|
2018-09-02 17:00:01 +02:00
|
|
|
|
2019-12-31 02:12:36 +01:00
|
|
|
Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519,
|
|
|
|
Ed448, X25519, X448, and DH are currently supported.
|
2018-09-02 17:00:01 +02:00
|
|
|
|
2019-01-03 18:59:34 +01:00
|
|
|
If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function
|
|
|
|
behaves as if [`keyObject.export()`][] had been called on its result. Otherwise,
|
2019-10-02 06:31:57 +02:00
|
|
|
the respective part of the key is returned as a [`KeyObject`][].
|
2019-01-03 18:59:34 +01:00
|
|
|
|
|
|
|
When encoding public keys, it is recommended to use `'spki'`. When encoding
|
2020-05-01 07:19:45 +02:00
|
|
|
private keys, it is recommended to use `'pkcs8'` with a strong passphrase,
|
|
|
|
and to keep the passphrase confidential.
|
2018-09-02 17:00:01 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { generateKeyPairSync } = require('crypto');
|
|
|
|
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
|
|
|
|
modulusLength: 4096,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
|
|
|
format: 'pem'
|
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: 'aes-256-cbc',
|
|
|
|
passphrase: 'top secret'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
The return value `{ publicKey, privateKey }` represents the generated key pair.
|
|
|
|
When PEM encoding was selected, the respective key will be a string, otherwise
|
|
|
|
it will be a buffer containing the data encoded as DER.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.getCiphers()`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {string[]} An array with the names of the supported cipher
|
2018-04-06 17:28:20 +02:00
|
|
|
algorithms.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const ciphers = crypto.getCiphers();
|
|
|
|
console.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...]
|
|
|
|
```
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.getCurves()`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v2.3.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {string[]} An array with the names of the supported elliptic curves.
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const curves = crypto.getCurves();
|
2017-01-20 02:42:50 +01:00
|
|
|
console.log(curves); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.getDiffieHellman(groupName)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.5
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `groupName` {string}
|
2019-09-24 22:27:37 +02:00
|
|
|
* Returns: {DiffieHellmanGroup}
|
2012-01-22 19:24:37 +01:00
|
|
|
|
2019-09-24 22:27:37 +02:00
|
|
|
Creates a predefined `DiffieHellmanGroup` key exchange object. The
|
2015-07-27 09:10:57 +02:00
|
|
|
supported groups are: `'modp1'`, `'modp2'`, `'modp5'` (defined in
|
2015-11-14 04:21:49 +01:00
|
|
|
[RFC 2412][], but see [Caveats][]) and `'modp14'`, `'modp15'`,
|
2015-12-27 05:54:01 +01:00
|
|
|
`'modp16'`, `'modp17'`, `'modp18'` (defined in [RFC 3526][]). The
|
2015-11-14 04:21:49 +01:00
|
|
|
returned object mimics the interface of objects created by
|
2016-01-05 11:49:54 +01:00
|
|
|
[`crypto.createDiffieHellman()`][], but will not allow changing
|
2018-08-26 18:02:27 +02:00
|
|
|
the keys (with [`diffieHellman.setPublicKey()`][], for example). The
|
2015-12-27 05:54:01 +01:00
|
|
|
advantage of using this method is that the parties do not have to
|
|
|
|
generate nor exchange a group modulus beforehand, saving both processor
|
2015-07-27 09:10:57 +02:00
|
|
|
and communication time.
|
2012-01-22 19:24:37 +01:00
|
|
|
|
|
|
|
Example (obtaining a shared secret):
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const alice = crypto.getDiffieHellman('modp14');
|
|
|
|
const bob = crypto.getDiffieHellman('modp14');
|
2012-01-22 19:24:37 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
alice.generateKeys();
|
|
|
|
bob.generateKeys();
|
2012-01-22 19:24:37 +01:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
|
|
|
|
const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
|
2014-08-27 16:01:01 +02:00
|
|
|
|
2017-01-20 02:42:50 +01:00
|
|
|
/* aliceSecret and bobSecret should be the same */
|
|
|
|
console.log(aliceSecret === bobSecret);
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2014-08-27 16:01:01 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.getFips()`
|
2018-01-24 01:32:19 +01:00
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
added: v10.0.0
|
2018-01-24 01:32:19 +01:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2020-03-31 12:51:47 +02:00
|
|
|
* Returns: {number} `1` if and only if a FIPS compliant crypto provider is
|
2020-04-02 22:22:08 +02:00
|
|
|
currently in use, `0` otherwise. A future semver-major release may change
|
|
|
|
the return type of this API to a {boolean}.
|
2018-01-24 01:32:19 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.getHashes()`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {string[]} An array of the names of the supported hash algorithms,
|
2019-01-11 20:43:58 +01:00
|
|
|
such as `'RSA-SHA256'`. Hash algorithms are also called "digest" algorithms.
|
2014-08-27 16:01:01 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const hashes = crypto.getHashes();
|
2017-01-20 02:42:50 +01:00
|
|
|
console.log(hashes); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-01-22 19:24:37 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.5
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
- version: v14.0.0
|
2019-11-21 16:00:03 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/30578
|
|
|
|
description: The `iterations` parameter is now restricted to positive
|
|
|
|
values. Earlier releases treated other values as one.
|
2017-03-16 04:26:14 +01:00
|
|
|
- version: v8.0.0
|
2017-02-22 00:10:25 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/11305
|
|
|
|
description: The `digest` parameter is always required now.
|
2017-02-21 23:38:44 +01:00
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/4047
|
|
|
|
description: Calling this function without passing the `digest` parameter
|
|
|
|
is deprecated now and will emit a warning.
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
|
|
|
description: The default encoding for `password` if it is a string changed
|
|
|
|
from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `password` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `salt` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `iterations` {number}
|
|
|
|
* `keylen` {number}
|
|
|
|
* `digest` {string}
|
|
|
|
* `callback` {Function}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `err` {Error}
|
|
|
|
* `derivedKey` {Buffer}
|
2015-12-27 05:54:01 +01:00
|
|
|
|
|
|
|
Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
|
2016-02-15 01:40:53 +01:00
|
|
|
implementation. A selected HMAC digest algorithm specified by `digest` is
|
2015-12-27 05:54:01 +01:00
|
|
|
applied to derive a key of the requested byte length (`keylen`) from the
|
2015-11-27 11:41:38 +01:00
|
|
|
`password`, `salt` and `iterations`.
|
2011-08-11 11:40:55 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The supplied `callback` function is called with two arguments: `err` and
|
2017-10-03 05:56:49 +02:00
|
|
|
`derivedKey`. If an error occurs while deriving the key, `err` will be set;
|
2018-04-29 19:46:41 +02:00
|
|
|
otherwise `err` will be `null`. By default, the successfully generated
|
2017-10-03 05:56:49 +02:00
|
|
|
`derivedKey` will be passed to the callback as a [`Buffer`][]. An error will be
|
|
|
|
thrown if any of the input arguments specify invalid values or types.
|
2011-09-22 22:12:10 +02:00
|
|
|
|
2018-09-14 13:02:44 +02:00
|
|
|
If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
|
2019-02-26 07:30:23 +01:00
|
|
|
please specify a `digest` explicitly.
|
2018-09-14 10:13:14 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `iterations` argument must be a number set as high as possible. The
|
|
|
|
higher the number of iterations, the more secure the derived key will be,
|
|
|
|
but will take a longer amount of time to complete.
|
2015-10-09 14:58:44 +02:00
|
|
|
|
2018-05-18 11:05:20 +02:00
|
|
|
The `salt` should be as unique as possible. It is recommended that a salt is
|
|
|
|
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
|
2015-10-09 14:58:44 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2017-10-14 16:30:41 +02:00
|
|
|
crypto.pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
if (err) throw err;
|
2017-10-14 16:30:41 +02:00
|
|
|
console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
```
|
2013-11-21 11:29:07 +01:00
|
|
|
|
2018-01-24 00:58:14 +01:00
|
|
|
The `crypto.DEFAULT_ENCODING` property can be used to change the way the
|
|
|
|
`derivedKey` is passed to the callback. This property, however, has been
|
|
|
|
deprecated and use should be avoided.
|
2017-10-03 05:56:49 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
crypto.DEFAULT_ENCODING = 'hex';
|
|
|
|
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log(derivedKey); // '3745e48...aa39b34'
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
An array of supported digest functions can be retrieved using
|
|
|
|
[`crypto.getHashes()`][].
|
2013-11-21 11:29:07 +01:00
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
This API uses libuv's threadpool, which can have surprising and
|
|
|
|
negative performance implications for some applications; see the
|
2017-08-23 23:59:06 +02:00
|
|
|
[`UV_THREADPOOL_SIZE`][] documentation for more information.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
2017-02-21 23:38:44 +01:00
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
- version: v14.0.0
|
2019-11-21 16:00:03 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/30578
|
|
|
|
description: The `iterations` parameter is now restricted to positive
|
|
|
|
values. Earlier releases treated other values as one.
|
2017-02-21 23:38:44 +01:00
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/4047
|
|
|
|
description: Calling this function without passing the `digest` parameter
|
|
|
|
is deprecated now and will emit a warning.
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5522
|
|
|
|
description: The default encoding for `password` if it is a string changed
|
|
|
|
from `binary` to `utf8`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `password` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `salt` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `iterations` {number}
|
|
|
|
* `keylen` {number}
|
|
|
|
* `digest` {string}
|
|
|
|
* Returns: {Buffer}
|
2012-10-13 02:36:18 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
|
2016-02-15 01:40:53 +01:00
|
|
|
implementation. A selected HMAC digest algorithm specified by `digest` is
|
2015-12-27 05:54:01 +01:00
|
|
|
applied to derive a key of the requested byte length (`keylen`) from the
|
2015-11-27 11:41:38 +01:00
|
|
|
`password`, `salt` and `iterations`.
|
2012-10-13 02:36:18 +02:00
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
If an error occurs an `Error` will be thrown, otherwise the derived key will be
|
2015-12-27 05:54:01 +01:00
|
|
|
returned as a [`Buffer`][].
|
|
|
|
|
2018-09-14 13:02:44 +02:00
|
|
|
If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
|
2019-02-26 07:30:23 +01:00
|
|
|
please specify a `digest` explicitly.
|
2018-09-14 10:13:14 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `iterations` argument must be a number set as high as possible. The
|
|
|
|
higher the number of iterations, the more secure the derived key will be,
|
|
|
|
but will take a longer amount of time to complete.
|
|
|
|
|
2018-05-18 11:05:20 +02:00
|
|
|
The `salt` should be as unique as possible. It is recommended that a salt is
|
|
|
|
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
2017-10-14 16:30:41 +02:00
|
|
|
const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
|
|
|
|
console.log(key.toString('hex')); // '3745e48...08d59ae'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2018-01-24 00:58:14 +01:00
|
|
|
The `crypto.DEFAULT_ENCODING` property may be used to change the way the
|
2018-06-04 20:58:47 +02:00
|
|
|
`derivedKey` is returned. This property, however, is deprecated and use
|
2018-01-24 00:58:14 +01:00
|
|
|
should be avoided.
|
2017-10-03 05:56:49 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
crypto.DEFAULT_ENCODING = 'hex';
|
|
|
|
const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
|
|
|
|
console.log(key); // '3745e48...aa39b34'
|
|
|
|
```
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
An array of supported digest functions can be retrieved using
|
|
|
|
[`crypto.getHashes()`][].
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.privateDecrypt(privateKey, buffer)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
2018-09-20 19:53:44 +02:00
|
|
|
changes:
|
2019-09-25 00:45:45 +02:00
|
|
|
- version: v12.11.0
|
2019-09-08 04:41:04 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/29489
|
|
|
|
description: The `oaepLabel` option was added.
|
2019-08-19 21:14:22 +02:00
|
|
|
- version: v12.9.0
|
2019-06-21 16:37:06 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/28335
|
|
|
|
description: The `oaepHash` option was added.
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: This function now supports key objects.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `privateKey` {Object | string | Buffer | KeyObject}
|
2020-03-18 09:57:30 +01:00
|
|
|
* `oaepHash` {string} The hash function to use for OAEP padding and MGF1.
|
2019-06-21 16:37:06 +02:00
|
|
|
**Default:** `'sha1'`
|
2020-02-17 10:50:58 +01:00
|
|
|
* `oaepLabel` {Buffer | TypedArray | DataView} The label to use for OAEP
|
|
|
|
padding. If not specified, no label is used.
|
2019-09-13 06:22:29 +02:00
|
|
|
* `padding` {crypto.constants} An optional padding value defined in
|
2017-03-11 07:30:48 +01:00
|
|
|
`crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING`,
|
2018-05-28 03:33:48 +02:00
|
|
|
`crypto.constants.RSA_PKCS1_PADDING`, or
|
|
|
|
`crypto.constants.RSA_PKCS1_OAEP_PADDING`.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `buffer` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Buffer} A new `Buffer` with the decrypted content.
|
2013-03-03 22:11:08 +01:00
|
|
|
|
2018-09-12 15:25:43 +02:00
|
|
|
Decrypts `buffer` with `privateKey`. `buffer` was previously encrypted using
|
|
|
|
the corresponding public key, for example using [`crypto.publicEncrypt()`][].
|
2013-10-15 16:31:14 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
If `privateKey` is not a [`KeyObject`][], this function behaves as if
|
|
|
|
`privateKey` had been passed to [`crypto.createPrivateKey()`][]. If it is an
|
|
|
|
object, the `padding` property can be passed. Otherwise, this function uses
|
|
|
|
`RSA_PKCS1_OAEP_PADDING`.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.privateEncrypt(privateKey, buffer)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.1.0
|
2018-09-20 19:53:44 +02:00
|
|
|
changes:
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: This function now supports key objects.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `privateKey` {Object | string | Buffer | KeyObject}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `key` {string | Buffer | KeyObject} A PEM encoded private key.
|
|
|
|
* `passphrase` {string | Buffer} An optional passphrase for the private key.
|
|
|
|
* `padding` {crypto.constants} An optional padding value defined in
|
2017-03-11 07:30:48 +01:00
|
|
|
`crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING` or
|
2018-05-28 03:33:48 +02:00
|
|
|
`crypto.constants.RSA_PKCS1_PADDING`.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `buffer` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Buffer} A new `Buffer` with the encrypted content.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2018-09-12 15:25:43 +02:00
|
|
|
Encrypts `buffer` with `privateKey`. The returned data can be decrypted using
|
|
|
|
the corresponding public key, for example using [`crypto.publicDecrypt()`][].
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
If `privateKey` is not a [`KeyObject`][], this function behaves as if
|
|
|
|
`privateKey` had been passed to [`crypto.createPrivateKey()`][]. If it is an
|
|
|
|
object, the `padding` property can be passed. Otherwise, this function uses
|
|
|
|
`RSA_PKCS1_PADDING`.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.publicDecrypt(key, buffer)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.1.0
|
2018-09-20 19:53:44 +02:00
|
|
|
changes:
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: This function now supports key objects.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `key` {Object | string | Buffer | KeyObject}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `passphrase` {string | Buffer} An optional passphrase for the private key.
|
|
|
|
* `padding` {crypto.constants} An optional padding value defined in
|
2017-07-02 05:18:30 +02:00
|
|
|
`crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING` or
|
2018-05-28 03:33:48 +02:00
|
|
|
`crypto.constants.RSA_PKCS1_PADDING`.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `buffer` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Buffer} A new `Buffer` with the decrypted content.
|
2013-10-15 16:31:14 +02:00
|
|
|
|
2018-09-12 15:25:43 +02:00
|
|
|
Decrypts `buffer` with `key`.`buffer` was previously encrypted using
|
|
|
|
the corresponding private key, for example using [`crypto.privateEncrypt()`][].
|
2013-10-15 16:31:14 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
If `key` is not a [`KeyObject`][], this function behaves as if
|
|
|
|
`key` had been passed to [`crypto.createPublicKey()`][]. If it is an
|
|
|
|
object, the `padding` property can be passed. Otherwise, this function uses
|
|
|
|
`RSA_PKCS1_PADDING`.
|
2013-10-15 16:31:14 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Because RSA public keys can be derived from private keys, a private key may
|
|
|
|
be passed instead of a public key.
|
2013-10-15 16:31:14 +02:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.publicEncrypt(key, buffer)`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
2018-09-20 19:53:44 +02:00
|
|
|
changes:
|
2019-09-25 00:45:45 +02:00
|
|
|
- version: v12.11.0
|
2019-09-08 04:41:04 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/29489
|
|
|
|
description: The `oaepLabel` option was added.
|
2019-08-19 21:14:22 +02:00
|
|
|
- version: v12.9.0
|
2019-06-21 16:37:06 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/28335
|
|
|
|
description: The `oaepHash` option was added.
|
2018-12-22 00:13:09 +01:00
|
|
|
- version: v11.6.0
|
2018-09-20 19:53:44 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/24234
|
|
|
|
description: This function now supports key objects.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
* `key` {Object | string | Buffer | KeyObject}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `key` {string | Buffer | KeyObject} A PEM encoded public or private key.
|
2020-03-18 09:57:30 +01:00
|
|
|
* `oaepHash` {string} The hash function to use for OAEP padding and MGF1.
|
2019-06-21 16:37:06 +02:00
|
|
|
**Default:** `'sha1'`
|
2020-02-17 10:50:58 +01:00
|
|
|
* `oaepLabel` {Buffer | TypedArray | DataView} The label to use for OAEP
|
|
|
|
padding. If not specified, no label is used.
|
2019-09-13 06:22:29 +02:00
|
|
|
* `passphrase` {string | Buffer} An optional passphrase for the private key.
|
|
|
|
* `padding` {crypto.constants} An optional padding value defined in
|
2017-03-11 07:30:48 +01:00
|
|
|
`crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING`,
|
2018-05-28 03:33:48 +02:00
|
|
|
`crypto.constants.RSA_PKCS1_PADDING`, or
|
|
|
|
`crypto.constants.RSA_PKCS1_OAEP_PADDING`.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `buffer` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {Buffer} A new `Buffer` with the encrypted content.
|
2014-05-05 16:35:28 +02:00
|
|
|
|
2017-10-08 19:35:18 +02:00
|
|
|
Encrypts the content of `buffer` with `key` and returns a new
|
2018-09-12 15:25:43 +02:00
|
|
|
[`Buffer`][] with encrypted content. The returned data can be decrypted using
|
|
|
|
the corresponding private key, for example using [`crypto.privateDecrypt()`][].
|
2014-05-05 16:35:28 +02:00
|
|
|
|
2018-09-20 19:53:44 +02:00
|
|
|
If `key` is not a [`KeyObject`][], this function behaves as if
|
|
|
|
`key` had been passed to [`crypto.createPublicKey()`][]. If it is an
|
|
|
|
object, the `padding` property can be passed. Otherwise, this function uses
|
|
|
|
`RSA_PKCS1_OAEP_PADDING`.
|
2014-08-23 15:38:32 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Because RSA public keys can be derived from private keys, a private key may
|
|
|
|
be passed instead of a public key.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.randomBytes(size[, callback])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.8
|
2017-12-10 21:12:21 +01:00
|
|
|
changes:
|
|
|
|
- version: v9.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/16454
|
|
|
|
description: Passing `null` as the `callback` argument now throws
|
|
|
|
`ERR_INVALID_CALLBACK`.
|
2016-08-26 17:07:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `size` {number}
|
|
|
|
* `callback` {Function}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `err` {Error}
|
|
|
|
* `buf` {Buffer}
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer} if the `callback` function is not provided.
|
2014-08-23 15:38:32 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Generates cryptographically strong pseudo-random data. The `size` argument
|
|
|
|
is a number indicating the number of bytes to generate.
|
2014-05-05 16:35:28 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
If a `callback` function is provided, the bytes are generated asynchronously
|
|
|
|
and the `callback` function is invoked with two arguments: `err` and `buf`.
|
2018-04-29 19:46:41 +02:00
|
|
|
If an error occurs, `err` will be an `Error` object; otherwise it is `null`. The
|
2015-12-27 05:54:01 +01:00
|
|
|
`buf` argument is a [`Buffer`][] containing the generated bytes.
|
2014-05-05 16:35:28 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// Asynchronous
|
|
|
|
const crypto = require('crypto');
|
|
|
|
crypto.randomBytes(256, (err, buf) => {
|
|
|
|
if (err) throw err;
|
2016-02-15 16:49:00 +01:00
|
|
|
console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
```
|
2014-05-05 16:35:28 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
If the `callback` function is not provided, the random bytes are generated
|
|
|
|
synchronously and returned as a [`Buffer`][]. An error will be thrown if
|
|
|
|
there is a problem generating the bytes.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// Synchronous
|
|
|
|
const buf = crypto.randomBytes(256);
|
|
|
|
console.log(
|
2016-03-18 13:22:22 +01:00
|
|
|
`${buf.length} bytes of random data: ${buf.toString('hex')}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2014-08-23 15:38:32 +02:00
|
|
|
|
2017-08-23 23:04:33 +02:00
|
|
|
The `crypto.randomBytes()` method will not complete until there is
|
|
|
|
sufficient entropy available.
|
2015-12-27 05:54:01 +01:00
|
|
|
This should normally never take longer than a few milliseconds. The only time
|
|
|
|
when generating the random bytes may conceivably block for a longer period of
|
|
|
|
time is right after boot, when the whole system is still low on entropy.
|
2014-05-05 16:35:28 +02:00
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
This API uses libuv's threadpool, which can have surprising and
|
|
|
|
negative performance implications for some applications; see the
|
2017-08-23 23:59:06 +02:00
|
|
|
[`UV_THREADPOOL_SIZE`][] documentation for more information.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
The asynchronous version of `crypto.randomBytes()` is carried out in a single
|
|
|
|
threadpool request. To minimize threadpool task length variation, partition
|
|
|
|
large `randomBytes` requests when doing so as part of fulfilling a client
|
|
|
|
request.
|
2017-11-22 16:03:17 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.randomFillSync(buffer[, offset][, size])`
|
2016-12-10 00:13:14 +01:00
|
|
|
<!-- YAML
|
2018-12-03 18:13:42 +01:00
|
|
|
added:
|
|
|
|
- v7.10.0
|
|
|
|
- v6.13.0
|
2017-09-06 17:10:34 +02:00
|
|
|
changes:
|
2017-09-01 18:50:47 +02:00
|
|
|
- version: v9.0.0
|
2017-09-06 17:10:34 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/15231
|
2017-12-10 21:42:25 +01:00
|
|
|
description: The `buffer` argument may be any `TypedArray` or `DataView`.
|
2016-12-10 00:13:14 +01:00
|
|
|
-->
|
|
|
|
|
2017-12-10 21:42:25 +01:00
|
|
|
* `buffer` {Buffer|TypedArray|DataView} Must be supplied.
|
2018-04-02 03:44:32 +02:00
|
|
|
* `offset` {number} **Default:** `0`
|
|
|
|
* `size` {number} **Default:** `buffer.length - offset`
|
2018-06-26 23:49:47 +02:00
|
|
|
* Returns: {Buffer|TypedArray|DataView} The object passed as `buffer` argument.
|
2016-12-10 00:13:14 +01:00
|
|
|
|
|
|
|
Synchronous version of [`crypto.randomFill()`][].
|
|
|
|
|
|
|
|
```js
|
|
|
|
const buf = Buffer.alloc(10);
|
|
|
|
console.log(crypto.randomFillSync(buf).toString('hex'));
|
|
|
|
|
|
|
|
crypto.randomFillSync(buf, 5);
|
|
|
|
console.log(buf.toString('hex'));
|
|
|
|
|
|
|
|
// The above is equivalent to the following:
|
|
|
|
crypto.randomFillSync(buf, 5, 5);
|
|
|
|
console.log(buf.toString('hex'));
|
|
|
|
```
|
|
|
|
|
2017-09-06 17:10:34 +02:00
|
|
|
Any `TypedArray` or `DataView` instance may be passed as `buffer`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const a = new Uint32Array(10);
|
2018-06-26 23:49:47 +02:00
|
|
|
console.log(Buffer.from(crypto.randomFillSync(a).buffer,
|
|
|
|
a.byteOffset, a.byteLength).toString('hex'));
|
2017-09-06 17:10:34 +02:00
|
|
|
|
|
|
|
const b = new Float64Array(10);
|
2018-06-26 23:49:47 +02:00
|
|
|
console.log(Buffer.from(crypto.randomFillSync(b).buffer,
|
|
|
|
b.byteOffset, b.byteLength).toString('hex'));
|
2017-09-06 17:10:34 +02:00
|
|
|
|
|
|
|
const c = new DataView(new ArrayBuffer(10));
|
2018-06-26 23:49:47 +02:00
|
|
|
console.log(Buffer.from(crypto.randomFillSync(c).buffer,
|
|
|
|
c.byteOffset, c.byteLength).toString('hex'));
|
2017-09-06 17:10:34 +02:00
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.randomFill(buffer[, offset][, size], callback)`
|
2016-12-10 00:13:14 +01:00
|
|
|
<!-- YAML
|
2018-12-03 18:13:42 +01:00
|
|
|
added:
|
|
|
|
- v7.10.0
|
|
|
|
- v6.13.0
|
2017-09-06 17:10:34 +02:00
|
|
|
changes:
|
2017-09-01 18:50:47 +02:00
|
|
|
- version: v9.0.0
|
2017-09-06 17:10:34 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/15231
|
2017-12-10 21:42:25 +01:00
|
|
|
description: The `buffer` argument may be any `TypedArray` or `DataView`.
|
2016-12-10 00:13:14 +01:00
|
|
|
-->
|
|
|
|
|
2017-12-10 21:42:25 +01:00
|
|
|
* `buffer` {Buffer|TypedArray|DataView} Must be supplied.
|
2018-04-02 03:44:32 +02:00
|
|
|
* `offset` {number} **Default:** `0`
|
|
|
|
* `size` {number} **Default:** `buffer.length - offset`
|
2016-12-10 00:13:14 +01:00
|
|
|
* `callback` {Function} `function(err, buf) {}`.
|
|
|
|
|
|
|
|
This function is similar to [`crypto.randomBytes()`][] but requires the first
|
|
|
|
argument to be a [`Buffer`][] that will be filled. It also
|
|
|
|
requires that a callback is passed in.
|
|
|
|
|
|
|
|
If the `callback` function is not provided, an error will be thrown.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const buf = Buffer.alloc(10);
|
|
|
|
crypto.randomFill(buf, (err, buf) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log(buf.toString('hex'));
|
|
|
|
});
|
|
|
|
|
|
|
|
crypto.randomFill(buf, 5, (err, buf) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log(buf.toString('hex'));
|
|
|
|
});
|
|
|
|
|
|
|
|
// The above is equivalent to the following:
|
|
|
|
crypto.randomFill(buf, 5, 5, (err, buf) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log(buf.toString('hex'));
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2017-09-06 17:10:34 +02:00
|
|
|
Any `TypedArray` or `DataView` instance may be passed as `buffer`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const a = new Uint32Array(10);
|
|
|
|
crypto.randomFill(a, (err, buf) => {
|
|
|
|
if (err) throw err;
|
2018-06-26 23:49:47 +02:00
|
|
|
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
|
|
|
|
.toString('hex'));
|
2017-09-06 17:10:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const b = new Float64Array(10);
|
|
|
|
crypto.randomFill(b, (err, buf) => {
|
|
|
|
if (err) throw err;
|
2018-06-26 23:49:47 +02:00
|
|
|
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
|
|
|
|
.toString('hex'));
|
2017-09-06 17:10:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const c = new DataView(new ArrayBuffer(10));
|
|
|
|
crypto.randomFill(c, (err, buf) => {
|
|
|
|
if (err) throw err;
|
2018-06-26 23:49:47 +02:00
|
|
|
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
|
|
|
|
.toString('hex'));
|
2017-09-06 17:10:34 +02:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
This API uses libuv's threadpool, which can have surprising and
|
|
|
|
negative performance implications for some applications; see the
|
2017-08-23 23:59:06 +02:00
|
|
|
[`UV_THREADPOOL_SIZE`][] documentation for more information.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
The asynchronous version of `crypto.randomFill()` is carried out in a single
|
|
|
|
threadpool request. To minimize threadpool task length variation, partition
|
|
|
|
large `randomFill` requests when doing so as part of fulfilling a client
|
|
|
|
request.
|
2017-11-22 16:03:17 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.scrypt(password, salt, keylen[, options], callback)`
|
2018-05-18 11:05:20 +02:00
|
|
|
<!-- YAML
|
2018-06-19 09:35:50 +02:00
|
|
|
added: v10.5.0
|
2018-06-25 17:40:59 +02:00
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
- version:
|
|
|
|
- v12.8.0
|
|
|
|
- v10.17.0
|
2019-07-21 20:14:34 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/28799
|
|
|
|
description: The `maxmem` value can now be any safe integer.
|
2018-08-13 11:02:06 +02:00
|
|
|
- version: v10.9.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21525
|
2018-06-25 17:40:59 +02:00
|
|
|
description: The `cost`, `blockSize` and `parallelization` option names
|
|
|
|
have been added.
|
2018-05-18 11:05:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `password` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `salt` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `keylen` {number}
|
|
|
|
* `options` {Object}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `cost` {number} CPU/memory cost parameter. Must be a power of two greater
|
2018-06-24 09:30:52 +02:00
|
|
|
than one. **Default:** `16384`.
|
2019-09-13 06:22:29 +02:00
|
|
|
* `blockSize` {number} Block size parameter. **Default:** `8`.
|
|
|
|
* `parallelization` {number} Parallelization parameter. **Default:** `1`.
|
|
|
|
* `N` {number} Alias for `cost`. Only one of both may be specified.
|
|
|
|
* `r` {number} Alias for `blockSize`. Only one of both may be specified.
|
|
|
|
* `p` {number} Alias for `parallelization`. Only one of both may be specified.
|
|
|
|
* `maxmem` {number} Memory upper bound. It is an error when (approximately)
|
2018-06-24 09:30:52 +02:00
|
|
|
`128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`.
|
2018-07-12 19:48:11 +02:00
|
|
|
* `callback` {Function}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `err` {Error}
|
|
|
|
* `derivedKey` {Buffer}
|
2018-05-18 11:05:20 +02:00
|
|
|
|
|
|
|
Provides an asynchronous [scrypt][] implementation. Scrypt is a password-based
|
|
|
|
key derivation function that is designed to be expensive computationally and
|
|
|
|
memory-wise in order to make brute-force attacks unrewarding.
|
|
|
|
|
|
|
|
The `salt` should be as unique as possible. It is recommended that a salt is
|
|
|
|
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
|
|
|
|
|
|
|
|
The `callback` function is called with two arguments: `err` and `derivedKey`.
|
|
|
|
`err` is an exception object when key derivation fails, otherwise `err` is
|
|
|
|
`null`. `derivedKey` is passed to the callback as a [`Buffer`][].
|
|
|
|
|
|
|
|
An exception is thrown when any of the input arguments specify invalid values
|
|
|
|
or types.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
// Using the factory defaults.
|
|
|
|
crypto.scrypt('secret', 'salt', 64, (err, derivedKey) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
|
|
|
|
});
|
|
|
|
// Using a custom N parameter. Must be a power of two.
|
|
|
|
crypto.scrypt('secret', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log(derivedKey.toString('hex')); // '3745e48...aa39b34'
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.scryptSync(password, salt, keylen[, options])`
|
2018-05-18 11:05:20 +02:00
|
|
|
<!-- YAML
|
2018-06-19 09:35:50 +02:00
|
|
|
added: v10.5.0
|
2018-06-25 17:40:59 +02:00
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
- version:
|
|
|
|
- v12.8.0
|
|
|
|
- v10.17.0
|
2019-07-21 20:14:34 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/28799
|
|
|
|
description: The `maxmem` value can now be any safe integer.
|
2018-08-13 11:02:06 +02:00
|
|
|
- version: v10.9.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21525
|
2018-06-25 17:40:59 +02:00
|
|
|
description: The `cost`, `blockSize` and `parallelization` option names
|
|
|
|
have been added.
|
2018-05-18 11:05:20 +02:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `password` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `salt` {string|Buffer|TypedArray|DataView}
|
|
|
|
* `keylen` {number}
|
|
|
|
* `options` {Object}
|
2019-09-13 06:22:29 +02:00
|
|
|
* `cost` {number} CPU/memory cost parameter. Must be a power of two greater
|
2018-06-24 09:30:52 +02:00
|
|
|
than one. **Default:** `16384`.
|
2019-09-13 06:22:29 +02:00
|
|
|
* `blockSize` {number} Block size parameter. **Default:** `8`.
|
|
|
|
* `parallelization` {number} Parallelization parameter. **Default:** `1`.
|
|
|
|
* `N` {number} Alias for `cost`. Only one of both may be specified.
|
|
|
|
* `r` {number} Alias for `blockSize`. Only one of both may be specified.
|
|
|
|
* `p` {number} Alias for `parallelization`. Only one of both may be specified.
|
|
|
|
* `maxmem` {number} Memory upper bound. It is an error when (approximately)
|
2018-06-24 09:30:52 +02:00
|
|
|
`128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`.
|
2018-07-12 19:48:11 +02:00
|
|
|
* Returns: {Buffer}
|
2018-05-18 11:05:20 +02:00
|
|
|
|
|
|
|
Provides a synchronous [scrypt][] implementation. Scrypt is a password-based
|
|
|
|
key derivation function that is designed to be expensive computationally and
|
|
|
|
memory-wise in order to make brute-force attacks unrewarding.
|
|
|
|
|
|
|
|
The `salt` should be as unique as possible. It is recommended that a salt is
|
|
|
|
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
|
|
|
|
|
|
|
|
An exception is thrown when key derivation fails, otherwise the derived key is
|
|
|
|
returned as a [`Buffer`][].
|
|
|
|
|
|
|
|
An exception is thrown when any of the input arguments specify invalid values
|
|
|
|
or types.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
// Using the factory defaults.
|
|
|
|
const key1 = crypto.scryptSync('secret', 'salt', 64);
|
|
|
|
console.log(key1.toString('hex')); // '3745e48...08d59ae'
|
|
|
|
// Using a custom N parameter. Must be a power of two.
|
|
|
|
const key2 = crypto.scryptSync('secret', 'salt', 64, { N: 1024 });
|
|
|
|
console.log(key2.toString('hex')); // '3745e48...aa39b34'
|
|
|
|
```
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.setEngine(engine[, flags])`
|
2016-08-26 17:07:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.11
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `engine` {string}
|
|
|
|
* `flags` {crypto.constants} **Default:** `crypto.constants.ENGINE_METHOD_ALL`
|
2015-01-27 20:58:14 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Load and set the `engine` for some or all OpenSSL functions (selected by flags).
|
2015-01-27 20:58:14 +01:00
|
|
|
|
2015-11-04 17:23:03 +01:00
|
|
|
`engine` could be either an id or a path to the engine's shared library.
|
2012-10-22 19:37:20 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The optional `flags` argument uses `ENGINE_METHOD_ALL` by default. The `flags`
|
2016-05-02 19:27:12 +02:00
|
|
|
is a bit field taking one of or a mix of the following flags (defined in
|
|
|
|
`crypto.constants`):
|
|
|
|
|
|
|
|
* `crypto.constants.ENGINE_METHOD_RSA`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_DSA`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_DH`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_RAND`
|
2018-03-14 11:27:57 +01:00
|
|
|
* `crypto.constants.ENGINE_METHOD_EC`
|
2016-05-02 19:27:12 +02:00
|
|
|
* `crypto.constants.ENGINE_METHOD_CIPHERS`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_DIGESTS`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_PKEY_METHS`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_ALL`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_NONE`
|
2012-10-22 19:37:20 +02:00
|
|
|
|
2018-03-14 11:27:57 +01:00
|
|
|
The flags below are deprecated in OpenSSL-1.1.0.
|
|
|
|
|
|
|
|
* `crypto.constants.ENGINE_METHOD_ECDH`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_ECDSA`
|
|
|
|
* `crypto.constants.ENGINE_METHOD_STORE`
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.setFips(bool)`
|
2018-01-24 01:32:19 +01:00
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
added: v10.0.0
|
2018-01-24 01:32:19 +01:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-01-24 01:32:19 +01:00
|
|
|
* `bool` {boolean} `true` to enable FIPS mode.
|
|
|
|
|
|
|
|
Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build.
|
|
|
|
Throws an error if FIPS mode is not available.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.sign(algorithm, data, key)`
|
2019-03-12 14:17:10 +01:00
|
|
|
<!-- YAML
|
2019-03-22 14:19:46 +01:00
|
|
|
added: v12.0.0
|
2019-03-12 14:17:10 +01:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2019-03-12 14:17:10 +01:00
|
|
|
* `algorithm` {string | null | undefined}
|
|
|
|
* `data` {Buffer | TypedArray | DataView}
|
|
|
|
* `key` {Object | string | Buffer | KeyObject}
|
|
|
|
* Returns: {Buffer}
|
|
|
|
|
|
|
|
Calculates and returns the signature for `data` using the given private key and
|
|
|
|
algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
|
|
|
|
dependent upon the key type (especially Ed25519 and Ed448).
|
|
|
|
|
|
|
|
If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
|
|
|
|
passed to [`crypto.createPrivateKey()`][]. If it is an object, the following
|
|
|
|
additional properties can be passed:
|
|
|
|
|
2019-08-21 00:05:55 +02:00
|
|
|
* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the
|
|
|
|
format of the generated signature. It can be one of the following:
|
|
|
|
* `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`.
|
|
|
|
* `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363.
|
2019-10-24 06:28:42 +02:00
|
|
|
* `padding` {integer} Optional padding value for RSA, one of the following:
|
2019-03-12 14:17:10 +01:00
|
|
|
* `crypto.constants.RSA_PKCS1_PADDING` (default)
|
|
|
|
* `crypto.constants.RSA_PKCS1_PSS_PADDING`
|
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
`RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function
|
2019-03-12 14:17:10 +01:00
|
|
|
used to sign the message as specified in section 3.1 of [RFC 4055][].
|
2019-10-24 06:28:42 +02:00
|
|
|
* `saltLength` {integer} Salt length for when padding is
|
2019-03-12 14:17:10 +01:00
|
|
|
`RSA_PKCS1_PSS_PADDING`. The special value
|
|
|
|
`crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest
|
|
|
|
size, `crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN` (default) sets it to the
|
|
|
|
maximum permissible value.
|
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.timingSafeEqual(a, b)`
|
2017-02-13 03:49:35 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.6.0
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `a` {Buffer | TypedArray | DataView}
|
|
|
|
* `b` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {boolean}
|
2017-02-13 03:49:35 +01:00
|
|
|
|
2017-10-30 16:52:18 +01:00
|
|
|
This function is based on a constant-time algorithm.
|
2017-02-13 03:49:35 +01:00
|
|
|
Returns true if `a` is equal to `b`, without leaking timing information that
|
|
|
|
would allow an attacker to guess one of the values. This is suitable for
|
|
|
|
comparing HMAC digests or secret values like authentication cookies or
|
|
|
|
[capability urls](https://www.w3.org/TR/capability-urls/).
|
|
|
|
|
2017-04-05 00:59:30 +02:00
|
|
|
`a` and `b` must both be `Buffer`s, `TypedArray`s, or `DataView`s, and they
|
|
|
|
must have the same length.
|
2017-02-13 03:49:35 +01:00
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
Use of `crypto.timingSafeEqual` does not guarantee that the *surrounding* code
|
|
|
|
is timing-safe. Care should be taken to ensure that the surrounding code does
|
|
|
|
not introduce timing vulnerabilities.
|
2017-02-13 03:49:35 +01:00
|
|
|
|
2019-12-24 01:58:10 +01:00
|
|
|
### `crypto.verify(algorithm, data, key, signature)`
|
2019-03-12 14:17:10 +01:00
|
|
|
<!-- YAML
|
2019-03-22 14:19:46 +01:00
|
|
|
added: v12.0.0
|
2019-03-12 14:17:10 +01:00
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
2019-03-12 14:17:10 +01:00
|
|
|
* `algorithm` {string | null | undefined}
|
|
|
|
* `data` {Buffer | TypedArray | DataView}
|
|
|
|
* `key` {Object | string | Buffer | KeyObject}
|
|
|
|
* `signature` {Buffer | TypedArray | DataView}
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
Verifies the given signature for `data` using the given key and algorithm. If
|
|
|
|
`algorithm` is `null` or `undefined`, then the algorithm is dependent upon the
|
|
|
|
key type (especially Ed25519 and Ed448).
|
|
|
|
|
|
|
|
If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
|
|
|
|
passed to [`crypto.createPublicKey()`][]. If it is an object, the following
|
|
|
|
additional properties can be passed:
|
|
|
|
|
2019-08-21 00:05:55 +02:00
|
|
|
* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the
|
|
|
|
format of the generated signature. It can be one of the following:
|
|
|
|
* `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`.
|
|
|
|
* `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363.
|
2019-10-24 06:28:42 +02:00
|
|
|
* `padding` {integer} Optional padding value for RSA, one of the following:
|
2019-03-12 14:17:10 +01:00
|
|
|
* `crypto.constants.RSA_PKCS1_PADDING` (default)
|
|
|
|
* `crypto.constants.RSA_PKCS1_PSS_PADDING`
|
|
|
|
|
2019-06-20 22:01:15 +02:00
|
|
|
`RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function
|
2019-03-12 14:17:10 +01:00
|
|
|
used to sign the message as specified in section 3.1 of [RFC 4055][].
|
2019-10-24 06:28:42 +02:00
|
|
|
* `saltLength` {integer} Salt length for when padding is
|
2019-03-12 14:17:10 +01:00
|
|
|
`RSA_PKCS1_PSS_PADDING`. The special value
|
|
|
|
`crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest
|
|
|
|
size, `crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN` (default) sets it to the
|
|
|
|
maximum permissible value.
|
|
|
|
|
|
|
|
The `signature` argument is the previously calculated signature for the `data`.
|
|
|
|
|
|
|
|
Because public keys can be derived from private keys, a private key or a public
|
|
|
|
key may be passed for `key`.
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
## Notes
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
### Legacy streams API (prior to Node.js 0.10)
|
2012-10-11 17:32:36 +02:00
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
The Crypto module was added to Node.js before there was the concept of a
|
2015-12-27 05:54:01 +01:00
|
|
|
unified Stream API, and before there were [`Buffer`][] objects for handling
|
|
|
|
binary data. As such, the many of the `crypto` defined classes have methods not
|
2016-02-15 01:40:53 +01:00
|
|
|
typically found on other Node.js classes that implement the [streams][stream]
|
|
|
|
API (e.g. `update()`, `final()`, or `digest()`). Also, many methods accepted
|
2018-04-29 19:46:41 +02:00
|
|
|
and returned `'latin1'` encoded strings by default rather than `Buffer`s. This
|
2015-12-27 05:54:01 +01:00
|
|
|
default was changed after Node.js v0.8 to use [`Buffer`][] objects by default
|
|
|
|
instead.
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
### Recent ECDH changes
|
2012-10-11 17:32:36 +02:00
|
|
|
|
2015-11-19 20:00:00 +01:00
|
|
|
Usage of `ECDH` with non-dynamically generated key pairs has been simplified.
|
2016-02-15 01:40:53 +01:00
|
|
|
Now, [`ecdh.setPrivateKey()`][] can be called with a preselected private key
|
|
|
|
and the associated public point (key) will be computed and stored in the object.
|
2015-12-27 05:54:01 +01:00
|
|
|
This allows code to only store and provide the private part of the EC key pair.
|
2016-02-15 01:40:53 +01:00
|
|
|
[`ecdh.setPrivateKey()`][] now also validates that the private key is valid for
|
|
|
|
the selected curve.
|
2015-12-27 05:54:01 +01:00
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
The [`ecdh.setPublicKey()`][] method is now deprecated as its inclusion in the
|
|
|
|
API is not useful. Either a previously stored private key should be set, which
|
|
|
|
automatically generates the associated public key, or [`ecdh.generateKeys()`][]
|
|
|
|
should be called. The main drawback of using [`ecdh.setPublicKey()`][] is that
|
|
|
|
it can be used to put the ECDH key pair into an inconsistent state.
|
2015-11-19 20:00:00 +01:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
### Support for weak or compromised algorithms
|
2015-07-27 09:10:57 +02:00
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
The `crypto` module still supports some algorithms which are already
|
|
|
|
compromised and are not currently recommended for use. The API also allows
|
2019-06-06 07:33:29 +02:00
|
|
|
the use of ciphers and hashes with a small key size that are too weak for safe
|
|
|
|
use.
|
2015-07-27 09:10:57 +02:00
|
|
|
|
|
|
|
Users should take full responsibility for selecting the crypto
|
|
|
|
algorithm and key size according to their security requirements.
|
|
|
|
|
2015-12-27 05:54:01 +01:00
|
|
|
Based on the recommendations of [NIST SP 800-131A][]:
|
2015-07-27 09:10:57 +02:00
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* MD5 and SHA-1 are no longer acceptable where collision resistance is
|
2015-07-27 09:10:57 +02:00
|
|
|
required such as digital signatures.
|
2019-09-13 06:22:29 +02:00
|
|
|
* The key used with RSA, DSA, and DH algorithms is recommended to have
|
2015-07-27 09:10:57 +02:00
|
|
|
at least 2048 bits and that of the curve of ECDSA and ECDH at least
|
|
|
|
224 bits, to be safe to use for several years.
|
2019-09-13 06:22:29 +02:00
|
|
|
* The DH groups of `modp1`, `modp2` and `modp5` have a key size
|
2015-07-27 09:10:57 +02:00
|
|
|
smaller than 2048 bits and are not recommended.
|
|
|
|
|
|
|
|
See the reference for other recommendations and details.
|
2012-10-11 17:32:36 +02:00
|
|
|
|
2017-12-18 13:22:08 +01:00
|
|
|
### CCM mode
|
|
|
|
|
2018-06-14 15:18:14 +02:00
|
|
|
CCM is one of the supported [AEAD algorithms][]. Applications which use this
|
2017-12-18 13:22:08 +01:00
|
|
|
mode must adhere to certain restrictions when using the cipher API:
|
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* The authentication tag length must be specified during cipher creation by
|
2017-12-18 13:22:08 +01:00
|
|
|
setting the `authTagLength` option and must be one of 4, 6, 8, 10, 12, 14 or
|
|
|
|
16 bytes.
|
2019-09-13 06:22:29 +02:00
|
|
|
* The length of the initialization vector (nonce) `N` must be between 7 and 13
|
2017-12-18 13:22:08 +01:00
|
|
|
bytes (`7 ≤ N ≤ 13`).
|
2019-09-13 06:22:29 +02:00
|
|
|
* The length of the plaintext is limited to `2 ** (8 * (15 - N))` bytes.
|
|
|
|
* When decrypting, the authentication tag must be set via `setAuthTag()` before
|
2019-07-10 16:35:06 +02:00
|
|
|
calling `update()`.
|
2017-12-18 13:22:08 +01:00
|
|
|
Otherwise, decryption will fail and `final()` will throw an error in
|
|
|
|
compliance with section 2.6 of [RFC 3610][].
|
2019-09-13 06:22:29 +02:00
|
|
|
* Using stream methods such as `write(data)`, `end(data)` or `pipe()` in CCM
|
2017-12-18 13:22:08 +01:00
|
|
|
mode might fail as CCM cannot handle more than one chunk of data per instance.
|
2019-09-13 06:22:29 +02:00
|
|
|
* When passing additional authenticated data (AAD), the length of the actual
|
2017-12-18 13:22:08 +01:00
|
|
|
message in bytes must be passed to `setAAD()` via the `plaintextLength`
|
2020-04-27 17:30:42 +02:00
|
|
|
option.
|
|
|
|
Many crypto libraries include the authentication tag in the ciphertext,
|
|
|
|
which means that they produce ciphertexts of the length
|
|
|
|
`plaintextLength + authTagLength`. Node.js does not include the authentication
|
|
|
|
tag, so the ciphertext length is always `plaintextLength`.
|
|
|
|
This is not necessary if no AAD is used.
|
2019-09-13 06:22:29 +02:00
|
|
|
* As CCM processes the whole message at once, `update()` can only be called
|
2017-12-18 13:22:08 +01:00
|
|
|
once.
|
2019-09-13 06:22:29 +02:00
|
|
|
* Even though calling `update()` is sufficient to encrypt/decrypt the message,
|
2018-06-04 21:32:54 +02:00
|
|
|
applications *must* call `final()` to compute or verify the
|
2017-12-18 13:22:08 +01:00
|
|
|
authentication tag.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
const key = 'keykeykeykeykeykeykeykey';
|
|
|
|
const nonce = crypto.randomBytes(12);
|
|
|
|
|
|
|
|
const aad = Buffer.from('0123456789', 'hex');
|
|
|
|
|
|
|
|
const cipher = crypto.createCipheriv('aes-192-ccm', key, nonce, {
|
|
|
|
authTagLength: 16
|
|
|
|
});
|
|
|
|
const plaintext = 'Hello world';
|
|
|
|
cipher.setAAD(aad, {
|
|
|
|
plaintextLength: Buffer.byteLength(plaintext)
|
|
|
|
});
|
|
|
|
const ciphertext = cipher.update(plaintext, 'utf8');
|
|
|
|
cipher.final();
|
|
|
|
const tag = cipher.getAuthTag();
|
|
|
|
|
2018-04-06 14:32:35 +02:00
|
|
|
// Now transmit { ciphertext, nonce, tag }.
|
2017-12-18 13:22:08 +01:00
|
|
|
|
|
|
|
const decipher = crypto.createDecipheriv('aes-192-ccm', key, nonce, {
|
|
|
|
authTagLength: 16
|
|
|
|
});
|
|
|
|
decipher.setAuthTag(tag);
|
|
|
|
decipher.setAAD(aad, {
|
|
|
|
plaintextLength: ciphertext.length
|
|
|
|
});
|
|
|
|
const receivedPlaintext = decipher.update(ciphertext, null, 'utf8');
|
|
|
|
|
|
|
|
try {
|
|
|
|
decipher.final();
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Authentication failed!');
|
2019-04-24 22:43:31 +02:00
|
|
|
return;
|
2017-12-18 13:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(receivedPlaintext);
|
|
|
|
```
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
## Crypto constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
2016-11-15 18:56:15 +01:00
|
|
|
The following constants exported by `crypto.constants` apply to various uses of
|
2016-05-02 19:27:12 +02:00
|
|
|
the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
### OpenSSL options
|
2019-10-14 05:46:56 +02:00
|
|
|
|
2016-05-02 19:27:12 +02:00
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<th>Constant</th>
|
|
|
|
<th>Description</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_ALL</code></td>
|
|
|
|
<td>Applies multiple bug workarounds within OpenSSL. See
|
2018-06-20 07:14:31 +02:00
|
|
|
<a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>
|
|
|
|
for detail.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION</code></td>
|
|
|
|
<td>Allows legacy insecure renegotiation between OpenSSL and unpatched
|
2016-11-15 18:56:15 +01:00
|
|
|
clients or servers. See
|
2018-06-20 07:14:31 +02:00
|
|
|
<a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_CIPHER_SERVER_PREFERENCE</code></td>
|
2016-11-23 23:14:34 +01:00
|
|
|
<td>Attempts to use the server's preferences instead of the client's when
|
2017-05-26 19:51:15 +02:00
|
|
|
selecting a cipher. Behavior depends on protocol version. See
|
2018-06-20 07:14:31 +02:00
|
|
|
<a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_CISCO_ANYCONNECT</code></td>
|
|
|
|
<td>Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_COOKIE_EXCHANGE</code></td>
|
|
|
|
<td>Instructs OpenSSL to turn on cookie exchange.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_CRYPTOPRO_TLSEXT_BUG</code></td>
|
|
|
|
<td>Instructs OpenSSL to add server-hello extension from an early version
|
|
|
|
of the cryptopro draft.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS</code></td>
|
|
|
|
<td>Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability
|
|
|
|
workaround added in OpenSSL 0.9.6d.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_EPHEMERAL_RSA</code></td>
|
|
|
|
<td>Instructs OpenSSL to always use the tmp_rsa key when performing RSA
|
|
|
|
operations.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_LEGACY_SERVER_CONNECT</code></td>
|
2016-11-23 23:14:34 +01:00
|
|
|
<td>Allows initial connection to servers that do not support RI.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_MICROSOFT_SESS_ID_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_MSIE_SSLV2_RSA_PADDING</code></td>
|
|
|
|
<td>Instructs OpenSSL to disable the workaround for a man-in-the-middle
|
|
|
|
protocol-version vulnerability in the SSL 2.0 server implementation.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NETSCAPE_CA_DN_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NETSCAPE_CHALLENGE_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_COMPRESSION</code></td>
|
|
|
|
<td>Instructs OpenSSL to disable support for SSL/TLS compression.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_QUERY_MTU</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION</code></td>
|
|
|
|
<td>Instructs OpenSSL to always start a new session when performing
|
|
|
|
renegotiation.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_SSLv2</code></td>
|
|
|
|
<td>Instructs OpenSSL to turn off SSL v2</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_SSLv3</code></td>
|
|
|
|
<td>Instructs OpenSSL to turn off SSL v3</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_TICKET</code></td>
|
|
|
|
<td>Instructs OpenSSL to disable use of RFC4507bis tickets.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_TLSv1</code></td>
|
|
|
|
<td>Instructs OpenSSL to turn off TLS v1</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_TLSv1_1</code></td>
|
|
|
|
<td>Instructs OpenSSL to turn off TLS v1.1</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_NO_TLSv1_2</code></td>
|
|
|
|
<td>Instructs OpenSSL to turn off TLS v1.2</td>
|
|
|
|
</tr>
|
|
|
|
<td><code>SSL_OP_PKCS1_CHECK_1</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_PKCS1_CHECK_2</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_SINGLE_DH_USE</code></td>
|
|
|
|
<td>Instructs OpenSSL to always create a new key when using
|
|
|
|
temporary/ephemeral DH parameters.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_SINGLE_ECDH_USE</code></td>
|
|
|
|
<td>Instructs OpenSSL to always create a new key when using
|
|
|
|
temporary/ephemeral ECDH parameters.</td>
|
|
|
|
</tr>
|
|
|
|
<td><code>SSL_OP_SSLEAY_080_CLIENT_DH_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_TLS_BLOCK_PADDING_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_TLS_D5_BUG</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>SSL_OP_TLS_ROLLBACK_BUG</code></td>
|
|
|
|
<td>Instructs OpenSSL to disable version rollback attack detection.</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
### OpenSSL engine constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<th>Constant</th>
|
|
|
|
<th>Description</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_RSA</code></td>
|
|
|
|
<td>Limit engine usage to RSA</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_DSA</code></td>
|
|
|
|
<td>Limit engine usage to DSA</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_DH</code></td>
|
|
|
|
<td>Limit engine usage to DH</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_RAND</code></td>
|
|
|
|
<td>Limit engine usage to RAND</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
2018-03-14 11:27:57 +01:00
|
|
|
<td><code>ENGINE_METHOD_EC</code></td>
|
|
|
|
<td>Limit engine usage to EC</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_CIPHERS</code></td>
|
|
|
|
<td>Limit engine usage to CIPHERS</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_DIGESTS</code></td>
|
|
|
|
<td>Limit engine usage to DIGESTS</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_PKEY_METHS</code></td>
|
|
|
|
<td>Limit engine usage to PKEY_METHDS</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_PKEY_ASN1_METHS</code></td>
|
|
|
|
<td>Limit engine usage to PKEY_ASN1_METHS</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_ALL</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ENGINE_METHOD_NONE</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
### Other OpenSSL constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<th>Constant</th>
|
|
|
|
<th>Description</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>DH_CHECK_P_NOT_SAFE_PRIME</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>DH_CHECK_P_NOT_PRIME</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>DH_UNABLE_TO_CHECK_GENERATOR</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>DH_NOT_SUITABLE_GENERATOR</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>ALPN_ENABLED</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_PKCS1_PADDING</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_SSLV23_PADDING</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_NO_PADDING</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_PKCS1_OAEP_PADDING</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_X931_PADDING</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_PKCS1_PSS_PADDING</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
2017-03-06 00:41:26 +01:00
|
|
|
<tr>
|
|
|
|
<td><code>RSA_PSS_SALTLEN_DIGEST</code></td>
|
2018-07-12 19:28:42 +02:00
|
|
|
<td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the
|
|
|
|
digest size when signing or verifying.</td>
|
2017-03-06 00:41:26 +01:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td>
|
2018-07-12 19:28:42 +02:00
|
|
|
<td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the
|
|
|
|
maximum permissible value when signing data.</td>
|
2017-03-06 00:41:26 +01:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>RSA_PSS_SALTLEN_AUTO</code></td>
|
2018-07-12 19:28:42 +02:00
|
|
|
<td>Causes the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to be
|
|
|
|
determined automatically when verifying a signature.</td>
|
2017-03-06 00:41:26 +01:00
|
|
|
</tr>
|
2016-05-02 19:27:12 +02:00
|
|
|
<tr>
|
|
|
|
<td><code>POINT_CONVERSION_COMPRESSED</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>POINT_CONVERSION_UNCOMPRESSED</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>POINT_CONVERSION_HYBRID</code></td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
### Node.js crypto constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<th>Constant</th>
|
|
|
|
<th>Description</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>defaultCoreCipherList</code></td>
|
|
|
|
<td>Specifies the built-in default cipher list used by Node.js.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>defaultCipherList</code></td>
|
|
|
|
<td>Specifies the active default cipher list used by the current Node.js
|
|
|
|
process.</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
2016-02-15 01:40:53 +01:00
|
|
|
[`Buffer`]: buffer.html
|
2018-03-14 11:27:57 +01:00
|
|
|
[`EVP_BytesToKey`]: https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html
|
2018-09-20 19:53:44 +02:00
|
|
|
[`KeyObject`]: #crypto_class_keyobject
|
2019-01-11 20:43:58 +01:00
|
|
|
[`Sign`]: #crypto_class_sign
|
2017-08-23 23:59:06 +02:00
|
|
|
[`UV_THREADPOOL_SIZE`]: cli.html#cli_uv_threadpool_size_size
|
2019-01-11 20:43:58 +01:00
|
|
|
[`Verify`]: #crypto_class_verify
|
2017-06-06 11:00:32 +02:00
|
|
|
[`cipher.final()`]: #crypto_cipher_final_outputencoding
|
|
|
|
[`cipher.update()`]: #crypto_cipher_update_data_inputencoding_outputencoding
|
2017-08-15 22:16:14 +02:00
|
|
|
[`crypto.createCipher()`]: #crypto_crypto_createcipher_algorithm_password_options
|
|
|
|
[`crypto.createCipheriv()`]: #crypto_crypto_createcipheriv_algorithm_key_iv_options
|
|
|
|
[`crypto.createDecipher()`]: #crypto_crypto_createdecipher_algorithm_password_options
|
|
|
|
[`crypto.createDecipheriv()`]: #crypto_crypto_createdecipheriv_algorithm_key_iv_options
|
2017-06-06 11:00:32 +02:00
|
|
|
[`crypto.createDiffieHellman()`]: #crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding
|
|
|
|
[`crypto.createECDH()`]: #crypto_crypto_createecdh_curvename
|
2017-08-15 22:16:14 +02:00
|
|
|
[`crypto.createHash()`]: #crypto_crypto_createhash_algorithm_options
|
|
|
|
[`crypto.createHmac()`]: #crypto_crypto_createhmac_algorithm_key_options
|
2018-09-20 19:53:44 +02:00
|
|
|
[`crypto.createPrivateKey()`]: #crypto_crypto_createprivatekey_key
|
|
|
|
[`crypto.createPublicKey()`]: #crypto_crypto_createpublickey_key
|
|
|
|
[`crypto.createSecretKey()`]: #crypto_crypto_createsecretkey_key
|
2017-08-15 22:16:14 +02:00
|
|
|
[`crypto.createSign()`]: #crypto_crypto_createsign_algorithm_options
|
|
|
|
[`crypto.createVerify()`]: #crypto_crypto_createverify_algorithm_options
|
2016-02-15 01:40:53 +01:00
|
|
|
[`crypto.getCurves()`]: #crypto_crypto_getcurves
|
2019-07-07 09:33:18 +02:00
|
|
|
[`crypto.getDiffieHellman()`]: #crypto_crypto_getdiffiehellman_groupname
|
2015-11-28 00:30:32 +01:00
|
|
|
[`crypto.getHashes()`]: #crypto_crypto_gethashes
|
2018-09-12 15:25:43 +02:00
|
|
|
[`crypto.privateDecrypt()`]: #crypto_crypto_privatedecrypt_privatekey_buffer
|
|
|
|
[`crypto.privateEncrypt()`]: #crypto_crypto_privateencrypt_privatekey_buffer
|
|
|
|
[`crypto.publicDecrypt()`]: #crypto_crypto_publicdecrypt_key_buffer
|
|
|
|
[`crypto.publicEncrypt()`]: #crypto_crypto_publicencrypt_key_buffer
|
2016-12-10 00:13:14 +01:00
|
|
|
[`crypto.randomBytes()`]: #crypto_crypto_randombytes_size_callback
|
2017-04-20 16:58:42 +02:00
|
|
|
[`crypto.randomFill()`]: #crypto_crypto_randomfill_buffer_offset_size_callback
|
2018-05-18 11:05:20 +02:00
|
|
|
[`crypto.scrypt()`]: #crypto_crypto_scrypt_password_salt_keylen_options_callback
|
2017-06-06 11:00:32 +02:00
|
|
|
[`decipher.final()`]: #crypto_decipher_final_outputencoding
|
|
|
|
[`decipher.update()`]: #crypto_decipher_update_data_inputencoding_outputencoding
|
|
|
|
[`diffieHellman.setPublicKey()`]: #crypto_diffiehellman_setpublickey_publickey_encoding
|
2016-02-15 01:40:53 +01:00
|
|
|
[`ecdh.generateKeys()`]: #crypto_ecdh_generatekeys_encoding_format
|
2017-06-06 11:00:32 +02:00
|
|
|
[`ecdh.setPrivateKey()`]: #crypto_ecdh_setprivatekey_privatekey_encoding
|
|
|
|
[`ecdh.setPublicKey()`]: #crypto_ecdh_setpublickey_publickey_encoding
|
2016-02-15 01:40:53 +01:00
|
|
|
[`hash.digest()`]: #crypto_hash_digest_encoding
|
2017-06-06 11:00:32 +02:00
|
|
|
[`hash.update()`]: #crypto_hash_update_data_inputencoding
|
2016-02-15 01:40:53 +01:00
|
|
|
[`hmac.digest()`]: #crypto_hmac_digest_encoding
|
2017-06-06 11:00:32 +02:00
|
|
|
[`hmac.update()`]: #crypto_hmac_update_data_inputencoding
|
2018-09-20 19:53:44 +02:00
|
|
|
[`keyObject.export()`]: #crypto_keyobject_export_options
|
2020-05-11 19:55:37 +02:00
|
|
|
[`postMessage()`]: worker_threads.html#worker_threads_port_postmessage_value_transferlist
|
2018-11-07 21:27:47 +01:00
|
|
|
[`sign.sign()`]: #crypto_sign_sign_privatekey_outputencoding
|
2017-06-06 11:00:32 +02:00
|
|
|
[`sign.update()`]: #crypto_sign_update_data_inputencoding
|
2020-06-07 07:06:34 +02:00
|
|
|
[`stream.Writable` options]: stream.html#stream_new_stream_writable_options
|
2018-11-27 20:49:21 +01:00
|
|
|
[`stream.transform` options]: stream.html#stream_new_stream_transform_options
|
2018-09-10 22:36:14 +02:00
|
|
|
[`util.promisify()`]: util.html#util_util_promisify_original
|
2017-09-09 13:09:28 +02:00
|
|
|
[`verify.update()`]: #crypto_verify_update_data_inputencoding
|
2018-11-07 21:27:47 +01:00
|
|
|
[`verify.verify()`]: #crypto_verify_verify_object_signature_signatureencoding
|
2017-12-18 13:22:08 +01:00
|
|
|
[AEAD algorithms]: https://en.wikipedia.org/wiki/Authenticated_encryption
|
|
|
|
[CCM mode]: #crypto_ccm_mode
|
2018-11-27 20:49:21 +01:00
|
|
|
[Caveats]: #crypto_support_for_weak_or_compromised_algorithms
|
2020-06-14 23:49:34 +02:00
|
|
|
[Crypto constants]: #crypto_crypto_constants_1
|
2018-02-11 20:55:16 +01:00
|
|
|
[HTML 5.2]: https://www.w3.org/TR/html52/changes.html#features-removed
|
|
|
|
[HTML5's `keygen` element]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen
|
2018-07-14 14:10:10 +02:00
|
|
|
[NIST SP 800-131A]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf
|
|
|
|
[NIST SP 800-132]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
|
|
|
|
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
2017-06-20 16:44:53 +02:00
|
|
|
[Nonce-Disrespecting Adversaries]: https://github.com/nonce-disrespect/nonce-disrespect
|
2018-03-14 11:27:57 +01:00
|
|
|
[OpenSSL's SPKAC implementation]: https://www.openssl.org/docs/man1.1.0/apps/openssl-spkac.html
|
2018-10-13 01:29:46 +02:00
|
|
|
[RFC 1421]: https://www.rfc-editor.org/rfc/rfc1421.txt
|
2015-12-02 17:17:19 +01:00
|
|
|
[RFC 2412]: https://www.rfc-editor.org/rfc/rfc2412.txt
|
|
|
|
[RFC 3526]: https://www.rfc-editor.org/rfc/rfc3526.txt
|
2017-12-18 13:22:08 +01:00
|
|
|
[RFC 3610]: https://www.rfc-editor.org/rfc/rfc3610.txt
|
2017-03-06 00:41:26 +01:00
|
|
|
[RFC 4055]: https://www.rfc-editor.org/rfc/rfc4055.txt
|
2018-10-13 01:29:46 +02:00
|
|
|
[RFC 5208]: https://www.rfc-editor.org/rfc/rfc5208.txt
|
2018-11-07 21:27:47 +01:00
|
|
|
[encoding]: buffer.html#buffer_buffers_and_character_encodings
|
2017-05-08 18:30:13 +02:00
|
|
|
[initialization vector]: https://en.wikipedia.org/wiki/Initialization_vector
|
2018-05-18 11:05:20 +02:00
|
|
|
[scrypt]: https://en.wikipedia.org/wiki/Scrypt
|
2016-02-15 01:40:53 +01:00
|
|
|
[stream-writable-write]: stream.html#stream_writable_write_chunk_encoding_callback
|
2017-05-08 18:30:13 +02:00
|
|
|
[stream]: stream.html
|