-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprng.js
More file actions
46 lines (42 loc) · 1.09 KB
/
prng.js
File metadata and controls
46 lines (42 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var constants = require('./constants');
var rp = require('request-promise');
var querystring = require('querystring');
module.exports = function(type) {
getEntropy()
.then((res) => {
var entropy = res.data.join('');
var randomness = generateRandomness(type, entropy);
console.info(randomness);
});
}
function generateRandomness(type, entropy) {
// converting string to base64 in node
var randomness = Buffer.from(entropy, 'base64');
switch(type) {
case constants.hex:
return randomness.toString('hex');
case constants.bin:
default:
// node base64 is already binary
return randomness.toString();
}
}
function getEntropy(type = 'hex16', length = 10, size = 10) {
var SOURCE_URL = 'https://qrng.anu.edu.au/API/jsonI.php';
var queryParams = querystring.stringify({
type,
length,
size
});
var url = `${SOURCE_URL}?${queryParams}`;
var options = {
method: 'get',
url,
json: true
}
return rp(options, (err, res, body) => {
if (err === null && body.data && body.data.length) {
body.data.join('');
}
});
}