Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorOli Lalonde <olalonde@gmail.com>2020-08-02 15:08:39 +0300
committerTobias Nießen <tniessen@tnie.de>2020-09-04 11:51:13 +0300
commit6e8701b92335a0fbf2f732948c00202b4fa9bbfe (patch)
treef9b0c1d7cd7b345291543dce34d25d4a7fc3d2cf /doc
parent3b925219c341062a9fc648049e217fea79f0ea3d (diff)
crypto: add randomInt function
PR-URL: https://github.com/nodejs/node/pull/34600 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'doc')
-rw-r--r--doc/api/crypto.md39
1 files changed, 39 insertions, 0 deletions
diff --git a/doc/api/crypto.md b/doc/api/crypto.md
index 539ce6fc79d..f0c7620c10d 100644
--- a/doc/api/crypto.md
+++ b/doc/api/crypto.md
@@ -2801,6 +2801,44 @@ threadpool request. To minimize threadpool task length variation, partition
large `randomFill` requests when doing so as part of fulfilling a client
request.
+### `crypto.randomInt([min, ]max[, callback])`
+<!-- YAML
+added: REPLACEME
+-->
+
+* `min` {integer} Start of random range (inclusive). **Default**: `0`.
+* `max` {integer} End of random range (exclusive).
+* `callback` {Function} `function(err, n) {}`.
+
+Return a random integer `n` such that `min <= n < max`. This
+implementation avoids [modulo bias][].
+
+The range (`max - min`) must be less than `2^48`. `min` and `max` must
+be safe integers.
+
+If the `callback` function is not provided, the random integer is
+generated synchronously.
+
+```js
+// Asynchronous
+crypto.randomInt(3, (err, n) => {
+ if (err) throw err;
+ console.log(`Random number chosen from (0, 1, 2): ${n}`);
+});
+```
+
+```js
+// Synchronous
+const n = crypto.randomInt(3);
+console.log(`Random number chosen from (0, 1, 2): ${n}`);
+```
+
+```js
+// With `min` argument
+const n = crypto.randomInt(1, 7);
+console.log(`The dice rolled: ${n}`);
+```
+
### `crypto.scrypt(password, salt, keylen[, options], callback)`
<!-- YAML
added: v10.5.0
@@ -3573,6 +3611,7 @@ See the [list of SSL OP Flags][] for details.
[NIST SP 800-131A]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf
[NIST SP 800-132]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
+[modulo bias]: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias
[Nonce-Disrespecting Adversaries]: https://github.com/nonce-disrespect/nonce-disrespect
[OpenSSL's SPKAC implementation]: https://www.openssl.org/docs/man1.1.0/apps/openssl-spkac.html
[RFC 1421]: https://www.rfc-editor.org/rfc/rfc1421.txt