Skip to content

Commit cc82194

Browse files
tniessentargos
authored andcommitted
crypto: allow promisifying generateKeyPair
PR-URL: #22660 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 4219093 commit cc82194

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/api/crypto.md

+4
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,9 @@ buffer containing the data encoded as DER. Note that Node.js itself does not
17531753
accept DER, it is supported for interoperability with other libraries such as
17541754
WebCrypto only.
17551755

1756+
If this method is invoked as its [`util.promisify()`][]ed version, it returns
1757+
a `Promise` for an `Object` with `publicKey` and `privateKey` properties.
1758+
17561759
### crypto.generateKeyPairSync(type, options)
17571760
<!-- YAML
17581761
added: REPLACEME
@@ -2907,6 +2910,7 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
29072910
[`stream.transform` options]: stream.html#stream_new_stream_transform_options
29082911
[`stream.Writable` options]: stream.html#stream_constructor_new_stream_writable_options
29092912
[`tls.createSecureContext()`]: tls.html#tls_tls_createsecurecontext_options
2913+
[`util.promisify()`]: util.html#util_util_promisify_original
29102914
[`verify.update()`]: #crypto_verify_update_data_inputencoding
29112915
[`verify.verify()`]: #crypto_verify_verify_object_signature_signatureformat
29122916
[AEAD algorithms]: https://en.wikipedia.org/wiki/Authenticated_encryption

lib/internal/crypto/keygen.js

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
PK_FORMAT_DER,
1515
PK_FORMAT_PEM
1616
} = process.binding('crypto');
17+
const { customPromisifyArgs } = require('internal/util');
1718
const { isUint32 } = require('internal/validators');
1819
const {
1920
ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,
@@ -43,6 +44,11 @@ function generateKeyPair(type, options, callback) {
4344
handleError(impl, wrap);
4445
}
4546

47+
Object.defineProperty(generateKeyPair, customPromisifyArgs, {
48+
value: ['publicKey', 'privateKey'],
49+
enumerable: false
50+
});
51+
4652
function generateKeyPairSync(type, options) {
4753
const impl = check(type, options);
4854
return handleError(impl);

test/parallel/test-crypto-keygen.js

+29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
publicEncrypt,
1414
privateDecrypt
1515
} = require('crypto');
16+
const { promisify } = require('util');
1617

1718
// Asserts that the size of the given key (in chars or bytes) is within 10% of
1819
// the expected size.
@@ -240,6 +241,34 @@ function convertDERToPEM(label, der) {
240241
}));
241242
}
242243

244+
{
245+
// Test the util.promisified API with async RSA key generation.
246+
promisify(generateKeyPair)('rsa', {
247+
publicExponent: 0x10001,
248+
modulusLength: 3072,
249+
publicKeyEncoding: {
250+
type: 'pkcs1',
251+
format: 'pem'
252+
},
253+
privateKeyEncoding: {
254+
type: 'pkcs1',
255+
format: 'pem'
256+
}
257+
}).then(common.mustCall((keys) => {
258+
const { publicKey, privateKey } = keys;
259+
assert.strictEqual(typeof publicKey, 'string');
260+
assert(pkcs1PubExp.test(publicKey));
261+
assertApproximateSize(publicKey, 600);
262+
263+
assert.strictEqual(typeof privateKey, 'string');
264+
assert(pkcs1PrivExp.test(privateKey));
265+
assertApproximateSize(privateKey, 2455);
266+
267+
testEncryptDecrypt(publicKey, privateKey);
268+
testSignVerify(publicKey, privateKey);
269+
})).catch(common.mustNotCall());
270+
}
271+
243272
{
244273
// Test invalid key types.
245274
for (const type of [undefined, null, 0]) {

0 commit comments

Comments
 (0)