summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2020-01-16 17:16:54 +0100
committertomsmeding <tom.smeding@gmail.com>2020-01-16 17:43:04 +0100
commit20b489a6cfd69275c896136a3f7aac2726faebd6 (patch)
treeb2242db4d3ed0e0fc324d88d0fe353dc3e4055a7 /server
parentdacca8575cb38695b39dc9e885685e6ef16ae62f (diff)
server: Uniformly random id generator
Diffstat (limited to 'server')
-rwxr-xr-xserver/serverstore.js15
1 files changed, 14 insertions, 1 deletions
diff --git a/server/serverstore.js b/server/serverstore.js
index e10ae9a..745877f 100755
--- a/server/serverstore.js
+++ b/server/serverstore.js
@@ -30,9 +30,22 @@ renewChallenge();
function gencode(){
var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+ var domainsize = Math.pow(256, 5);
+ var targetsize = Math.pow(62, 6);
+ var limit = domainsize - (domainsize % targetsize);
+
+ var number;
+ do {
+ var bytes = crypto.randomBytes(5); // 62^6 < 256^5, so large enough
+ number = 0; // 256^5 < 2^52, so fits in JS's double
+ for (var i = 0; i < 5; i++) number = 256 * number + bytes[i];
+ } while (number >= limit); // ensure uniformity
+
var code = "";
for(var i = 0; i < 6; i++) {
- code += alphabet[~~(Math.random() * alphabet.length)];
+ code += alphabet[number % 62];
+ number = Math.floor(number / 62);
}
return code;
}