GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — crypto: allow promisifying generateKeyPair · nodejs/node@cc82194

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • crypto.md
  • lib/internal/crypto
    • keygen.js
  • test/parallel
    • test-crypto-keygen.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

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 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
Collapse file

‎lib/internal/crypto/keygen.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/keygen.js
+6Lines changed: 6 additions & 0 deletions
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);
Collapse file

‎test/parallel/test-crypto-keygen.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-keygen.js
+29Lines changed: 29 additions & 0 deletions
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 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.