Merge pull request 'use uniform sampling in secure-rndstr' (#33) from sugar/forkey:use-uniform-sampling-in-secure-rndstr into main
Some checks are pending
Lint / lint (misskey-js) (push) Blocked by required conditions
Lint / typecheck (misskey-js) (push) Blocked by required conditions
Test (backend) / validate-api-json (22.x) (push) Successful in 4m1s
Lint / lint (frontend) (push) Blocked by required conditions
Lint / lint (backend) (push) Blocked by required conditions
Lint / lint (sw) (push) Blocked by required conditions
Lint / typecheck (backend) (push) Blocked by required conditions
Lint / pnpm_install (push) Successful in 2m29s
Test (backend) / unit (22.x) (push) Successful in 7m5s
Test (backend) / e2e (22.x) (push) Successful in 9m11s
Test (production install and build) / production (22.x) (push) Successful in 3m10s

Reviewed-on: #33
Reviewed-by: leah <leah@noreply.woem.men>
This commit is contained in:
sugar 2025-01-17 14:22:10 +00:00
commit 7c3eb990a6

View file

@ -9,17 +9,9 @@ export const L_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
const LU_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; const LU_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
export function secureRndstr(length = 32, { chars = LU_CHARS } = {}): string { export function secureRndstr(length = 32, { chars = LU_CHARS } = {}): string {
const chars_len = chars.length;
let str = ''; let str = '';
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
let rand = Math.floor((crypto.randomBytes(1).readUInt8(0) / 0xFF) * chars_len); str += chars.charAt(crypto.randomInt(chars.length));
if (rand === chars_len) {
rand = chars_len - 1;
} }
str += chars.charAt(rand);
}
return str; return str;
} }