crypto: allow promisifying generateKeyPair

PR-URL: https://github.com/nodejs/node/pull/22660
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Tobias Nießen 2018-09-10 22:36:14 +02:00
parent 8c502f54ce
commit 085dcf0e59
No known key found for this signature in database
GPG Key ID: 718207F8FD156B70
3 changed files with 39 additions and 0 deletions

View File

@ -1731,6 +1731,9 @@ buffer containing the data encoded as DER. Note that Node.js itself does not
accept DER, it is supported for interoperability with other libraries such as
WebCrypto only.
If this method is invoked as its [`util.promisify()`][]ed version, it returns
a `Promise` for an `Object` with `publicKey` and `privateKey` properties.
### crypto.generateKeyPairSync(type, options)
<!-- YAML
added: REPLACEME
@ -2882,6 +2885,7 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
[`sign.update()`]: #crypto_sign_update_data_inputencoding
[`stream.transform` options]: stream.html#stream_new_stream_transform_options
[`stream.Writable` options]: stream.html#stream_constructor_new_stream_writable_options
[`util.promisify()`]: util.html#util_util_promisify_original
[`verify.update()`]: #crypto_verify_update_data_inputencoding
[`verify.verify()`]: #crypto_verify_verify_object_signature_signatureformat
[AEAD algorithms]: https://en.wikipedia.org/wiki/Authenticated_encryption

View File

@ -15,6 +15,7 @@ const {
PK_FORMAT_DER,
PK_FORMAT_PEM
} = internalBinding('crypto');
const { customPromisifyArgs } = require('internal/util');
const { isUint32 } = require('internal/validators');
const {
ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,
@ -44,6 +45,11 @@ function generateKeyPair(type, options, callback) {
handleError(impl, wrap);
}
Object.defineProperty(generateKeyPair, customPromisifyArgs, {
value: ['publicKey', 'privateKey'],
enumerable: false
});
function generateKeyPairSync(type, options) {
const impl = check(type, options);
return handleError(impl);

View File

@ -13,6 +13,7 @@ const {
publicEncrypt,
privateDecrypt
} = require('crypto');
const { promisify } = require('util');
// Asserts that the size of the given key (in chars or bytes) is within 10% of
// the expected size.
@ -240,6 +241,34 @@ function convertDERToPEM(label, der) {
}));
}
{
// Test the util.promisified API with async RSA key generation.
promisify(generateKeyPair)('rsa', {
publicExponent: 0x10001,
modulusLength: 3072,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem'
}
}).then(common.mustCall((keys) => {
const { publicKey, privateKey } = keys;
assert.strictEqual(typeof publicKey, 'string');
assert(pkcs1PubExp.test(publicKey));
assertApproximateSize(publicKey, 600);
assert.strictEqual(typeof privateKey, 'string');
assert(pkcs1PrivExp.test(privateKey));
assertApproximateSize(privateKey, 2455);
testEncryptDecrypt(publicKey, privateKey);
testSignVerify(publicKey, privateKey);
})).catch(common.mustNotCall());
}
{
// Test invalid key types.
for (const type of [undefined, null, 0]) {