Skip to content

Commit 221df22

Browse files
committed
crypto: deprecate aliases for randomBytes
PR-URL: #22519 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f417ea1 commit 221df22

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

doc/api/deprecations.md

+12
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,17 @@ Type: Runtime
10391039
The `crypto._toBuf()` function was not designed to be used by modules outside
10401040
of Node.js core and will be removed in the future.
10411041
1042+
<a id="DEP0115"></a>
1043+
### DEP0115: crypto.prng(), crypto.pseudoRandomBytes(), crypto.rng()
1044+
1045+
Type: Runtime
1046+
1047+
In recent versions of Node.js, there is no difference between
1048+
[`crypto.randomBytes()`][] and `crypto.pseudoRandomBytes()`. The latter is
1049+
deprecated along with the undocumented aliases `crypto.prng()` and
1050+
`crypto.rng()` in favor of [`crypto.randomBytes()`][] and will be removed in a
1051+
future release.
1052+
10421053
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
10431054
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
10441055
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
@@ -1065,6 +1076,7 @@ of Node.js core and will be removed in the future.
10651076
[`crypto.DEFAULT_ENCODING`]: crypto.html#crypto_crypto_default_encoding
10661077
[`crypto.fips`]: crypto.html#crypto_crypto_fips
10671078
[`crypto.pbkdf2()`]: crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback
1079+
[`crypto.randomBytes()`]: crypto.html#crypto_crypto_randombytes_size_callback
10681080
[`crypto.scrypt()`]: crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback
10691081
[`decipher.final()`]: crypto.html#crypto_decipher_final_outputencoding
10701082
[`decipher.setAuthTag()`]: crypto.html#crypto_decipher_setauthtag_buffer

lib/crypto.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,11 @@ module.exports = exports = {
155155
pbkdf2Sync,
156156
privateDecrypt,
157157
privateEncrypt,
158-
prng: randomBytes,
159-
pseudoRandomBytes: randomBytes,
160158
publicDecrypt,
161159
publicEncrypt,
162160
randomBytes,
163161
randomFill,
164162
randomFillSync,
165-
rng: randomBytes,
166163
scrypt,
167164
scryptSync,
168165
setEngine,
@@ -234,5 +231,22 @@ Object.defineProperties(exports, {
234231
configurable: false,
235232
enumerable: true,
236233
value: constants
234+
},
235+
236+
// Aliases for randomBytes are deprecated.
237+
// The ecosystem needs those to exist for backwards compatibility with
238+
// ancient Node.js runtimes (0.10, 0.12).
239+
prng: {
240+
enumerable: false,
241+
value: deprecate(randomBytes, 'crypto.prng is deprecated.', 'DEP0115')
242+
},
243+
pseudoRandomBytes: {
244+
enumerable: false,
245+
value: deprecate(randomBytes,
246+
'crypto.pseudoRandomBytes is deprecated.', 'DEP0115')
247+
},
248+
rng: {
249+
enumerable: false,
250+
value: deprecate(randomBytes, 'crypto.rng is deprecated.', 'DEP0115')
237251
}
238252
});

test/parallel/test-crypto-random.js

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const kMaxPossibleLength = Math.min(kMaxLength, kMaxUint32);
3535
// bump, we register a lot of exit listeners
3636
process.setMaxListeners(256);
3737

38+
common.expectWarning('DeprecationWarning',
39+
'crypto.pseudoRandomBytes is deprecated.', 'DEP0115');
40+
3841
{
3942
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach((f) => {
4043
[undefined, null, false, true, {}, []].forEach((value) => {

0 commit comments

Comments
 (0)