1 parent 7ade628 commit a98b5b6Copy full SHA for a98b5b6
1 file changed
apps/javascript/src/challenges/password-generator/index.js
@@ -94,8 +94,17 @@ const APLHABETWORDS = {
94
};
95
96
const secureRandomIndex = (length) => {
97
- if (length <= 0) return 0;
98
- return crypto.getRandomValues(new Uint32Array(1))[0] % length;
+ if (length <= 1) return 0;
+
99
+ const buckets = Math.floor(0x100000000 / length);
100
+ const limit = buckets * length;
101
+ const buf = new Uint32Array(1);
102
+ let value;
103
+ do {
104
+ crypto.getRandomValues(buf);
105
+ value = buf[0];
106
+ } while (value >= limit);
107
+ return Math.floor(value / buckets);
108
109
110
const functionMap = {
0 commit comments