Skip to content

Commit 2142b5b

Browse files
mat50015ljharb
authored andcommitted
[New] add getRandomValues, getRandomUUID, webcrypto
1 parent 460a13b commit 2142b5b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

index.js

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

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

@@ -65,6 +90,10 @@ exports.privateDecrypt = publicEncrypt.privateDecrypt
6590
// }
6691
// })
6792

93+
var { Crypto } = require('@peculiar/webcrypto')
94+
var crypto = new Crypto()
95+
exports.webcrypto = crypto
96+
6897
var rf = require('randomfill')
6998

7099
exports.randomFill = rf.randomFill

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"pbkdf2": "^3.0.3",
2929
"public-encrypt": "^4.0.0",
3030
"randombytes": "^2.0.0",
31-
"randomfill": "^1.0.3"
31+
"randomfill": "^1.0.3",
32+
"@peculiar/webcrypto": "1.4.3"
3233
},
3334
"devDependencies": {
3435
"hash-test-vectors": "~1.3.2",

0 commit comments

Comments
 (0)