ef354e94f2
* refactor(backend): replace rndstr with secureRndstr * Update pnpm-lock.yaml * .js
20 lines
539 B
TypeScript
20 lines
539 B
TypeScript
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;
|
|
}
|