GeezerVison is showing github.com — src: fix `crypto.privateEncrypt` fails first time · nodejs/node@614d646
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/crypto/crypto_keys.cc +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -319,6 +319,7 @@ MaybeLocal<Value> WritePrivateKey( |
319 | 319 | } |
320 | 320 | } |
321 | 321 |
|
| 322 | + MarkPopErrorOnReturn mark_pop_error_on_return; |
322 | 323 | bool err; |
323 | 324 |
|
324 | 325 | PKEncodingType encoding_type = config.type_.ToChecked(); |
|
Collapse file
Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | + |
| 4 | +// Test for https://github.com/nodejs/node/issues/40814 |
| 5 | + |
| 6 | +if (!common.hasCrypto) |
| 7 | + common.skip('missing crypto'); |
| 8 | + |
| 9 | +if (!common.hasOpenSSL3) |
| 10 | + common.skip('only openssl3'); // https://github.com/nodejs/node/pull/42793#issuecomment-1107491901 |
| 11 | + |
| 12 | +const assert = require('assert'); |
| 13 | +const crypto = require('crypto'); |
| 14 | + |
| 15 | +const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { |
| 16 | + modulusLength: 2048, |
| 17 | + publicKeyEncoding: { |
| 18 | + type: 'spki', |
| 19 | + format: 'pem' |
| 20 | + }, |
| 21 | + privateKeyEncoding: { |
| 22 | + type: 'pkcs8', |
| 23 | + format: 'pem', |
| 24 | + cipher: 'aes-128-ecb', |
| 25 | + passphrase: 'abcdef' |
| 26 | + } |
| 27 | +}); |
| 28 | +assert.notStrictEqual(privateKey.toString(), ''); |
| 29 | + |
| 30 | +const msg = 'The quick brown fox jumps over the lazy dog'; |
| 31 | + |
| 32 | +const encryptedString = crypto.privateEncrypt({ |
| 33 | + key: privateKey, |
| 34 | + passphrase: 'abcdef' |
| 35 | +}, Buffer.from(msg)).toString('base64'); |
| 36 | +const decryptedString = crypto.publicDecrypt(publicKey, Buffer.from(encryptedString, 'base64')).toString(); |
| 37 | +console.log(`Encrypted: ${encryptedString}`); |
| 38 | +console.log(`Decrypted: ${decryptedString}`); |
| 39 | + |
| 40 | +assert.notStrictEqual(encryptedString, ''); |
| 41 | +assert.strictEqual(decryptedString, msg); |