Skip to content

Commit

Permalink
[New] add getRandomValues, getRandomUUID, webcrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
mat50015 authored and ljharb committed Nov 18, 2023
1 parent 460a13b commit 2142b5b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
'use strict'

var floor = Math.floor

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

exports.getRandomValues = function getRandomValues(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] = floor(randomFloat * 256)
}
return abv
}

exports.randomUUID = function randomUUID() {
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 Expand Up @@ -65,6 +90,10 @@ exports.privateDecrypt = publicEncrypt.privateDecrypt
// }
// })

var { Crypto } = require('@peculiar/webcrypto')
var crypto = new Crypto()
exports.webcrypto = crypto

var rf = require('randomfill')

exports.randomFill = rf.randomFill
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"pbkdf2": "^3.0.3",
"public-encrypt": "^4.0.0",
"randombytes": "^2.0.0",
"randomfill": "^1.0.3"
"randomfill": "^1.0.3",
"@peculiar/webcrypto": "1.4.3"
},
"devDependencies": {
"hash-test-vectors": "~1.3.2",
Expand Down

0 comments on commit 2142b5b

Please sign in to comment.