-
Notifications
You must be signed in to change notification settings - Fork 0
/
idGenerator.js
55 lines (41 loc) · 1.08 KB
/
idGenerator.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
54
55
var crypto = require('crypto');
const sha1 = require('sha1');
var k = 8;
var newID = function (id){
//console.log(id);
var toBeHashed = ''+id;
var shasum = crypto.createHash('sha1');
shasum.update(toBeHashed);
var hashedVal = shasum.digest('hex');
// console.log('hashedVal');
// console.log(hashedVal);
var ID = hex2bin(hashedVal);
// console.log('ID before being cut');
// console.log(ID);
ID = ID.substr(0,k);
//var ID = parseInt('0x'+hashedVal,2);
//var ID = leftPad(randomIntInc(0, 255).toString(2),8);
return ID;
}
function randomIntInc (low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
var leftPad = function (str, length) {
str = str == null ? '' : String(str);
length = ~~length;
pad = '';
padLength = length - str.length;
while(padLength--) {
pad += '0';
}
return pad + str;
};
function hex2bin(hex){
return ((parseInt(hex, 16)).toString(2)).substr(8);
//Før ændring
//return ("00000000" + (parseInt(hex, 16)).toString(2)).substr(8);
}
module.exports = {
newID,
leftPad
};