1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const assert = require('assert');
8const {
9  generateKeyPair,
10} = require('crypto');
11const {
12  assertApproximateSize,
13  testEncryptDecrypt,
14  testSignVerify,
15  pkcs1PubExp,
16  pkcs1PrivExp,
17} = require('../common/crypto');
18const { promisify } = require('util');
19
20// Test the util.promisified API with async RSA key generation.
21{
22  promisify(generateKeyPair)('rsa', {
23    publicExponent: 0x10001,
24    modulusLength: 512,
25    publicKeyEncoding: {
26      type: 'pkcs1',
27      format: 'pem'
28    },
29    privateKeyEncoding: {
30      type: 'pkcs1',
31      format: 'pem'
32    }
33  }).then(common.mustCall((keys) => {
34    const { publicKey, privateKey } = keys;
35    assert.strictEqual(typeof publicKey, 'string');
36    assert.match(publicKey, pkcs1PubExp);
37    assertApproximateSize(publicKey, 180);
38
39    assert.strictEqual(typeof privateKey, 'string');
40    assert.match(privateKey, pkcs1PrivExp);
41    assertApproximateSize(privateKey, 512);
42
43    testEncryptDecrypt(publicKey, privateKey);
44    testSignVerify(publicKey, privateKey);
45  }));
46}
47