Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: fix crypto.privateEncrypt fails first time
crypto.privateEncrypt fails for the first time after crypto.generateKeyPairSync with certain parameters Because the error stack is not cleaned up when crypto.generateKeyPairSync exits. Fixes: #40814
- Loading branch information
1 parent
1fe5d56
commit b306018
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const crypto = require('crypto'); | ||
|
||
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { | ||
modulusLength: 2048, | ||
publicKeyEncoding: { | ||
type: 'spki', | ||
format: 'pem' | ||
}, | ||
privateKeyEncoding: { | ||
type: 'pkcs8', | ||
format: 'pem', | ||
cipher: 'aes-128-ecb', | ||
passphrase: 'abcdef' | ||
} | ||
}); | ||
assert.notStrictEqual(privateKey.toString(), ''); | ||
|
||
const msg = 'The quick brown fox jumps over the lazy dog'; | ||
|
||
const encryptedString = crypto.privateEncrypt({ | ||
key: privateKey, | ||
passphrase: 'abcdef' | ||
}, Buffer.from(msg)).toString('base64'); | ||
const decryptedString = crypto.publicDecrypt(publicKey, Buffer.from(encryptedString, 'base64')).toString(); | ||
console.log(`Encrypted: ${encryptedString}`); | ||
console.log(`Decrypted: ${decryptedString}`); | ||
|
||
assert.notStrictEqual(encryptedString, ''); | ||
assert.strictEqual(decryptedString, msg); |