-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence.js
44 lines (37 loc) · 1.1 KB
/
sequence.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
class SequenceEngine {
constructor(key, mod) {
this.key = key;
this.mod = mod;
}
async next() {
this.key = await this.calculateSHA256Hash(this.key);
const val = 1+this.hash(this.key);
return val;
}
hash(key) {
let hashValue = 0;
for (let i = 0; i < key.length; i++) {
hashValue = (hashValue * 31 + key.charCodeAt(i)) % (this.mod);
}
return hashValue;
}
async calculateSHA256Hash(input) {
const encoder = new TextEncoder();
const data = encoder.encode(input);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashedString = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashedString;
}
updateMod(mod) {
this.mod = mod;
}
}
// working fine
async function start() {
const engine = new SequenceEngine("hello", 23);
for(let i = 0;i < 100;i++) {
const num = await engine.next();
console.log(num);
}
}