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 testEncryptDecrypt, 13 testSignVerify, 14} = require('../common/crypto'); 15 16// Tests key objects are returned when key encodings are not specified. 17{ 18 // If no publicKeyEncoding is specified, a key object should be returned. 19 generateKeyPair('rsa', { 20 modulusLength: 1024, 21 privateKeyEncoding: { 22 type: 'pkcs1', 23 format: 'pem' 24 } 25 }, common.mustSucceed((publicKey, privateKey) => { 26 assert.strictEqual(typeof publicKey, 'object'); 27 assert.strictEqual(publicKey.type, 'public'); 28 assert.strictEqual(publicKey.asymmetricKeyType, 'rsa'); 29 30 // The private key should still be a string. 31 assert.strictEqual(typeof privateKey, 'string'); 32 33 testEncryptDecrypt(publicKey, privateKey); 34 testSignVerify(publicKey, privateKey); 35 })); 36 37 // If no privateKeyEncoding is specified, a key object should be returned. 38 generateKeyPair('rsa', { 39 modulusLength: 1024, 40 publicKeyEncoding: { 41 type: 'pkcs1', 42 format: 'pem' 43 } 44 }, common.mustSucceed((publicKey, privateKey) => { 45 // The public key should still be a string. 46 assert.strictEqual(typeof publicKey, 'string'); 47 48 assert.strictEqual(typeof privateKey, 'object'); 49 assert.strictEqual(privateKey.type, 'private'); 50 assert.strictEqual(privateKey.asymmetricKeyType, 'rsa'); 51 52 testEncryptDecrypt(publicKey, privateKey); 53 testSignVerify(publicKey, privateKey); 54 })); 55} 56