1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const assert = require('assert');
8const {
9  generateKeyPairSync,
10} = require('crypto');
11const {
12  assertApproximateSize,
13  testEncryptDecrypt,
14  testSignVerify,
15  pkcs1PubExp,
16  pkcs8Exp,
17} = require('../common/crypto');
18
19// To make the test faster, we will only test sync key generation once and
20// with a relatively small key.
21{
22  const ret = generateKeyPairSync('rsa', {
23    publicExponent: 3,
24    modulusLength: 512,
25    publicKeyEncoding: {
26      type: 'pkcs1',
27      format: 'pem'
28    },
29    privateKeyEncoding: {
30      type: 'pkcs8',
31      format: 'pem'
32    }
33  });
34
35  assert.strictEqual(Object.keys(ret).length, 2);
36  const { publicKey, privateKey } = ret;
37
38  assert.strictEqual(typeof publicKey, 'string');
39  assert.match(publicKey, pkcs1PubExp);
40  assertApproximateSize(publicKey, 162);
41  assert.strictEqual(typeof privateKey, 'string');
42  assert.match(privateKey, pkcs8Exp);
43  assertApproximateSize(privateKey, 512);
44
45  testEncryptDecrypt(publicKey, privateKey);
46  testSignVerify(publicKey, privateKey);
47}
48