since3-portal/lib/generate-password.ts
2026-09-27 18:57:56 +02:00

29 lines
859 B
TypeScript

import { randomInt } from "crypto"
const LOWERCASE = "abcdefghijkmnpqrstuvwxyz"
const UPPERCASE = "ABCDEFGHJKLMNPQRSTUVWXYZ"
const DIGITS = "23456789"
const SYMBOLS = "!@#$%&*"
const ALL_CHARS = LOWERCASE + UPPERCASE + DIGITS + SYMBOLS
export function generatePassword(length = 14) {
const required = [
LOWERCASE[randomInt(LOWERCASE.length)],
UPPERCASE[randomInt(UPPERCASE.length)],
DIGITS[randomInt(DIGITS.length)],
SYMBOLS[randomInt(SYMBOLS.length)],
]
const rest = Array.from({ length: length - required.length }, () =>
ALL_CHARS[randomInt(ALL_CHARS.length)]
)
const password = [...required, ...rest]
for (let i = password.length - 1; i > 0; i--) {
const j = randomInt(i + 1)
;[password[i], password[j]] = [password[j], password[i]]
}
return password.join("")
}