generate Password

This commit is contained in:
NK 2023-04-21 22:04:50 +01:00
parent 2c4f56c9b3
commit 66dbc4fd86
3 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,35 @@
/**
* Generate a random password
* @param length The length of the password
* @param options The options for the password
*/
export function generatePassword(
length: number,
options: GeneratePasswordOptions
) {
const upper = options.upper || false;
const lower = options.lower || false;
const numbers = options.numbers || false;
const special = options.special || false;
const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerChars = "abcdefghijklmnopqrstuvwxyz";
const numberChars = "0123456789";
const specialChars = "!@#$%^&*()_+-=[]{}|;':\",./<>?";
let password = "";
let chars = "";
if (upper) chars += upperChars;
if (lower) chars += lowerChars;
if (numbers) chars += numberChars;
if (special) chars += specialChars;
for (let i = 0; i < length; i++) {
password += chars.charAt(Math.floor(Math.random() * chars.length));
}
return password;
}
export interface GeneratePasswordOptions {
upper: boolean;
lower: boolean;
numbers: boolean;
special: boolean;
}

4
src/index.d.ts vendored
View file

@ -40,3 +40,7 @@ export { superscript } from "./functions/superscript";
export { doublestruck } from "./functions/doublestruck"; export { doublestruck } from "./functions/doublestruck";
export { fractur } from "./functions/fractur"; export { fractur } from "./functions/fractur";
export { round } from "./functions/round"; export { round } from "./functions/round";
export {
generatePassword,
GeneratePasswordOptions,
} from "./functions/generatePassword";

View file

@ -45,6 +45,10 @@ export { doublestruck } from "./functions/doublestruck";
export { fractur } from "./functions/fractur"; export { fractur } from "./functions/fractur";
export { randomID } from "./functions/randomID"; export { randomID } from "./functions/randomID";
export { round } from "./functions/round"; export { round } from "./functions/round";
export {
generatePassword,
GeneratePasswordOptions,
} from "./functions/generatePassword";
const config: ConfigURLS = { const config: ConfigURLS = {
api: "https://api.night0721.me", api: "https://api.night0721.me",