mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-28 01:04:05 +00:00
Update util.js
This commit is contained in:
parent
11bcd1e2ed
commit
0e6d7694ce
1 changed files with 13 additions and 4 deletions
15
src/util.js
15
src/util.js
|
@ -102,12 +102,21 @@ function getRandomInt(min, max) {
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
}
|
}
|
||||||
exports.getRandomInt = getRandomInt;
|
exports.getRandomInt = getRandomInt;
|
||||||
|
function getCryptoRandomInt(min, max) {
|
||||||
|
const randomBuffer = new Uint32Array(1);
|
||||||
|
crypto.getRandomValues(randomBuffer);
|
||||||
|
const randomNumber = randomBuffer[0] / (0xffffffff + 1);
|
||||||
|
min = Math.ceil(min);
|
||||||
|
max = Math.floor(max);
|
||||||
|
return Math.floor(randomNumber * (max - min + 1)) + min;
|
||||||
|
}
|
||||||
|
exports.getCryptoRandomInt = getCryptoRandomInt;
|
||||||
function genSecret(length = 64) {
|
function genSecret(length = 64) {
|
||||||
let secret = "";
|
let secret = "";
|
||||||
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
let charsLength = chars.length;
|
const charsLength = chars.length;
|
||||||
for ( let i = 0; i < length; i++ ) {
|
for ( let i = 0; i < length; i++ ) {
|
||||||
secret += chars.charAt(Math.floor(Math.random() * charsLength));
|
secret += chars.charAt(getCryptoRandomInt(0, charsLength));
|
||||||
}
|
}
|
||||||
return secret;
|
return secret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue