mirror of
https://github.com/Since-3/since3-portal.git
synced 2026-10-12 19:51:17 +00:00
29 lines
859 B
TypeScript
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("")
|
|
}
|