mirror of
https://github.com/kokonect-link/cherrypick
synced 2024-12-14 06:38:28 +09:00
d071d18dd7
* wip * wip * fix * clean up * Update tsconfig.json * Update activitypub.ts * wip
22 lines
562 B
TypeScript
22 lines
562 B
TypeScript
import * as crypto from 'node:crypto';
|
|
|
|
const L_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
const LU_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
export function secureRndstr(length = 32, useLU = true): string {
|
|
const chars = useLU ? LU_CHARS : L_CHARS;
|
|
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;
|
|
}
|