-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutils.js
53 lines (51 loc) · 1.54 KB
/
utils.js
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
47
48
49
50
51
52
53
const crypto = require('crypto');
/**
* Hashed a string
* @param string
* @returns {string}
*/
const sha512 = string =>
crypto
.createHash('sha512')
.update(string)
.digest('hex');
/**
* Generate a 256 characters string
* @returns {string}
*/
const generateServerSeed = () => crypto.randomBytes(256).toString('hex');
/**
* Custom function for concatenating server-seed, client-seed and nonce.
* @param serverSeed
* @param clientSeed
* @param nonce
* @returns {*}
*/
const combine = (serverSeed, clientSeed, nonce) =>
serverSeed + clientSeed + nonce;
/**
* Converts a hex hash into a number between [0, 99]
* @param hashedValue
* @returns {*}
*/
const getResult = hashedValue => {
// the offset of the interval
let index = 0;
// result variable
let result;
do {
// get the decimal value from an interval of 5 hex letters
result = parseInt(hashedValue.substring(index * 5, index * 5 + 5), 16);
// increment the offset in case we will need to repeat the operation above
index += 1;
// if all the numbers were over 999999 and we reached the end of the string, we set that to a default value of 9999 (99 as a result)
if (index * 5 + 5 > 129) {
result = 9999;
break;
}
} while (result >= 1e6);
// the result is between 0-999999 and we need to convert if into a 4 digit number
// we a apply a modulus of 1000 and the 4 digit number is further split into a 2 digit number with decimals
return [result % 1e4] * 1e-2;
};
module.exports = { generateServerSeed, sha512, combine, getResult };