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
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:
commit
7c3eb990a6
1 changed files with 1 additions and 9 deletions
|
@ -9,17 +9,9 @@ export const L_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|||
const LU_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
export function secureRndstr(length = 32, { chars = LU_CHARS } = {}): string {
|
||||
const chars_len = chars.length;
|
||||
|
||||
let str = '';
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
let rand = Math.floor((crypto.randomBytes(1).readUInt8(0) / 0xFF) * chars_len);
|
||||
if (rand === chars_len) {
|
||||
rand = chars_len - 1;
|
||||
}
|
||||
str += chars.charAt(rand);
|
||||
str += chars.charAt(crypto.randomInt(chars.length));
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue