Skip to content

Commit c75f87c

Browse files
committed
crypto: refactor the crypto module
* Split single monolithic file into multiple * Make Certificate methods static * Allow randomFill(Sync) to use any ArrayBufferView * Use internal/errors throughout * Improve arg validation in Hash/Hmac * Doc updates PR-URL: nodejs#15231 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Fedor Indutny <[email protected]>
1 parent 8fa5fcc commit c75f87c

25 files changed

+1712
-1030
lines changed

doc/api/crypto.md

+102-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,60 @@ The `crypto` module provides the `Certificate` class for working with SPKAC
4848
data. The most common usage is handling output generated by the HTML5
4949
`<keygen>` element. Node.js uses [OpenSSL's SPKAC implementation][] internally.
5050

51-
### new crypto.Certificate()
51+
### Certificate.exportChallenge(spkac)
52+
<!-- YAML
53+
added: REPLACEME
54+
-->
55+
- `spkac` {string | Buffer | TypedArray | DataView}
56+
- Returns {Buffer} The challenge component of the `spkac` data structure, which
57+
includes a public key and a challenge.
58+
59+
```js
60+
const { Certificate } = require('crypto');
61+
const spkac = getSpkacSomehow();
62+
const challenge = Certificate.exportChallenge(spkac);
63+
console.log(challenge.toString('utf8'));
64+
// Prints: the challenge as a UTF8 string
65+
```
66+
67+
### Certificate.exportPublicKey(spkac)
68+
<!-- YAML
69+
added: REPLACEME
70+
-->
71+
- `spkac` {string | Buffer | TypedArray | DataView}
72+
- Returns {Buffer} The public key component of the `spkac` data structure,
73+
which includes a public key and a challenge.
74+
75+
```js
76+
const { Certificate } = require('crypto');
77+
const spkac = getSpkacSomehow();
78+
const publicKey = Certificate.exportPublicKey(spkac);
79+
console.log(publicKey);
80+
// Prints: the public key as <Buffer ...>
81+
```
82+
83+
### Certificate.verifySpkac(spkac)
84+
<!-- YAML
85+
added: REPLACEME
86+
-->
87+
- `spkac` {Buffer | TypedArray | DataView}
88+
- Returns {boolean} `true` if the given `spkac` data structure is valid, `false`
89+
otherwise.
90+
91+
```js
92+
const { Certificate } = require('crypto');
93+
const spkac = getSpkacSomehow();
94+
console.log(Certificate.verifySpkac(Buffer.from(spkac)));
95+
// Prints: true or false
96+
```
97+
98+
### Legacy API
99+
100+
As a still supported legacy interface, it is possible (but not recommended) to
101+
create new instances of the `crypto.Certificate` class as illustrated in the
102+
examples below.
103+
104+
#### new crypto.Certificate()
52105

53106
Instances of the `Certificate` class can be created using the `new` keyword
54107
or by calling `crypto.Certificate()` as a function:
@@ -60,7 +113,7 @@ const cert1 = new crypto.Certificate();
60113
const cert2 = crypto.Certificate();
61114
```
62115

63-
### certificate.exportChallenge(spkac)
116+
#### certificate.exportChallenge(spkac)
64117
<!-- YAML
65118
added: v0.11.8
66119
-->
@@ -76,7 +129,7 @@ console.log(challenge.toString('utf8'));
76129
// Prints: the challenge as a UTF8 string
77130
```
78131

79-
### certificate.exportPublicKey(spkac)
132+
#### certificate.exportPublicKey(spkac)
80133
<!-- YAML
81134
added: v0.11.8
82135
-->
@@ -92,7 +145,7 @@ console.log(publicKey);
92145
// Prints: the public key as <Buffer ...>
93146
```
94147

95-
### certificate.verifySpkac(spkac)
148+
#### certificate.verifySpkac(spkac)
96149
<!-- YAML
97150
added: v0.11.8
98151
-->
@@ -1747,9 +1800,13 @@ negative performance implications for some applications, see the
17471800
### crypto.randomFillSync(buffer[, offset][, size])
17481801
<!-- YAML
17491802
added: v7.10.0
1803+
changes:
1804+
- version: REPLACEME
1805+
pr-url: https://github.com/nodejs/node/pull/15231
1806+
description: The `buffer` argument may be any ArrayBufferView
17501807
-->
17511808

1752-
* `buffer` {Buffer|Uint8Array} Must be supplied.
1809+
* `buffer` {Buffer|Uint8Array|ArrayBufferView} Must be supplied.
17531810
* `offset` {number} Defaults to `0`.
17541811
* `size` {number} Defaults to `buffer.length - offset`.
17551812

@@ -1769,12 +1826,29 @@ crypto.randomFillSync(buf, 5, 5);
17691826
console.log(buf.toString('hex'));
17701827
```
17711828

1829+
Any `TypedArray` or `DataView` instance may be passed as `buffer`.
1830+
1831+
```js
1832+
const a = new Uint32Array(10);
1833+
console.log(crypto.randomFillSync(a).toString('hex'));
1834+
1835+
const b = new Float64Array(10);
1836+
console.log(crypto.randomFillSync(a).toString('hex'));
1837+
1838+
const c = new DataView(new ArrayBuffer(10));
1839+
console.log(crypto.randomFillSync(a).toString('hex'));
1840+
```
1841+
17721842
### crypto.randomFill(buffer[, offset][, size], callback)
17731843
<!-- YAML
17741844
added: v7.10.0
1845+
changes:
1846+
- version: REPLACEME
1847+
pr-url: https://github.com/nodejs/node/pull/15231
1848+
description: The `buffer` argument may be any ArrayBufferView
17751849
-->
17761850

1777-
* `buffer` {Buffer|Uint8Array} Must be supplied.
1851+
* `buffer` {Buffer|Uint8Array|ArrayBufferView} Must be supplied.
17781852
* `offset` {number} Defaults to `0`.
17791853
* `size` {number} Defaults to `buffer.length - offset`.
17801854
* `callback` {Function} `function(err, buf) {}`.
@@ -1804,6 +1878,28 @@ crypto.randomFill(buf, 5, 5, (err, buf) => {
18041878
});
18051879
```
18061880

1881+
Any `TypedArray` or `DataView` instance may be passed as `buffer`.
1882+
1883+
```js
1884+
const a = new Uint32Array(10);
1885+
crypto.randomFill(a, (err, buf) => {
1886+
if (err) throw err;
1887+
console.log(buf.toString('hex'));
1888+
});
1889+
1890+
const b = new Float64Array(10);
1891+
crypto.randomFill(b, (err, buf) => {
1892+
if (err) throw err;
1893+
console.log(buf.toString('hex'));
1894+
});
1895+
1896+
const c = new DataView(new ArrayBuffer(10));
1897+
crypto.randomFill(c, (err, buf) => {
1898+
if (err) throw err;
1899+
console.log(buf.toString('hex'));
1900+
});
1901+
```
1902+
18071903
Note that this API uses libuv's threadpool, which can have surprising and
18081904
negative performance implications for some applications, see the
18091905
[`UV_THREADPOOL_SIZE`][] documentation for more information.

doc/api/errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ Used when `Console` is instantiated without `stdout` stream or when `stdout` or
621621

622622
Used when the native call from `process.cpuUsage` cannot be processed properly.
623623

624+
<a id="ERR_CRYPTO_ECDH_INVALID_FORMAT"></a>
625+
### ERR_CRYPTO_ECDH_INVALID_FORMAT
626+
627+
Used when an invalid value for the `format` argument has been passed to the
628+
`crypto.ECDH()` class `getPublicKey()` method.
629+
624630
<a id="ERR_DNS_SET_SERVERS_FAILED"></a>
625631
### ERR_DNS_SET_SERVERS_FAILED
626632

0 commit comments

Comments
 (0)