summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/secure-rndstr.ts
blob: 1fa686c0dd794c05cfff4d4ebde191ebae5604f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * 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++) {
		const rand = crypto.randomInt(0, chars_len);
		str += chars.charAt(rand);
	}

	return str;
}