-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
crypto: implement randomuuid #36729
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: implement randomuuid #36729
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,8 @@ const { | |
randomBytes, | ||
randomFill, | ||
randomFillSync, | ||
randomInt | ||
randomInt, | ||
randomUUID, | ||
} = require('internal/crypto/random'); | ||
const { | ||
pbkdf2, | ||
|
@@ -199,6 +200,7 @@ module.exports = { | |
randomFill, | ||
randomFillSync, | ||
randomInt, | ||
randomUUID, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding naming: The function name We were also discussing renaming the api of the After all I still think the term It also avoids naming conflicts with the current So all in all I'm 👍 for naming it like that in Node.js |
||
scrypt, | ||
scryptSync, | ||
sign: signOneShot, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
randomUUID, | ||
} = require('crypto'); | ||
|
||
const last = new Set([ | ||
'00000000-0000-0000-0000-000000000000' | ||
]); | ||
|
||
function testMatch(uuid) { | ||
assert.match( | ||
uuid, | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); | ||
} | ||
|
||
// Generate a number of UUID's to make sure we're | ||
// not just generating the same value over and over | ||
// and to make sure the batching changes the random | ||
// bytes. | ||
for (let n = 0; n < 130; n++) { | ||
const uuid = randomUUID(); | ||
assert(!last.has(uuid)); | ||
last.add(uuid); | ||
assert.strictEqual(typeof uuid, 'string'); | ||
assert.strictEqual(uuid.length, 36); | ||
testMatch(uuid); | ||
|
||
// Check that version 4 identifier was populated. | ||
assert.strictEqual( | ||
jasnell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Buffer.from(uuid.substr(14, 2), 'hex')[0] & 0x40, 0x40); | ||
|
||
// Check that clock_seq_hi_and_reserved was populated with reserved bits. | ||
assert.strictEqual( | ||
Buffer.from(uuid.substr(19, 2), 'hex')[0] & 0b1100_0000, 0b1000_0000); | ||
} | ||
|
||
// Test non-buffered UUID's | ||
{ | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
|
||
assert.throws(() => randomUUID(1), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
|
||
assert.throws(() => randomUUID({ disableEntropyCache: '' }), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.