GeezerVison is showing github.com — crypto: allow promisifying generateKeyPair · nodejs/node@cc82194
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/crypto.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1753,6 +1753,9 @@ buffer containing the data encoded as DER. Note that Node.js itself does not | 1753 | 1753 | accept DER, it is supported for interoperability with other libraries such as | 1754 | 1754 | WebCrypto only. | 1755 | 1755 |
| | 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 | + | 1756 | 1759 | ### crypto.generateKeyPairSync(type, options) | 1757 | 1760 | <!-- YAML | 1758 | 1761 | added: REPLACEME | @@ -2907,6 +2910,7 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL. | 2907 | 2910 | [`stream.transform` options]: stream.html#stream_new_stream_transform_options | 2908 | 2911 | [`stream.Writable` options]: stream.html#stream_constructor_new_stream_writable_options | 2909 | 2912 | [`tls.createSecureContext()`]: tls.html#tls_tls_createsecurecontext_options | | 2913 | +[`util.promisify()`]: util.html#util_util_promisify_original | 2910 | 2914 | [`verify.update()`]: #crypto_verify_update_data_inputencoding | 2911 | 2915 | [`verify.verify()`]: #crypto_verify_verify_object_signature_signatureformat | 2912 | 2916 | [AEAD algorithms]: https://en.wikipedia.org/wiki/Authenticated_encryption | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/keygen.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ const { | 14 | 14 | PK_FORMAT_DER, | 15 | 15 | PK_FORMAT_PEM | 16 | 16 | } = process.binding('crypto'); | | 17 | +const { customPromisifyArgs } = require('internal/util'); | 17 | 18 | const { isUint32 } = require('internal/validators'); | 18 | 19 | const { | 19 | 20 | ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS, | @@ -43,6 +44,11 @@ function generateKeyPair(type, options, callback) { | 43 | 44 | handleError(impl, wrap); | 44 | 45 | } | 45 | 46 |
| | 47 | +Object.defineProperty(generateKeyPair, customPromisifyArgs, { | | 48 | + value: ['publicKey', 'privateKey'], | | 49 | + enumerable: false | | 50 | +}); | | 51 | + | 46 | 52 | function generateKeyPairSync(type, options) { | 47 | 53 | const impl = check(type, options); | 48 | 54 | return handleError(impl); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-keygen.js +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ const { | 13 | 13 | publicEncrypt, | 14 | 14 | privateDecrypt | 15 | 15 | } = require('crypto'); | | 16 | +const { promisify } = require('util'); | 16 | 17 |
| 17 | 18 | // Asserts that the size of the given key (in chars or bytes) is within 10% of | 18 | 19 | // the expected size. | @@ -240,6 +241,34 @@ function convertDERToPEM(label, der) { | 240 | 241 | })); | 241 | 242 | } | 242 | 243 |
| | 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 | + | 243 | 272 | { | 244 | 273 | // Test invalid key types. | 245 | 274 | for (const type of [undefined, null, 0]) { | |
|