generate Password
This commit is contained in:
parent
2c4f56c9b3
commit
66dbc4fd86
3 changed files with 43 additions and 0 deletions
35
src/functions/generatePassword.ts
Normal file
35
src/functions/generatePassword.ts
Normal 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
4
src/index.d.ts
vendored
|
@ -40,3 +40,7 @@ export { superscript } from "./functions/superscript";
|
|||
export { doublestruck } from "./functions/doublestruck";
|
||||
export { fractur } from "./functions/fractur";
|
||||
export { round } from "./functions/round";
|
||||
export {
|
||||
generatePassword,
|
||||
GeneratePasswordOptions,
|
||||
} from "./functions/generatePassword";
|
||||
|
|
|
@ -45,6 +45,10 @@ export { doublestruck } from "./functions/doublestruck";
|
|||
export { fractur } from "./functions/fractur";
|
||||
export { randomID } from "./functions/randomID";
|
||||
export { round } from "./functions/round";
|
||||
export {
|
||||
generatePassword,
|
||||
GeneratePasswordOptions,
|
||||
} from "./functions/generatePassword";
|
||||
|
||||
const config: ConfigURLS = {
|
||||
api: "https://api.night0721.me",
|
||||
|
|
Loading…
Reference in a new issue