- Cuenta
- Hachís de semillas siguiente
- Semilla revelada
- Resultado de repartir
#!/usr/bin/env node
const crypto = require("crypto");
class LuckyNumberGenerator {
constructor(numbersCount = 312) {
this.numbers = Array(numbersCount).fill(false);
this.numbersCount = numbersCount;
}
selectNumber(payload, secret) {
const hmac = cryptolib
.createHmac("sha512", payload)
.update(secret)
.digest("hex");
let f7 = hmac.substring(0, 7);
let int = parseInt(f7, 16);
let number = this.reduce(int, 0, this.numbersCount - 1);
// Ensure the number hasn't been drawn yet.
while (this.numbers[number]) {
int = (int + 1) % (Math.pow(2, 28)); // Increment the integer, wrapping around if necessary
number = this.reduce(int, 0, this.numbersCount - 1);
}
this.numbers[number] = true; // Mark the number as drawn
return number;
}
selectNumbers(payload, secret, count = 6) {
const numbers = [];
for (let i = 0; i < count; i++) {
numbers.push(this.selectNumber(payload + i, secret)); // Append an index to the payload for unique hashes
}
return numbers;
}
// use to reduce a number to appropriate size
reduce = (number, outMin, outMax) =>
(number % ((outMax - outMin) + 1)) + outMin;
}
// const luckyNumbersGenerator = new LuckyNumberGenerator();
// const uid = '';
// const secret = '';
// const luckyNumbers = luckyNumbersGenerator.selectNumbers(uid, secret, 6);
// console.log(luckyNumbers); === undefined