From 08f91308885537e417e3df721894683e5dad5f4e Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 15 Jan 2021 07:11:13 -0800 Subject: [PATCH] crypto: implement randomuuid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/36729 Backport-PR-URL: https://github.com/nodejs/node/pull/36945 Reviewed-By: Сковорода Никита Андреевич Reviewed-By: Matteo Collina Reviewed-By: Michaël Zasso Reviewed-By: Filip Skokan Reviewed-By: Ben Coe --- benchmark/crypto/randomUUID.js | 17 +++ doc/api/crypto.md | 15 +++ doc/api/errors.md | 6 ++ lib/crypto.js | 4 +- lib/internal/crypto/random.js | 133 ++++++++++++++++++++++-- lib/internal/errors.js | 1 + src/node_crypto.cc | 34 ++++++ test/parallel/test-crypto-randomuuid.js | 58 +++++++++++ 8 files changed, 259 insertions(+), 9 deletions(-) create mode 100644 benchmark/crypto/randomUUID.js create mode 100644 test/parallel/test-crypto-randomuuid.js diff --git a/benchmark/crypto/randomUUID.js b/benchmark/crypto/randomUUID.js new file mode 100644 index 00000000000000..cca05242874738 --- /dev/null +++ b/benchmark/crypto/randomUUID.js @@ -0,0 +1,17 @@ +'use strict'; + +const common = require('../common.js'); +const { randomUUID } = require('crypto'); + +const bench = common.createBenchmark(main, { + n: [1e7], + disableEntropyCache: [0, 1], +}); + +function main({ n, disableEntropyCache }) { + disableEntropyCache = !!disableEntropyCache; + bench.start(); + for (let i = 0; i < n; ++i) + randomUUID({ disableEntropyCache }); + bench.end(n); +} diff --git a/doc/api/crypto.md b/doc/api/crypto.md index fb4d7977b17ff8..5ae5ad6b7a99ee 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2841,6 +2841,20 @@ const n = crypto.randomInt(1, 7); console.log(`The dice rolled: ${n}`); ``` +### `crypto.randomUUID([options])` + + +* `options` {Object} + * `disableEntropyCache` {boolean} By default, to improve performance, + Node.js generates and caches enough random data to generate up to + 128 random UUIDs. To generate a UUID without using the cache, set + `disableEntropyCache` to `true`. **Defaults**: `false`. +* Returns: {string} + +Generates a random [RFC 4122][] Version 4 UUID. + ### `crypto.scrypt(password, salt, keylen[, options], callback)`