Skip to content

Commit

Permalink
feat: implement getRandomValues and randomUUID - resolves #213 and #214
Browse files Browse the repository at this point in the history
  • Loading branch information
markhughes committed May 4, 2022
1 parent 460a13b commit 647d96a
Show file tree
Hide file tree
Showing 5 changed files with 21,018 additions and 1 deletion.
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
'use strict'

exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = require('randombytes')

exports.getRandomValues = function (abv) {
var l = abv.length
while (l--) {
var bytes = exports.randomBytes(7)
var randomFloat = (bytes[0] % 32) / 32

for (var i = 0; i < bytes.length; i++) {
var byte = bytes[i]
randomFloat = (randomFloat + byte) / 256
}

abv[l] = Math.floor(randomFloat * 256)
}
return abv
}

exports.randomUUID = function () {
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, function (c) {
return (c ^ (exports.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
})
}

exports.createHash = exports.Hash = require('create-hash')
exports.createHmac = exports.Hmac = require('create-hmac')

Expand Down
Loading

0 comments on commit 647d96a

Please sign in to comment.