Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: deprecate aliases for randomBytes #22519

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,17 @@ Type: Runtime
The `crypto._toBuf()` function was not designed to be used by modules outside
of Node.js core and will be removed in the future.

<a id="DEP00XX"></a>
### DEP00XX: crypto.prng(), crypto.pseudoRandomBytes(), crypto.rng()

Type: Runtime

In recent versions of Node.js, there is no difference between
[`crypto.randomBytes()`][] and `crypto.pseudoRandomBytes()`. The latter is
deprecated along with the undocumented aliases `crypto.prng()` and
`crypto.rng()` in favor of [`crypto.randomBytes()`][] and will be removed in a
future release.

[`--pending-deprecation`]: cli.html#cli_pending_deprecation
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
Expand All @@ -1065,6 +1076,7 @@ of Node.js core and will be removed in the future.
[`crypto.DEFAULT_ENCODING`]: crypto.html#crypto_crypto_default_encoding
[`crypto.fips`]: crypto.html#crypto_crypto_fips
[`crypto.pbkdf2()`]: crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback
[`crypto.randomBytes()`]: crypto.html#crypto_crypto_randombytes_size_callback
[`crypto.scrypt()`]: crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback
[`decipher.final()`]: crypto.html#crypto_decipher_final_outputencoding
[`decipher.setAuthTag()`]: crypto.html#crypto_decipher_setauthtag_buffer
Expand Down
20 changes: 17 additions & 3 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,11 @@ module.exports = exports = {
pbkdf2Sync,
privateDecrypt,
privateEncrypt,
prng: randomBytes,
pseudoRandomBytes: randomBytes,
publicDecrypt,
publicEncrypt,
randomBytes,
randomFill,
randomFillSync,
rng: randomBytes,
scrypt,
scryptSync,
setEngine,
Expand Down Expand Up @@ -234,5 +231,22 @@ Object.defineProperties(exports, {
configurable: false,
enumerable: true,
value: constants
},

// Aliases for randomBytes are deprecated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment:

// The ecosystem needs those to exists for backwards compatibility with
// ancient Node.js runtimes (0.10, 0.12).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it. IMO this kind of makes it sound as if we couldn't remove these functions in the future though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, this is the sound of #22519 (comment) comment.

// The ecosystem needs those to exist for backwards compatibility with
// ancient Node.js runtimes (0.10, 0.12).
prng: {
enumerable: false,
value: deprecate(randomBytes, 'crypto.prng is deprecated.', 'DEP00XX')
},
pseudoRandomBytes: {
enumerable: false,
value: deprecate(randomBytes,
'crypto.pseudoRandomBytes is deprecated.', 'DEP00XX')
},
rng: {
enumerable: false,
value: deprecate(randomBytes, 'crypto.rng is deprecated.', 'DEP00XX')
}
});
3 changes: 3 additions & 0 deletions test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const kMaxPossibleLength = Math.min(kMaxLength, kMaxUint32);
// bump, we register a lot of exit listeners
process.setMaxListeners(256);

common.expectWarning('DeprecationWarning',
'crypto.pseudoRandomBytes is deprecated.', 'DEP00XX');

{
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach((f) => {
[undefined, null, false, true, {}, []].forEach((value) => {
Expand Down