mirror of
https://github.com/misskey-dev/misskey.git
synced 2024-12-29 22:59:20 +09:00
c1514ce91d
Fix #13290
26 lines
643 B
TypeScript
26 lines
643 B
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import * as crypto from 'node:crypto';
|
|
|
|
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);
|
|
}
|
|
|
|
return str;
|
|
}
|