11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Test for https://github.com/nodejs/node/issues/40814
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciif (!common.hasOpenSSL3)
101cb0ef41Sopenharmony_ci  common.skip('only openssl3'); // https://github.com/nodejs/node/pull/42793#issuecomment-1107491901
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst assert = require('assert');
131cb0ef41Sopenharmony_ciconst crypto = require('crypto');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
161cb0ef41Sopenharmony_ci  modulusLength: 2048,
171cb0ef41Sopenharmony_ci  publicKeyEncoding: {
181cb0ef41Sopenharmony_ci    type: 'spki',
191cb0ef41Sopenharmony_ci    format: 'pem'
201cb0ef41Sopenharmony_ci  },
211cb0ef41Sopenharmony_ci  privateKeyEncoding: {
221cb0ef41Sopenharmony_ci    type: 'pkcs8',
231cb0ef41Sopenharmony_ci    format: 'pem',
241cb0ef41Sopenharmony_ci    cipher: 'aes-128-ecb',
251cb0ef41Sopenharmony_ci    passphrase: 'abcdef'
261cb0ef41Sopenharmony_ci  }
271cb0ef41Sopenharmony_ci});
281cb0ef41Sopenharmony_ciassert.notStrictEqual(privateKey.toString(), '');
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst msg = 'The quick brown fox jumps over the lazy dog';
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst encryptedString = crypto.privateEncrypt({
331cb0ef41Sopenharmony_ci  key: privateKey,
341cb0ef41Sopenharmony_ci  passphrase: 'abcdef'
351cb0ef41Sopenharmony_ci}, Buffer.from(msg)).toString('base64');
361cb0ef41Sopenharmony_ciconst decryptedString = crypto.publicDecrypt(publicKey, Buffer.from(encryptedString, 'base64')).toString();
371cb0ef41Sopenharmony_ciconsole.log(`Encrypted: ${encryptedString}`);
381cb0ef41Sopenharmony_ciconsole.log(`Decrypted: ${decryptedString}`);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciassert.notStrictEqual(encryptedString, '');
411cb0ef41Sopenharmony_ciassert.strictEqual(decryptedString, msg);
42