2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-12 03:37:45 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
import * as crypto from 'node:crypto';
|
2020-03-29 16:16:36 +02:00
|
|
|
|
2023-06-25 04:04:33 +02:00
|
|
|
export const L_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
|
2020-03-29 16:16:36 +02:00
|
|
|
const LU_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
|
2023-06-25 04:04:33 +02:00
|
|
|
export function secureRndstr(length = 32, { chars = LU_CHARS } = {}): string {
|
2020-03-29 16:16:36 +02:00
|
|
|
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;
|
|
|
|
}
|