Skip to content

Commit 647d96a

Browse files
committed
feat: implement getRandomValues and randomUUID - resolves #213 and #214
1 parent 460a13b commit 647d96a

File tree

5 files changed

+21018
-1
lines changed

5 files changed

+21018
-1
lines changed

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
'use strict'
22

33
exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = require('randombytes')
4+
5+
exports.getRandomValues = function (abv) {
6+
var l = abv.length
7+
while (l--) {
8+
var bytes = exports.randomBytes(7)
9+
var randomFloat = (bytes[0] % 32) / 32
10+
11+
for (var i = 0; i < bytes.length; i++) {
12+
var byte = bytes[i]
13+
randomFloat = (randomFloat + byte) / 256
14+
}
15+
16+
abv[l] = Math.floor(randomFloat * 256)
17+
}
18+
return abv
19+
}
20+
21+
exports.randomUUID = function () {
22+
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, function (c) {
23+
return (c ^ (exports.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
24+
})
25+
}
26+
427
exports.createHash = exports.Hash = require('create-hash')
528
exports.createHmac = exports.Hmac = require('create-hmac')
629

0 commit comments

Comments
 (0)