Skip to content

Latest commit

Β 

History

History
2916 lines (2440 loc) Β· 99.1 KB

File metadata and controls

2916 lines (2440 loc) Β· 99.1 KB
Β 
Feb 27, 2012
Feb 27, 2012
1
# Crypto
Oct 28, 2010
Oct 28, 2010
2
Aug 28, 2017
Aug 28, 2017
3
<!--introduced_in=v0.3.6-->
4
Aug 4, 2016
Aug 4, 2016
5
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
6
Jan 6, 2016
Jan 6, 2016
7
The `crypto` module provides cryptographic functionality that includes a set of
Dec 1, 2017
Dec 1, 2017
8
wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.
Jan 6, 2016
Jan 6, 2016
9
Oct 28, 2010
Oct 28, 2010
10
Use `require('crypto')` to access this module.
11
Jan 21, 2016
Jan 21, 2016
12
```js
13
const crypto = require('crypto');
14
15
const secret = 'abcdefg';
16
const hash = crypto.createHmac('sha256', secret)
17
.update('I love cupcakes')
18
.digest('hex');
19
console.log(hash);
Nov 16, 2016
Nov 16, 2016
20
// Prints:
21
// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
Jan 21, 2016
Jan 21, 2016
22
```
Oct 28, 2010
Oct 28, 2010
23
Mar 23, 2016
Mar 23, 2016
24
## Determining if crypto support is unavailable
25
26
It is possible for Node.js to be built without including support for the
27
`crypto` module. In such cases, calling `require('crypto')` will result in an
28
error being thrown.
29
30
```js
Jan 24, 2017
Jan 24, 2017
31
let crypto;
Mar 23, 2016
Mar 23, 2016
32
try {
33
crypto = require('crypto');
34
} catch (err) {
35
console.log('crypto support is disabled!');
36
}
37
```
38
Nov 13, 2015
Nov 13, 2015
39
## Class: Certificate
Sep 12, 2016
Sep 12, 2016
40
<!-- YAML
41
added: v0.11.8
42
-->
Oct 12, 2012
Oct 12, 2012
43
Jan 6, 2016
Jan 6, 2016
44
SPKAC is a Certificate Signing Request mechanism originally implemented by
Feb 14, 2018
Feb 14, 2018
45
Netscape and was specified formally as part of [HTML5's `keygen` element][].
46
47
Note that `<keygen>` is deprecated since [HTML 5.2][] and new projects
48
should not use this element anymore.
Jan 6, 2016
Jan 6, 2016
49
50
The `crypto` module provides the `Certificate` class for working with SPKAC
51
data. The most common usage is handling output generated by the HTML5
52
`<keygen>` element. Node.js uses [OpenSSL's SPKAC implementation][] internally.
53
Sep 18, 2017
Sep 18, 2017
54
### Certificate.exportChallenge(spkac)
55
<!-- YAML
Oct 31, 2017
Oct 31, 2017
56
added: v9.0.0
Sep 18, 2017
Sep 18, 2017
57
-->
Jul 15, 2018
Jul 15, 2018
58
* `spkac` {string | Buffer | TypedArray | DataView}
59
* Returns: {Buffer} The challenge component of the `spkac` data structure, which
Apr 8, 2018
Apr 8, 2018
60
includes a public key and a challenge.
Sep 18, 2017
Sep 18, 2017
61
62
```js
63
const { Certificate } = require('crypto');
64
const spkac = getSpkacSomehow();
65
const challenge = Certificate.exportChallenge(spkac);
66
console.log(challenge.toString('utf8'));
67
// Prints: the challenge as a UTF8 string
68
```
69
Oct 15, 2017
Oct 15, 2017
70
### Certificate.exportPublicKey(spkac[, encoding])
Sep 18, 2017
Sep 18, 2017
71
<!-- YAML
Oct 31, 2017
Oct 31, 2017
72
added: v9.0.0
Sep 18, 2017
Sep 18, 2017
73
-->
Jul 15, 2018
Jul 15, 2018
74
* `spkac` {string | Buffer | TypedArray | DataView}
75
* `encoding` {string}
76
* Returns: {Buffer} The public key component of the `spkac` data structure,
Apr 8, 2018
Apr 8, 2018
77
which includes a public key and a challenge.
Sep 18, 2017
Sep 18, 2017
78
79
```js
80
const { Certificate } = require('crypto');
81
const spkac = getSpkacSomehow();
82
const publicKey = Certificate.exportPublicKey(spkac);
83
console.log(publicKey);
84
// Prints: the public key as <Buffer ...>
85
```
86
87
### Certificate.verifySpkac(spkac)
88
<!-- YAML
Oct 31, 2017
Oct 31, 2017
89
added: v9.0.0
Sep 18, 2017
Sep 18, 2017
90
-->
Jul 15, 2018
Jul 15, 2018
91
* `spkac` {Buffer | TypedArray | DataView}
92
* Returns: {boolean} `true` if the given `spkac` data structure is valid,
Apr 8, 2018
Apr 8, 2018
93
`false` otherwise.
Sep 18, 2017
Sep 18, 2017
94
95
```js
96
const { Certificate } = require('crypto');
97
const spkac = getSpkacSomehow();
98
console.log(Certificate.verifySpkac(Buffer.from(spkac)));
99
// Prints: true or false
100
```
101
102
### Legacy API
103
104
As a still supported legacy interface, it is possible (but not recommended) to
105
create new instances of the `crypto.Certificate` class as illustrated in the
106
examples below.
107
108
#### new crypto.Certificate()
Jan 6, 2016
Jan 6, 2016
109
110
Instances of the `Certificate` class can be created using the `new` keyword
111
or by calling `crypto.Certificate()` as a function:
112
Jan 21, 2016
Jan 21, 2016
113
```js
114
const crypto = require('crypto');
Jan 6, 2016
Jan 6, 2016
115
Jan 21, 2016
Jan 21, 2016
116
const cert1 = new crypto.Certificate();
117
const cert2 = crypto.Certificate();
118
```
Oct 12, 2012
Oct 12, 2012
119
Sep 18, 2017
Sep 18, 2017
120
#### certificate.exportChallenge(spkac)
Sep 12, 2016
Sep 12, 2016
121
<!-- YAML
122
added: v0.11.8
123
-->
Jul 15, 2018
Jul 15, 2018
124
* `spkac` {string | Buffer | TypedArray | DataView}
125
* Returns: {Buffer} The challenge component of the `spkac` data structure, which
Apr 8, 2018
Apr 8, 2018
126
includes a public key and a challenge.
Oct 12, 2012
Oct 12, 2012
127
Jan 21, 2016
Jan 21, 2016
128
```js
129
const cert = require('crypto').Certificate();
130
const spkac = getSpkacSomehow();
131
const challenge = cert.exportChallenge(spkac);
132
console.log(challenge.toString('utf8'));
Nov 16, 2016
Nov 16, 2016
133
// Prints: the challenge as a UTF8 string
Jan 21, 2016
Jan 21, 2016
134
```
Oct 12, 2012
Oct 12, 2012
135
Sep 18, 2017
Sep 18, 2017
136
#### certificate.exportPublicKey(spkac)
Sep 12, 2016
Sep 12, 2016
137
<!-- YAML
138
added: v0.11.8
139
-->
Jul 15, 2018
Jul 15, 2018
140
* `spkac` {string | Buffer | TypedArray | DataView}
141
* Returns: {Buffer} The public key component of the `spkac` data structure,
Apr 8, 2018
Apr 8, 2018
142
which includes a public key and a challenge.
Jan 6, 2016
Jan 6, 2016
143
Jan 21, 2016
Jan 21, 2016
144
```js
145
const cert = require('crypto').Certificate();
146
const spkac = getSpkacSomehow();
147
const publicKey = cert.exportPublicKey(spkac);
148
console.log(publicKey);
Nov 16, 2016
Nov 16, 2016
149
// Prints: the public key as <Buffer ...>
Jan 21, 2016
Jan 21, 2016
150
```
Oct 13, 2012
Oct 13, 2012
151
Sep 18, 2017
Sep 18, 2017
152
#### certificate.verifySpkac(spkac)
Sep 12, 2016
Sep 12, 2016
153
<!-- YAML
154
added: v0.11.8
155
-->
Jul 15, 2018
Jul 15, 2018
156
* `spkac` {Buffer | TypedArray | DataView}
157
* Returns: {boolean} `true` if the given `spkac` data structure is valid,
Apr 8, 2018
Apr 8, 2018
158
`false` otherwise.
Jan 6, 2016
Jan 6, 2016
159
Jan 21, 2016
Jan 21, 2016
160
```js
161
const cert = require('crypto').Certificate();
162
const spkac = getSpkacSomehow();
Apr 27, 2016
Apr 27, 2016
163
console.log(cert.verifySpkac(Buffer.from(spkac)));
Nov 16, 2016
Nov 16, 2016
164
// Prints: true or false
Jan 21, 2016
Jan 21, 2016
165
```
Oct 13, 2012
Oct 13, 2012
166
Nov 13, 2015
Nov 13, 2015
167
## Class: Cipher
Sep 12, 2016
Sep 12, 2016
168
<!-- YAML
169
added: v0.1.94
170
-->
Oct 13, 2012
Oct 13, 2012
171
Jan 6, 2016
Jan 6, 2016
172
Instances of the `Cipher` class are used to encrypt data. The class can be
173
used in one of two ways:
Oct 13, 2012
Oct 13, 2012
174
Jan 6, 2016
Jan 6, 2016
175
- As a [stream][] that is both readable and writable, where plain unencrypted
176
data is written to produce encrypted data on the readable side, or
Feb 28, 2016
Feb 28, 2016
177
- Using the [`cipher.update()`][] and [`cipher.final()`][] methods to produce
178
the encrypted data.
Jun 8, 2015
Jun 8, 2015
179
Feb 28, 2016
Feb 28, 2016
180
The [`crypto.createCipher()`][] or [`crypto.createCipheriv()`][] methods are
181
used to create `Cipher` instances. `Cipher` objects are not to be created
182
directly using the `new` keyword.
Jun 8, 2015
Jun 8, 2015
183
Jan 6, 2016
Jan 6, 2016
184
Example: Using `Cipher` objects as streams:
Jun 8, 2015
Jun 8, 2015
185
Jan 21, 2016
Jan 21, 2016
186
```js
187
const crypto = require('crypto');
188
const cipher = crypto.createCipher('aes192', 'a password');
Jun 8, 2015
Jun 8, 2015
189
Jan 24, 2017
Jan 24, 2017
190
let encrypted = '';
Jan 21, 2016
Jan 21, 2016
191
cipher.on('readable', () => {
Jan 24, 2017
Jan 24, 2017
192
const data = cipher.read();
Jan 21, 2016
Jan 21, 2016
193
if (data)
Jan 23, 2016
Jan 23, 2016
194
encrypted += data.toString('hex');
195
});
196
cipher.on('end', () => {
197
console.log(encrypted);
198
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
Jan 21, 2016
Jan 21, 2016
199
});
Jun 8, 2015
Jun 8, 2015
200
Jan 23, 2016
Jan 23, 2016
201
cipher.write('some clear text data');
Jan 21, 2016
Jan 21, 2016
202
cipher.end();
203
```
Jan 6, 2016
Jan 6, 2016
204
205
Example: Using `Cipher` and piped streams:
206
Jan 21, 2016
Jan 21, 2016
207
```js
208
const crypto = require('crypto');
209
const fs = require('fs');
210
const cipher = crypto.createCipher('aes192', 'a password');
Jan 6, 2016
Jan 6, 2016
211
Jan 21, 2016
Jan 21, 2016
212
const input = fs.createReadStream('test.js');
213
const output = fs.createWriteStream('test.enc');
Jan 6, 2016
Jan 6, 2016
214
Jan 21, 2016
Jan 21, 2016
215
input.pipe(cipher).pipe(output);
216
```
Jan 6, 2016
Jan 6, 2016
217
Feb 28, 2016
Feb 28, 2016
218
Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
Jan 6, 2016
Jan 6, 2016
219
Jan 21, 2016
Jan 21, 2016
220
```js
221
const crypto = require('crypto');
222
const cipher = crypto.createCipher('aes192', 'a password');
Oct 28, 2010
Oct 28, 2010
223
Jan 24, 2017
Jan 24, 2017
224
let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
Jan 23, 2016
Jan 23, 2016
225
encrypted += cipher.final('hex');
226
console.log(encrypted);
Nov 16, 2016
Nov 16, 2016
227
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
Jan 21, 2016
Jan 21, 2016
228
```
Jan 6, 2016
Jan 6, 2016
229
Jun 13, 2017
Jun 13, 2017
230
### cipher.final([outputEncoding])
Sep 12, 2016
Sep 12, 2016
231
<!-- YAML
232
added: v0.1.94
233
-->
Jul 15, 2018
Jul 15, 2018
234
* `outputEncoding` {string}
235
* Returns: {Buffer | string} Any remaining enciphered contents.
Apr 12, 2018
Apr 12, 2018
236
If `outputEncoding` is one of `'latin1'`, `'base64'` or `'hex'`, a string is
237
returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned.
Jan 6, 2016
Jan 6, 2016
238
239
Once the `cipher.final()` method has been called, the `Cipher` object can no
240
longer be used to encrypt data. Attempts to call `cipher.final()` more than
241
once will result in an error being thrown.
Jun 25, 2014
Jun 25, 2014
242
Apr 6, 2018
Apr 6, 2018
243
### cipher.setAAD(buffer[, options])
Sep 12, 2016
Sep 12, 2016
244
<!-- YAML
245
added: v1.0.0
246
-->
Jul 15, 2018
Jul 15, 2018
247
* `buffer` {Buffer}
248
* `options` {Object} [`stream.transform` options][]
Jun 24, 2018
Jun 24, 2018
249
- `plaintextLength` {number}
Jul 15, 2018
Jul 15, 2018
250
* Returns: {Cipher} for method chaining.
Oct 28, 2010
Oct 28, 2010
251
Jul 18, 2018
Jul 18, 2018
252
When using an authenticated encryption mode (`GCM`, `CCM` and `OCB` are
253
currently supported), the `cipher.setAAD()` method sets the value used for the
Jan 6, 2016
Jan 6, 2016
254
_additional authenticated data_ (AAD) input parameter.
255
Jul 18, 2018
Jul 18, 2018
256
The `options` argument is optional for `GCM` and `OCB`. When using `CCM`, the
Apr 6, 2018
Apr 6, 2018
257
`plaintextLength` option must be specified and its value must match the length
258
of the plaintext in bytes. See [CCM mode][].
259
Feb 7, 2017
Feb 7, 2017
260
The `cipher.setAAD()` method must be called before [`cipher.update()`][].
261
Jan 6, 2016
Jan 6, 2016
262
### cipher.getAuthTag()
Sep 12, 2016
Sep 12, 2016
263
<!-- YAML
264
added: v1.0.0
265
-->
Jul 18, 2018
Jul 18, 2018
266
* Returns: {Buffer} When using an authenticated encryption mode (`GCM`, `CCM`
267
and `OCB` are currently supported), the `cipher.getAuthTag()` method returns a
Apr 13, 2018
Apr 13, 2018
268
[`Buffer`][] containing the _authentication tag_ that has been computed from
269
the given data.
Jan 6, 2016
Jan 6, 2016
270
271
The `cipher.getAuthTag()` method should only be called after encryption has
Feb 28, 2016
Feb 28, 2016
272
been completed using the [`cipher.final()`][] method.
Oct 23, 2012
Oct 23, 2012
273
Jun 13, 2017
Jun 13, 2017
274
### cipher.setAutoPadding([autoPadding])
Sep 12, 2016
Sep 12, 2016
275
<!-- YAML
276
added: v0.7.1
277
-->
Jul 15, 2018
Jul 15, 2018
278
* `autoPadding` {boolean} **Default:** `true`
279
* Returns: {Cipher} for method chaining.
Oct 28, 2010
Oct 28, 2010
280
Jan 6, 2016
Jan 6, 2016
281
When using block encryption algorithms, the `Cipher` class will automatically
282
add padding to the input data to the appropriate block size. To disable the
283
default padding call `cipher.setAutoPadding(false)`.
284
Jun 13, 2017
Jun 13, 2017
285
When `autoPadding` is `false`, the length of the entire input data must be a
May 2, 2018
May 2, 2018
286
multiple of the cipher's block size or [`cipher.final()`][] will throw an error.
Jan 6, 2016
Jan 6, 2016
287
Disabling automatic padding is useful for non-standard padding, for instance
288
using `0x0` instead of PKCS padding.
289
Feb 7, 2017
Feb 7, 2017
290
The `cipher.setAutoPadding()` method must be called before
291
[`cipher.final()`][].
Oct 28, 2010
Oct 28, 2010
292
Jun 13, 2017
Jun 13, 2017
293
### cipher.update(data[, inputEncoding][, outputEncoding])
Sep 12, 2016
Sep 12, 2016
294
<!-- YAML
295
added: v0.1.94
Feb 24, 2017
Feb 24, 2017
296
changes:
297
- version: v6.0.0
298
pr-url: https://github.com/nodejs/node/pull/5522
Jun 13, 2017
Jun 13, 2017
299
description: The default `inputEncoding` changed from `binary` to `utf8`.
Sep 12, 2016
Sep 12, 2016
300
-->
Jul 15, 2018
Jul 15, 2018
301
* `data` {string | Buffer | TypedArray | DataView}
302
* `inputEncoding` {string}
303
* `outputEncoding` {string}
304
* Returns: {Buffer | string}
Oct 28, 2010
Oct 28, 2010
305
Jun 13, 2017
Jun 13, 2017
306
Updates the cipher with `data`. If the `inputEncoding` argument is given,
Dec 5, 2016
Dec 5, 2016
307
its value must be one of `'utf8'`, `'ascii'`, or `'latin1'` and the `data`
Jun 13, 2017
Jun 13, 2017
308
argument is a string using the specified encoding. If the `inputEncoding`
Apr 12, 2017
Apr 12, 2017
309
argument is not given, `data` must be a [`Buffer`][], `TypedArray`, or
Apr 4, 2018
Apr 4, 2018
310
`DataView`. If `data` is a [`Buffer`][], `TypedArray`, or `DataView`, then
Jun 13, 2017
Jun 13, 2017
311
`inputEncoding` is ignored.
Oct 28, 2010
Oct 28, 2010
312
Jun 13, 2017
Jun 13, 2017
313
The `outputEncoding` specifies the output format of the enciphered
314
data, and can be `'latin1'`, `'base64'` or `'hex'`. If the `outputEncoding`
Jan 6, 2016
Jan 6, 2016
315
is specified, a string using the specified encoding is returned. If no
Jun 13, 2017
Jun 13, 2017
316
`outputEncoding` is provided, a [`Buffer`][] is returned.
Oct 28, 2010
Oct 28, 2010
317
Jan 6, 2016
Jan 6, 2016
318
The `cipher.update()` method can be called multiple times with new data until
Feb 28, 2016
Feb 28, 2016
319
[`cipher.final()`][] is called. Calling `cipher.update()` after
320
[`cipher.final()`][] will result in an error being thrown.
Apr 3, 2011
Apr 3, 2011
321
Nov 13, 2015
Nov 13, 2015
322
## Class: Decipher
Sep 12, 2016
Sep 12, 2016
323
<!-- YAML
324
added: v0.1.94
325
-->
Apr 3, 2011
Apr 3, 2011
326
Jan 6, 2016
Jan 6, 2016
327
Instances of the `Decipher` class are used to decrypt data. The class can be
328
used in one of two ways:
329
330
- As a [stream][] that is both readable and writable, where plain encrypted
331
data is written to produce unencrypted data on the readable side, or
Feb 28, 2016
Feb 28, 2016
332
- Using the [`decipher.update()`][] and [`decipher.final()`][] methods to
333
produce the unencrypted data.
Jan 6, 2016
Jan 6, 2016
334
Feb 28, 2016
Feb 28, 2016
335
The [`crypto.createDecipher()`][] or [`crypto.createDecipheriv()`][] methods are
336
used to create `Decipher` instances. `Decipher` objects are not to be created
Jan 6, 2016
Jan 6, 2016
337
directly using the `new` keyword.
338
339
Example: Using `Decipher` objects as streams:
340
Jan 21, 2016
Jan 21, 2016
341
```js
342
const crypto = require('crypto');
343
const decipher = crypto.createDecipher('aes192', 'a password');
Jan 6, 2016
Jan 6, 2016
344
Jan 24, 2017
Jan 24, 2017
345
let decrypted = '';
Jan 21, 2016
Jan 21, 2016
346
decipher.on('readable', () => {
Jan 24, 2017
Jan 24, 2017
347
const data = decipher.read();
Jan 21, 2016
Jan 21, 2016
348
if (data)
Jul 15, 2016
Jul 15, 2016
349
decrypted += data.toString('utf8');
Jan 23, 2016
Jan 23, 2016
350
});
351
decipher.on('end', () => {
352
console.log(decrypted);
353
// Prints: some clear text data
Jan 21, 2016
Jan 21, 2016
354
});
Jan 6, 2016
Jan 6, 2016
355
Apr 24, 2017
Apr 24, 2017
356
const encrypted =
Apr 24, 2017
Apr 24, 2017
357
'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
Jan 23, 2016
Jan 23, 2016
358
decipher.write(encrypted, 'hex');
Jan 21, 2016
Jan 21, 2016
359
decipher.end();
360
```
Jan 6, 2016
Jan 6, 2016
361
362
Example: Using `Decipher` and piped streams:
363
Jan 25, 2016
Jan 25, 2016
364
```js
Jan 21, 2016
Jan 21, 2016
365
const crypto = require('crypto');
366
const fs = require('fs');
367
const decipher = crypto.createDecipher('aes192', 'a password');
Jan 6, 2016
Jan 6, 2016
368
Jan 21, 2016
Jan 21, 2016
369
const input = fs.createReadStream('test.enc');
370
const output = fs.createWriteStream('test.js');
Jan 6, 2016
Jan 6, 2016
371
Jan 21, 2016
Jan 21, 2016
372
input.pipe(decipher).pipe(output);
373
```
Jan 6, 2016
Jan 6, 2016
374
Feb 28, 2016
Feb 28, 2016
375
Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
Apr 3, 2011
Apr 3, 2011
376
Jan 21, 2016
Jan 21, 2016
377
```js
378
const crypto = require('crypto');
379
const decipher = crypto.createDecipher('aes192', 'a password');
Apr 3, 2011
Apr 3, 2011
380
Apr 24, 2017
Apr 24, 2017
381
const encrypted =
Apr 24, 2017
Apr 24, 2017
382
'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
Jan 24, 2017
Jan 24, 2017
383
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
Jan 23, 2016
Jan 23, 2016
384
decrypted += decipher.final('utf8');
385
console.log(decrypted);
Nov 16, 2016
Nov 16, 2016
386
// Prints: some clear text data
Jan 21, 2016
Jan 21, 2016
387
```
Apr 3, 2011
Apr 3, 2011
388
Jun 13, 2017
Jun 13, 2017
389
### decipher.final([outputEncoding])
Sep 12, 2016
Sep 12, 2016
390
<!-- YAML
391
added: v0.1.94
392
-->
Jul 15, 2018
Jul 15, 2018
393
* `outputEncoding` {string}
394
* Returns: {Buffer | string} Any remaining deciphered contents.
Apr 12, 2018
Apr 12, 2018
395
If `outputEncoding` is one of `'latin1'`, `'ascii'` or `'utf8'`, a string is
396
returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned.
Feb 27, 2012
Feb 27, 2012
397
Jan 6, 2016
Jan 6, 2016
398
Once the `decipher.final()` method has been called, the `Decipher` object can
399
no longer be used to decrypt data. Attempts to call `decipher.final()` more
400
than once will result in an error being thrown.
Dec 14, 2012
Dec 14, 2012
401
Jun 24, 2018
Jun 24, 2018
402
### decipher.setAAD(buffer[, options])
Sep 12, 2016
Sep 12, 2016
403
<!-- YAML
404
added: v1.0.0
Feb 24, 2017
Feb 24, 2017
405
changes:
406
- version: v7.2.0
407
pr-url: https://github.com/nodejs/node/pull/9398
408
description: This method now returns a reference to `decipher`.
Sep 12, 2016
Sep 12, 2016
409
-->
Jul 15, 2018
Jul 15, 2018
410
* `buffer` {Buffer | TypedArray | DataView}
411
* `options` {Object} [`stream.transform` options][]
Jun 24, 2018
Jun 24, 2018
412
- `plaintextLength` {number}
Jul 15, 2018
Jul 15, 2018
413
* Returns: {Decipher} for method chaining.
Feb 27, 2012
Feb 27, 2012
414
Jul 18, 2018
Jul 18, 2018
415
When using an authenticated encryption mode (`GCM`, `CCM` and `OCB` are
416
currently supported), the `decipher.setAAD()` method sets the value used for the
Jan 6, 2016
Jan 6, 2016
417
_additional authenticated data_ (AAD) input parameter.
Oct 28, 2010
Oct 28, 2010
418
Jun 24, 2018
Jun 24, 2018
419
The `options` argument is optional for `GCM`. When using `CCM`, the
420
`plaintextLength` option must be specified and its value must match the length
421
of the plaintext in bytes. See [CCM mode][].
422
Feb 7, 2017
Feb 7, 2017
423
The `decipher.setAAD()` method must be called before [`decipher.update()`][].
424
Nov 13, 2015
Nov 13, 2015
425
### decipher.setAuthTag(buffer)
Sep 12, 2016
Sep 12, 2016
426
<!-- YAML
427
added: v1.0.0
Feb 24, 2017
Feb 24, 2017
428
changes:
Oct 23, 2018
Oct 23, 2018
429
- version: v11.0.0
Apr 14, 2018
Apr 14, 2018
430
pr-url: https://github.com/nodejs/node/pull/17825
431
description: This method now throws if the GCM tag length is invalid.
Feb 24, 2017
Feb 24, 2017
432
- version: v7.2.0
433
pr-url: https://github.com/nodejs/node/pull/9398
434
description: This method now returns a reference to `decipher`.
Sep 12, 2016
Sep 12, 2016
435
-->
Jul 15, 2018
Jul 15, 2018
436
* `buffer` {Buffer | TypedArray | DataView}
437
* Returns: {Decipher} for method chaining.
Oct 23, 2012
Oct 23, 2012
438
Jul 18, 2018
Jul 18, 2018
439
When using an authenticated encryption mode (`GCM`, `CCM` and `OCB` are
440
currently supported), the `decipher.setAuthTag()` method is used to pass in the
Feb 28, 2016
Feb 28, 2016
441
received _authentication tag_. If no tag is provided, or if the cipher text
Dec 28, 2017
Dec 28, 2017
442
has been tampered with, [`decipher.final()`][] will throw, indicating that the
Apr 14, 2018
Apr 14, 2018
443
cipher text should be discarded due to failed authentication. If the tag length
Jun 24, 2018
Jun 24, 2018
444
is invalid according to [NIST SP 800-38D][] or does not match the value of the
445
`authTagLength` option, `decipher.setAuthTag()` will throw an error.
Jan 30, 2018
Jan 30, 2018
446
Feb 7, 2017
Feb 7, 2017
447
The `decipher.setAuthTag()` method must be called before
Sep 21, 2018
Sep 21, 2018
448
[`decipher.final()`][] and can only be called once.
Feb 7, 2017
Feb 7, 2017
449
Jun 13, 2017
Jun 13, 2017
450
### decipher.setAutoPadding([autoPadding])
Sep 12, 2016
Sep 12, 2016
451
<!-- YAML
452
added: v0.7.1
453
-->
Jul 15, 2018
Jul 15, 2018
454
* `autoPadding` {boolean} **Default:** `true`
455
* Returns: {Decipher} for method chaining.
Oct 28, 2010
Oct 28, 2010
456
Jan 6, 2016
Jan 6, 2016
457
When data has been encrypted without standard block padding, calling
Apr 7, 2016
Apr 7, 2016
458
`decipher.setAutoPadding(false)` will disable automatic padding to prevent
Feb 28, 2016
Feb 28, 2016
459
[`decipher.final()`][] from checking for and removing padding.
Jan 6, 2016
Jan 6, 2016
460
461
Turning auto padding off will only work if the input data's length is a
462
multiple of the ciphers block size.
463
464
The `decipher.setAutoPadding()` method must be called before
Feb 7, 2017
Feb 7, 2017
465
[`decipher.final()`][].
Oct 28, 2010
Oct 28, 2010
466
Jun 13, 2017
Jun 13, 2017
467
### decipher.update(data[, inputEncoding][, outputEncoding])
Sep 12, 2016
Sep 12, 2016
468
<!-- YAML
469
added: v0.1.94
Feb 24, 2017
Feb 24, 2017
470
changes:
471
- version: v6.0.0
472
pr-url: https://github.com/nodejs/node/pull/5522
Jun 13, 2017
Jun 13, 2017
473
description: The default `inputEncoding` changed from `binary` to `utf8`.
Sep 12, 2016
Sep 12, 2016
474
-->
Jul 15, 2018
Jul 15, 2018
475
* `data` {string | Buffer | TypedArray | DataView}
476
* `inputEncoding` {string}
477
* `outputEncoding` {string}
478
* Returns: {Buffer | string}
Jul 29, 2011
Jul 29, 2011
479
Jun 13, 2017
Jun 13, 2017
480
Updates the decipher with `data`. If the `inputEncoding` argument is given,
Dec 5, 2016
Dec 5, 2016
481
its value must be one of `'latin1'`, `'base64'`, or `'hex'` and the `data`
Jun 13, 2017
Jun 13, 2017
482
argument is a string using the specified encoding. If the `inputEncoding`
Jan 6, 2016
Jan 6, 2016
483
argument is not given, `data` must be a [`Buffer`][]. If `data` is a
Jun 13, 2017
Jun 13, 2017
484
[`Buffer`][] then `inputEncoding` is ignored.
Oct 28, 2010
Oct 28, 2010
485
Jun 13, 2017
Jun 13, 2017
486
The `outputEncoding` specifies the output format of the enciphered
487
data, and can be `'latin1'`, `'ascii'` or `'utf8'`. If the `outputEncoding`
Jan 6, 2016
Jan 6, 2016
488
is specified, a string using the specified encoding is returned. If no
Jun 13, 2017
Jun 13, 2017
489
`outputEncoding` is provided, a [`Buffer`][] is returned.
Jan 6, 2016
Jan 6, 2016
490
491
The `decipher.update()` method can be called multiple times with new data until
Feb 28, 2016
Feb 28, 2016
492
[`decipher.final()`][] is called. Calling `decipher.update()` after
493
[`decipher.final()`][] will result in an error being thrown.
Oct 28, 2010
Oct 28, 2010
494
Nov 13, 2015
Nov 13, 2015
495
## Class: DiffieHellman
Sep 12, 2016
Sep 12, 2016
496
<!-- YAML
497
added: v0.5.0
498
-->
Oct 28, 2010
Oct 28, 2010
499
Jan 6, 2016
Jan 6, 2016
500
The `DiffieHellman` class is a utility for creating Diffie-Hellman key
501
exchanges.
502
503
Instances of the `DiffieHellman` class can be created using the
Feb 28, 2016
Feb 28, 2016
504
[`crypto.createDiffieHellman()`][] function.
Jan 6, 2016
Jan 6, 2016
505
Jan 21, 2016
Jan 21, 2016
506
```js
507
const crypto = require('crypto');
508
const assert = require('assert');
Jan 6, 2016
Jan 6, 2016
509
Jan 21, 2016
Jan 21, 2016
510
// Generate Alice's keys...
Mar 21, 2016
Mar 21, 2016
511
const alice = crypto.createDiffieHellman(2048);
Jan 24, 2017
Jan 24, 2017
512
const aliceKey = alice.generateKeys();
Dec 14, 2012
Dec 14, 2012
513
Jan 21, 2016
Jan 21, 2016
514
// Generate Bob's keys...
Mar 21, 2016
Mar 21, 2016
515
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
Jan 24, 2017
Jan 24, 2017
516
const bobKey = bob.generateKeys();
Jan 6, 2016
Jan 6, 2016
517
Jan 21, 2016
Jan 21, 2016
518
// Exchange and generate the secret...
Jan 24, 2017
Jan 24, 2017
519
const aliceSecret = alice.computeSecret(bobKey);
520
const bobSecret = bob.computeSecret(aliceKey);
Jan 6, 2016
Jan 6, 2016
521
Mar 21, 2016
Mar 21, 2016
522
// OK
Jan 24, 2017
Jan 24, 2017
523
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
Jan 21, 2016
Jan 21, 2016
524
```
Oct 28, 2010
Oct 28, 2010
525
Jun 13, 2017
Jun 13, 2017
526
### diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])
Sep 12, 2016
Sep 12, 2016
527
<!-- YAML
528
added: v0.5.0
529
-->
Jul 15, 2018
Jul 15, 2018
530
* `otherPublicKey` {string | Buffer | TypedArray | DataView}
531
* `inputEncoding` {string}
532
* `outputEncoding` {string}
533
* Returns: {Buffer | string}
Feb 27, 2012
Feb 27, 2012
534
Jun 13, 2017
Jun 13, 2017
535
Computes the shared secret using `otherPublicKey` as the other
Jan 6, 2016
Jan 6, 2016
536
party's public key and returns the computed shared secret. The supplied
Jun 13, 2017
Jun 13, 2017
537
key is interpreted using the specified `inputEncoding`, and secret is
538
encoded using specified `outputEncoding`. Encodings can be
539
`'latin1'`, `'hex'`, or `'base64'`. If the `inputEncoding` is not
540
provided, `otherPublicKey` is expected to be a [`Buffer`][],
Apr 12, 2017
Apr 12, 2017
541
`TypedArray`, or `DataView`.
Feb 27, 2012
Feb 27, 2012
542
Jun 13, 2017
Jun 13, 2017
543
If `outputEncoding` is given a string is returned; otherwise, a
Jan 6, 2016
Jan 6, 2016
544
[`Buffer`][] is returned.
Feb 27, 2012
Feb 27, 2012
545
Nov 13, 2015
Nov 13, 2015
546
### diffieHellman.generateKeys([encoding])
Sep 12, 2016
Sep 12, 2016
547
<!-- YAML
548
added: v0.5.0
549
-->
Jul 15, 2018
Jul 15, 2018
550
* `encoding` {string}
551
* Returns: {Buffer | string}
Oct 28, 2010
Oct 28, 2010
552
Nov 13, 2015
Nov 13, 2015
553
Generates private and public Diffie-Hellman key values, and returns
Jan 6, 2016
Jan 6, 2016
554
the public key in the specified `encoding`. This key should be
Jun 7, 2016
Jun 7, 2016
555
transferred to the other party. Encoding can be `'latin1'`, `'hex'`,
Feb 28, 2016
Feb 28, 2016
556
or `'base64'`. If `encoding` is provided a string is returned; otherwise a
Jan 6, 2016
Jan 6, 2016
557
[`Buffer`][] is returned.
Oct 28, 2010
Oct 28, 2010
558
Nov 13, 2015
Nov 13, 2015
559
### diffieHellman.getGenerator([encoding])
Sep 12, 2016
Sep 12, 2016
560
<!-- YAML
561
added: v0.5.0
562
-->
Jul 15, 2018
Jul 15, 2018
563
* `encoding` {string}
564
* Returns: {Buffer | string}
Oct 28, 2010
Oct 28, 2010
565
Jan 6, 2016
Jan 6, 2016
566
Returns the Diffie-Hellman generator in the specified `encoding`, which can
Apr 4, 2018
Apr 4, 2018
567
be `'latin1'`, `'hex'`, or `'base64'`. If `encoding` is provided a string is
Jan 6, 2016
Jan 6, 2016
568
returned; otherwise a [`Buffer`][] is returned.
Oct 28, 2010
Oct 28, 2010
569
Nov 13, 2015
Nov 13, 2015
570
### diffieHellman.getPrime([encoding])
Sep 12, 2016
Sep 12, 2016
571
<!-- YAML
572
added: v0.5.0
573
-->
Jul 15, 2018
Jul 15, 2018
574
* `encoding` {string}
575
* Returns: {Buffer | string}
Jul 29, 2011
Jul 29, 2011
576
Jan 6, 2016
Jan 6, 2016
577
Returns the Diffie-Hellman prime in the specified `encoding`, which can
Jun 7, 2016
Jun 7, 2016
578
be `'latin1'`, `'hex'`, or `'base64'`. If `encoding` is provided a string is
Jan 6, 2016
Jan 6, 2016
579
returned; otherwise a [`Buffer`][] is returned.
Oct 28, 2010
Oct 28, 2010
580
Nov 13, 2015
Nov 13, 2015
581
### diffieHellman.getPrivateKey([encoding])
Sep 12, 2016
Sep 12, 2016
582
<!-- YAML
583
added: v0.5.0
584
-->
Jul 15, 2018
Jul 15, 2018
585
* `encoding` {string}
586
* Returns: {Buffer | string}
Oct 28, 2010
Oct 28, 2010
587
Jan 6, 2016
Jan 6, 2016
588
Returns the Diffie-Hellman private key in the specified `encoding`,
Jun 7, 2016
Jun 7, 2016
589
which can be `'latin1'`, `'hex'`, or `'base64'`. If `encoding` is provided a
Jan 6, 2016
Jan 6, 2016
590
string is returned; otherwise a [`Buffer`][] is returned.
Oct 28, 2010
Oct 28, 2010
591
Nov 13, 2015
Nov 13, 2015
592
### diffieHellman.getPublicKey([encoding])
Sep 12, 2016
Sep 12, 2016
593
<!-- YAML
594
added: v0.5.0
595
-->
Jul 15, 2018
Jul 15, 2018
596
* `encoding` {string}
597
* Returns: {Buffer | string}
Jul 25, 2011
Jul 25, 2011
598
Jan 6, 2016
Jan 6, 2016
599
Returns the Diffie-Hellman public key in the specified `encoding`, which
Jun 7, 2016
Jun 7, 2016
600
can be `'latin1'`, `'hex'`, or `'base64'`. If `encoding` is provided a
Jan 6, 2016
Jan 6, 2016
601
string is returned; otherwise a [`Buffer`][] is returned.
Dec 14, 2012
Dec 14, 2012
602
Jun 13, 2017
Jun 13, 2017
603
### diffieHellman.setPrivateKey(privateKey[, encoding])
Sep 12, 2016
Sep 12, 2016
604
<!-- YAML
605
added: v0.5.0
606
-->
Jul 15, 2018
Jul 15, 2018
607
* `privateKey` {string | Buffer | TypedArray | DataView}
608
* `encoding` {string}
Jan 10, 2015
Jan 10, 2015
609
Jan 6, 2016
Jan 6, 2016
610
Sets the Diffie-Hellman private key. If the `encoding` argument is provided
Jun 13, 2017
Jun 13, 2017
611
and is either `'latin1'`, `'hex'`, or `'base64'`, `privateKey` is expected
612
to be a string. If no `encoding` is provided, `privateKey` is expected
Apr 12, 2017
Apr 12, 2017
613
to be a [`Buffer`][], `TypedArray`, or `DataView`.
Jan 10, 2015
Jan 10, 2015
614
Jun 13, 2017
Jun 13, 2017
615
### diffieHellman.setPublicKey(publicKey[, encoding])
Sep 12, 2016
Sep 12, 2016
616
<!-- YAML
617
added: v0.5.0
618
-->
Jul 15, 2018
Jul 15, 2018
619
* `publicKey` {string | Buffer | TypedArray | DataView}
620
* `encoding` {string}
Jul 25, 2011
Jul 25, 2011
621
Jan 6, 2016
Jan 6, 2016
622
Sets the Diffie-Hellman public key. If the `encoding` argument is provided
Jun 13, 2017
Jun 13, 2017
623
and is either `'latin1'`, `'hex'` or `'base64'`, `publicKey` is expected
624
to be a string. If no `encoding` is provided, `publicKey` is expected
Apr 12, 2017
Apr 12, 2017
625
to be a [`Buffer`][], `TypedArray`, or `DataView`.
Jul 25, 2011
Jul 25, 2011
626
Nov 13, 2015
Nov 13, 2015
627
### diffieHellman.verifyError
Sep 12, 2016
Sep 12, 2016
628
<!-- YAML
629
added: v0.11.12
630
-->
Jun 12, 2012
Jun 12, 2012
631
Jan 6, 2016
Jan 6, 2016
632
A bit field containing any warnings and/or errors resulting from a check
633
performed during initialization of the `DiffieHellman` object.
634
635
The following values are valid for this property (as defined in `constants`
636
module):
Oct 28, 2010
Oct 28, 2010
637
Nov 13, 2015
Nov 13, 2015
638
* `DH_CHECK_P_NOT_SAFE_PRIME`
639
* `DH_CHECK_P_NOT_PRIME`
640
* `DH_UNABLE_TO_CHECK_GENERATOR`
641
* `DH_NOT_SUITABLE_GENERATOR`
Feb 27, 2012
Feb 27, 2012
642
Nov 13, 2015
Nov 13, 2015
643
## Class: ECDH
Sep 12, 2016
Sep 12, 2016
644
<!-- YAML
645
added: v0.11.14
646
-->
Feb 27, 2012
Feb 27, 2012
647
Jan 6, 2016
Jan 6, 2016
648
The `ECDH` class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
649
key exchanges.
650
651
Instances of the `ECDH` class can be created using the
Feb 28, 2016
Feb 28, 2016
652
[`crypto.createECDH()`][] function.
Jan 6, 2016
Jan 6, 2016
653
Jan 21, 2016
Jan 21, 2016
654
```js
655
const crypto = require('crypto');
656
const assert = require('assert');
Feb 27, 2012
Feb 27, 2012
657
Jan 21, 2016
Jan 21, 2016
658
// Generate Alice's keys...
659
const alice = crypto.createECDH('secp521r1');
Jan 24, 2017
Jan 24, 2017
660
const aliceKey = alice.generateKeys();
Jan 6, 2016
Jan 6, 2016
661
Jan 21, 2016
Jan 21, 2016
662
// Generate Bob's keys...
663
const bob = crypto.createECDH('secp521r1');
Jan 24, 2017
Jan 24, 2017
664
const bobKey = bob.generateKeys();
Jan 6, 2016
Jan 6, 2016
665
Jan 21, 2016
Jan 21, 2016
666
// Exchange and generate the secret...
Jan 24, 2017
Jan 24, 2017
667
const aliceSecret = alice.computeSecret(bobKey);
668
const bobSecret = bob.computeSecret(aliceKey);
Jan 6, 2016
Jan 6, 2016
669
Jan 24, 2017
Jan 24, 2017
670
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
Jun 29, 2017
Jun 29, 2017
671
// OK
Jan 21, 2016
Jan 21, 2016
672
```
Dec 14, 2012
Dec 14, 2012
673
Jun 16, 2018
Jun 16, 2018
674
### Class Method: ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])
Mar 23, 2018
Mar 23, 2018
675
<!-- YAML
Apr 24, 2018
Apr 24, 2018
676
added: v10.0.0
Mar 23, 2018
Mar 23, 2018
677
-->
678
Jul 15, 2018
Jul 15, 2018
679
* `key` {string | Buffer | TypedArray | DataView}
680
* `curve` {string}
681
* `inputEncoding` {string}
682
* `outputEncoding` {string}
683
* `format` {string} **Default:** `'uncompressed'`
684
* Returns: {Buffer | string}
Mar 23, 2018
Mar 23, 2018
685
686
Converts the EC Diffie-Hellman public key specified by `key` and `curve` to the
687
format specified by `format`. The `format` argument specifies point encoding
688
and can be `'compressed'`, `'uncompressed'` or `'hybrid'`. The supplied key is
689
interpreted using the specified `inputEncoding`, and the returned key is encoded
690
using the specified `outputEncoding`. Encodings can be `'latin1'`, `'hex'`,
691
or `'base64'`.
692
693
Use [`crypto.getCurves()`][] to obtain a list of available curve names.
694
On recent OpenSSL releases, `openssl ecparam -list_curves` will also display
695
the name and description of each available elliptic curve.
696
697
If `format` is not specified the point will be returned in `'uncompressed'`
698
format.
699
700
If the `inputEncoding` is not provided, `key` is expected to be a [`Buffer`][],
701
`TypedArray`, or `DataView`.
702
703
Example (uncompressing a key):
704
705
```js
Sep 3, 2018
Sep 3, 2018
706
const { createECDH, ECDH } = require('crypto');
Mar 23, 2018
Mar 23, 2018
707
Sep 3, 2018
Sep 3, 2018
708
const ecdh = createECDH('secp256k1');
Mar 23, 2018
Mar 23, 2018
709
ecdh.generateKeys();
710
711
const compressedKey = ecdh.getPublicKey('hex', 'compressed');
712
713
const uncompressedKey = ECDH.convertKey(compressedKey,
714
'secp256k1',
715
'hex',
716
'hex',
717
'uncompressed');
718
719
// the converted key and the uncompressed public key should be the same
720
console.log(uncompressedKey === ecdh.getPublicKey('hex'));
721
```
722
Jun 13, 2017
Jun 13, 2017
723
### ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])
Sep 12, 2016
Sep 12, 2016
724
<!-- YAML
725
added: v0.11.14
Feb 24, 2017
Feb 24, 2017
726
changes:
727
- version: v6.0.0
728
pr-url: https://github.com/nodejs/node/pull/5522
Dec 1, 2017
Dec 1, 2017
729
description: The default `inputEncoding` changed from `binary` to `utf8`
Apr 24, 2018
Apr 24, 2018
730
- version: v10.0.0
Dec 1, 2017
Dec 1, 2017
731
pr-url: https://github.com/nodejs/node/pull/16849
732
description: Changed error format to better support invalid public key
733
error
Sep 12, 2016
Sep 12, 2016
734
-->
Jul 15, 2018
Jul 15, 2018
735
* `otherPublicKey` {string | Buffer | TypedArray | DataView}
736
* `inputEncoding` {string}
737
* `outputEncoding` {string}
738
* Returns: {Buffer | string}
Oct 28, 2010
Oct 28, 2010
739
Jun 13, 2017
Jun 13, 2017
740
Computes the shared secret using `otherPublicKey` as the other
Jan 6, 2016
Jan 6, 2016
741
party's public key and returns the computed shared secret. The supplied
Jun 13, 2017
Jun 13, 2017
742
key is interpreted using specified `inputEncoding`, and the returned secret
743
is encoded using the specified `outputEncoding`. Encodings can be
744
`'latin1'`, `'hex'`, or `'base64'`. If the `inputEncoding` is not
745
provided, `otherPublicKey` is expected to be a [`Buffer`][], `TypedArray`, or
Apr 12, 2017
Apr 12, 2017
746
`DataView`.
Dec 27, 2011
Dec 27, 2011
747
Jun 13, 2017
Jun 13, 2017
748
If `outputEncoding` is given a string will be returned; otherwise a
Jan 6, 2016
Jan 6, 2016
749
[`Buffer`][] is returned.
Oct 28, 2010
Oct 28, 2010
750
Dec 1, 2017
Dec 1, 2017
751
`ecdh.computeSecret` will throw an
752
`ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY` error when `otherPublicKey`
753
lies outside of the elliptic curve. Since `otherPublicKey` is
754
usually supplied from a remote user over an insecure network,
755
its recommended for developers to handle this exception accordingly.
756
Feb 28, 2016
Feb 28, 2016
757
### ecdh.generateKeys([encoding[, format]])
Sep 12, 2016
Sep 12, 2016
758
<!-- YAML
759
added: v0.11.14
760
-->
Jul 15, 2018
Jul 15, 2018
761
* `encoding` {string}
762
* `format` {string} **Default:** `'uncompressed'`
763
* Returns: {Buffer | string}
Oct 28, 2010
Oct 28, 2010
764
Nov 13, 2015
Nov 13, 2015
765
Generates private and public EC Diffie-Hellman key values, and returns
Jan 6, 2016
Jan 6, 2016
766
the public key in the specified `format` and `encoding`. This key should be
Nov 13, 2015
Nov 13, 2015
767
transferred to the other party.
Oct 28, 2010
Oct 28, 2010
768
Aug 27, 2016
Aug 27, 2016
769
The `format` argument specifies point encoding and can be `'compressed'` or
770
`'uncompressed'`. If `format` is not specified, the point will be returned in
771
`'uncompressed'` format.
Oct 28, 2010
Oct 28, 2010
772
Jun 7, 2016
Jun 7, 2016
773
The `encoding` argument can be `'latin1'`, `'hex'`, or `'base64'`. If
Jan 6, 2016
Jan 6, 2016
774
`encoding` is provided a string is returned; otherwise a [`Buffer`][]
775
is returned.
Jul 29, 2011
Jul 29, 2011
776
Feb 28, 2016
Feb 28, 2016
777
### ecdh.getPrivateKey([encoding])
Sep 12, 2016
Sep 12, 2016
778
<!-- YAML
779
added: v0.11.14
780
-->
Jul 15, 2018
Jul 15, 2018
781
* `encoding` {string}
782
* Returns: {Buffer | string} The EC Diffie-Hellman private key in the specified
Apr 8, 2018
Apr 8, 2018
783
`encoding`, which can be `'latin1'`, `'hex'`, or `'base64'`. If `encoding`
784
is provided a string is returned; otherwise a [`Buffer`][] is returned.
Jan 18, 2012
Jan 18, 2012
785
Mar 15, 2017
Mar 15, 2017
786
### ecdh.getPublicKey([encoding][, format])
Sep 12, 2016
Sep 12, 2016
787
<!-- YAML
788
added: v0.11.14
789
-->
Jul 15, 2018
Jul 15, 2018
790
* `encoding` {string}
791
* `format` {string} **Default:** `'uncompressed'`
792
* Returns: {Buffer | string} The EC Diffie-Hellman public key in the specified
Apr 8, 2018
Apr 8, 2018
793
`encoding` and `format`.
Dec 7, 2013
Dec 7, 2013
794
Aug 27, 2016
Aug 27, 2016
795
The `format` argument specifies point encoding and can be `'compressed'` or
796
`'uncompressed'`. If `format` is not specified the point will be returned in
797
`'uncompressed'` format.
Mar 4, 2014
Mar 4, 2014
798
Jun 7, 2016
Jun 7, 2016
799
The `encoding` argument can be `'latin1'`, `'hex'`, or `'base64'`. If
Jan 6, 2016
Jan 6, 2016
800
`encoding` is specified, a string is returned; otherwise a [`Buffer`][] is
801
returned.
Mar 4, 2014
Mar 4, 2014
802
Jun 13, 2017
Jun 13, 2017
803
### ecdh.setPrivateKey(privateKey[, encoding])
Sep 12, 2016
Sep 12, 2016
804
<!-- YAML
805
added: v0.11.14
806
-->
Jul 15, 2018
Jul 15, 2018
807
* `privateKey` {string | Buffer | TypedArray | DataView}
808
* `encoding` {string}
Jul 29, 2011
Jul 29, 2011
809
Jun 7, 2016
Jun 7, 2016
810
Sets the EC Diffie-Hellman private key. The `encoding` can be `'latin1'`,
Jun 13, 2017
Jun 13, 2017
811
`'hex'` or `'base64'`. If `encoding` is provided, `privateKey` is expected
812
to be a string; otherwise `privateKey` is expected to be a [`Buffer`][],
Apr 12, 2017
Apr 12, 2017
813
`TypedArray`, or `DataView`.
814
Jun 13, 2017
Jun 13, 2017
815
If `privateKey` is not valid for the curve specified when the `ECDH` object was
Jan 6, 2016
Jan 6, 2016
816
created, an error is thrown. Upon setting the private key, the associated
May 2, 2018
May 2, 2018
817
public point (key) is also generated and set in the `ECDH` object.
Dec 7, 2015
Dec 7, 2015
818
Jun 13, 2017
Jun 13, 2017
819
### ecdh.setPublicKey(publicKey[, encoding])
Sep 12, 2016
Sep 12, 2016
820
<!-- YAML
821
added: v0.11.14
822
deprecated: v5.2.0
823
-->
Dec 7, 2015
Dec 7, 2015
824
Aug 4, 2016
Aug 4, 2016
825
> Stability: 0 - Deprecated
Dec 7, 2015
Dec 7, 2015
826
Jul 15, 2018
Jul 15, 2018
827
* `publicKey` {string | Buffer | TypedArray | DataView}
828
* `encoding` {string}
Mar 15, 2017
Mar 15, 2017
829
Jun 7, 2016
Jun 7, 2016
830
Sets the EC Diffie-Hellman public key. Key encoding can be `'latin1'`,
Jun 13, 2017
Jun 13, 2017
831
`'hex'` or `'base64'`. If `encoding` is provided `publicKey` is expected to
Apr 12, 2017
Apr 12, 2017
832
be a string; otherwise a [`Buffer`][], `TypedArray`, or `DataView` is expected.
Jan 6, 2016
Jan 6, 2016
833
834
Note that there is not normally a reason to call this method because `ECDH`
835
only requires a private key and the other party's public key to compute the
Feb 28, 2016
Feb 28, 2016
836
shared secret. Typically either [`ecdh.generateKeys()`][] or
837
[`ecdh.setPrivateKey()`][] will be called. The [`ecdh.setPrivateKey()`][] method
838
attempts to generate the public point/key associated with the private key being
839
set.
Oct 28, 2010
Oct 28, 2010
840
Nov 13, 2015
Nov 13, 2015
841
Example (obtaining a shared secret):
Jul 25, 2011
Jul 25, 2011
842
Jan 21, 2016
Jan 21, 2016
843
```js
844
const crypto = require('crypto');
845
const alice = crypto.createECDH('secp256k1');
846
const bob = crypto.createECDH('secp256k1');
Jul 25, 2011
Jul 25, 2011
847
Oct 29, 2018
Oct 29, 2018
848
// This is a shortcut way of specifying one of Alice's previous private
Jan 21, 2016
Jan 21, 2016
849
// keys. It would be unwise to use such a predictable private key in a real
850
// application.
851
alice.setPrivateKey(
852
crypto.createHash('sha256').update('alice', 'utf8').digest()
853
);
Dec 7, 2015
Dec 7, 2015
854
Jan 21, 2016
Jan 21, 2016
855
// Bob uses a newly generated cryptographically strong
Jan 24, 2017
Jan 24, 2017
856
// pseudorandom key pair
857
bob.generateKeys();
Oct 28, 2010
Oct 28, 2010
858
Jan 24, 2017
Jan 24, 2017
859
const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
860
const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
Feb 27, 2012
Feb 27, 2012
861
Jan 24, 2017
Jan 24, 2017
862
// aliceSecret and bobSecret should be the same shared secret value
863
console.log(aliceSecret === bobSecret);
Jan 21, 2016
Jan 21, 2016
864
```
Dec 14, 2012
Dec 14, 2012
865
Nov 13, 2015
Nov 13, 2015
866
## Class: Hash
Sep 12, 2016
Sep 12, 2016
867
<!-- YAML
868
added: v0.1.92
869
-->
Dec 27, 2011
Dec 27, 2011
870
Jan 6, 2016
Jan 6, 2016
871
The `Hash` class is a utility for creating hash digests of data. It can be
872
used in one of two ways:
873
874
- As a [stream][] that is both readable and writable, where data is written
875
to produce a computed hash digest on the readable side, or
Feb 28, 2016
Feb 28, 2016
876
- Using the [`hash.update()`][] and [`hash.digest()`][] methods to produce the
Jan 6, 2016
Jan 6, 2016
877
computed hash.
878
Feb 28, 2016
Feb 28, 2016
879
The [`crypto.createHash()`][] method is used to create `Hash` instances. `Hash`
Jan 6, 2016
Jan 6, 2016
880
objects are not to be created directly using the `new` keyword.
881
882
Example: Using `Hash` objects as streams:
883
Jan 21, 2016
Jan 21, 2016
884
```js
885
const crypto = require('crypto');
886
const hash = crypto.createHash('sha256');
Jan 6, 2016
Jan 6, 2016
887
Jan 21, 2016
Jan 21, 2016
888
hash.on('readable', () => {
Jan 24, 2017
Jan 24, 2017
889
const data = hash.read();
Jun 29, 2017
Jun 29, 2017
890
if (data) {
Jan 21, 2016
Jan 21, 2016
891
console.log(data.toString('hex'));
892
// Prints:
893
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
Jun 29, 2017
Jun 29, 2017
894
}
Jan 21, 2016
Jan 21, 2016
895
});
Jan 6, 2016
Jan 6, 2016
896
Jan 21, 2016
Jan 21, 2016
897
hash.write('some data to hash');
898
hash.end();
899
```
Jan 6, 2016
Jan 6, 2016
900
901
Example: Using `Hash` and piped streams:
902
Jan 21, 2016
Jan 21, 2016
903
```js
904
const crypto = require('crypto');
905
const fs = require('fs');
906
const hash = crypto.createHash('sha256');
Jan 6, 2016
Jan 6, 2016
907
Jan 21, 2016
Jan 21, 2016
908
const input = fs.createReadStream('test.js');
909
input.pipe(hash).pipe(process.stdout);
910
```
Oct 28, 2010
Oct 28, 2010
911
Feb 28, 2016
Feb 28, 2016
912
Example: Using the [`hash.update()`][] and [`hash.digest()`][] methods:
Oct 28, 2010
Oct 28, 2010
913
Jan 21, 2016
Jan 21, 2016
914
```js
915
const crypto = require('crypto');
916
const hash = crypto.createHash('sha256');
Jan 6, 2016
Jan 6, 2016
917
Jan 21, 2016
Jan 21, 2016
918
hash.update('some data to hash');
919
console.log(hash.digest('hex'));
Nov 16, 2016
Nov 16, 2016
920
// Prints:
921
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
Jan 21, 2016
Jan 21, 2016
922
```
Oct 28, 2010
Oct 28, 2010
923
Nov 13, 2015
Nov 13, 2015
924
### hash.digest([encoding])
Sep 12, 2016
Sep 12, 2016
925
<!-- YAML
926
added: v0.1.92
927
-->
Jul 15, 2018
Jul 15, 2018
928
* `encoding` {string}
929
* Returns: {Buffer | string}
Oct 28, 2010
Oct 28, 2010
930
Jan 6, 2016
Jan 6, 2016
931
Calculates the digest of all of the data passed to be hashed (using the
Jun 7, 2016
Jun 7, 2016
932
[`hash.update()`][] method). The `encoding` can be `'hex'`, `'latin1'` or
Feb 28, 2016
Feb 28, 2016
933
`'base64'`. If `encoding` is provided a string will be returned; otherwise
Jan 6, 2016
Jan 6, 2016
934
a [`Buffer`][] is returned.
Nov 13, 2015
Nov 13, 2015
935
Jan 6, 2016
Jan 6, 2016
936
The `Hash` object can not be used again after `hash.digest()` method has been
937
called. Multiple calls will cause an error to be thrown.
Jul 29, 2011
Jul 29, 2011
938
Jun 13, 2017
Jun 13, 2017
939
### hash.update(data[, inputEncoding])
Sep 12, 2016
Sep 12, 2016
940
<!-- YAML
941
added: v0.1.92
Feb 24, 2017
Feb 24, 2017
942
changes:
943
- version: v6.0.0
944
pr-url: https://github.com/nodejs/node/pull/5522
Jun 13, 2017
Jun 13, 2017
945
description: The default `inputEncoding` changed from `binary` to `utf8`.
Sep 12, 2016
Sep 12, 2016
946
-->
Jul 15, 2018
Jul 15, 2018
947
* `data` {string | Buffer | TypedArray | DataView}
948
* `inputEncoding` {string}
Jan 18, 2012
Jan 18, 2012
949
Nov 13, 2015
Nov 13, 2015
950
Updates the hash content with the given `data`, the encoding of which
Jun 13, 2017
Jun 13, 2017
951
is given in `inputEncoding` and can be `'utf8'`, `'ascii'` or
Jun 7, 2016
Jun 7, 2016
952
`'latin1'`. If `encoding` is not provided, and the `data` is a string, an
Apr 12, 2017
Apr 12, 2017
953
encoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or
Jun 13, 2017
Jun 13, 2017
954
`DataView`, then `inputEncoding` is ignored.
Jan 18, 2012
Jan 18, 2012
955
Nov 13, 2015
Nov 13, 2015
956
This can be called many times with new data as it is streamed.
Dec 7, 2013
Dec 7, 2013
957
Nov 13, 2015
Nov 13, 2015
958
## Class: Hmac
Sep 12, 2016
Sep 12, 2016
959
<!-- YAML
960
added: v0.1.94
961
-->
Dec 7, 2013
Dec 7, 2013
962
Jan 6, 2016
Jan 6, 2016
963
The `Hmac` Class is a utility for creating cryptographic HMAC digests. It can
964
be used in one of two ways:
965
966
- As a [stream][] that is both readable and writable, where data is written
967
to produce a computed HMAC digest on the readable side, or
Feb 28, 2016
Feb 28, 2016
968
- Using the [`hmac.update()`][] and [`hmac.digest()`][] methods to produce the
Jan 6, 2016
Jan 6, 2016
969
computed HMAC digest.
970
Feb 28, 2016
Feb 28, 2016
971
The [`crypto.createHmac()`][] method is used to create `Hmac` instances. `Hmac`
Jan 6, 2016
Jan 6, 2016
972
objects are not to be created directly using the `new` keyword.
973
974
Example: Using `Hmac` objects as streams:
975
Jan 21, 2016
Jan 21, 2016
976
```js
977
const crypto = require('crypto');
978
const hmac = crypto.createHmac('sha256', 'a secret');
Jan 6, 2016
Jan 6, 2016
979
Jan 21, 2016
Jan 21, 2016
980
hmac.on('readable', () => {
Jan 24, 2017
Jan 24, 2017
981
const data = hmac.read();
Jun 29, 2017
Jun 29, 2017
982
if (data) {
Jan 21, 2016
Jan 21, 2016
983
console.log(data.toString('hex'));
984
// Prints:
985
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
Jun 29, 2017
Jun 29, 2017
986
}
Jan 21, 2016
Jan 21, 2016
987
});
Jan 6, 2016
Jan 6, 2016
988
Jan 21, 2016
Jan 21, 2016
989
hmac.write('some data to hash');
990
hmac.end();
991
```
Jan 6, 2016
Jan 6, 2016
992
993
Example: Using `Hmac` and piped streams:
994
Jan 21, 2016
Jan 21, 2016
995
```js
996
const crypto = require('crypto');
997
const fs = require('fs');
998
const hmac = crypto.createHmac('sha256', 'a secret');
Jan 6, 2016
Jan 6, 2016
999
Jan 21, 2016
Jan 21, 2016
1000
const input = fs.createReadStream('test.js');