From 27c9d2883316a75383327c9d250d2174d05a6854 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 1 Jan 2021 21:59:44 -0800 Subject: [PATCH] crypto: implement randomuuid Signed-off-by: James M Snell --- benchmark/crypto/randomUUID.js | 17 ++++ doc/api/crypto.md | 16 ++++ lib/crypto.js | 4 +- lib/internal/crypto/random.js | 110 +++++++++++++++++++++++- src/crypto/crypto_util.cc | 34 ++++++++ test/parallel/test-crypto-randomuuid.js | 58 +++++++++++++ 6 files changed, 237 insertions(+), 2 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 2b762fa2eeef69..78dd5b74845dbb 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -3160,6 +3160,21 @@ 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 will pre-emptively generate and persistently cache 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)`