mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
Password Generator finished and working
This commit is contained in:
parent
e348ea2aa9
commit
5930e8764d
5 changed files with 235 additions and 1 deletions
|
|
@ -19,7 +19,7 @@ export async function colorConvert(app: FastifyInstance) {
|
|||
return reply.status(400).send({ error: "No RGB declared!" });
|
||||
}
|
||||
const hex = (`#${(+data.red).toString(16).padStart(2, "0")}${(+data.green).toString(16).padStart(2, "0")}${(+data.blue).toString(16).padStart(2, "0")}`).toUpperCase();
|
||||
reply.header("Content-Type", "application/json").send({ hex: hex });
|
||||
reply.header("Content-Type", "text/plain").status(200).send(hex);
|
||||
} catch (error) {
|
||||
console.error("Convert error:", error);
|
||||
reply.status(500).send({ error: "Error while converting!" });
|
||||
|
|
|
|||
60
backend/src/routes/passwordgenerate.route.ts
Normal file
60
backend/src/routes/passwordgenerate.route.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
|
||||
interface RequestBody {
|
||||
length: string;
|
||||
numb: boolean;
|
||||
lower: boolean;
|
||||
upper: boolean;
|
||||
special: boolean;
|
||||
}
|
||||
|
||||
export async function passwordGenerate(app: FastifyInstance) {
|
||||
app.post(
|
||||
"/api/password-generate",
|
||||
async (
|
||||
request: FastifyRequest<{ Body: RequestBody }>,
|
||||
reply: FastifyReply,
|
||||
) => {
|
||||
try {
|
||||
const data = request.body;
|
||||
if (!data) {
|
||||
return reply.status(400).send({ error: "No length or type declared!" });
|
||||
}
|
||||
let numArray: string[] = "0123456789".split("");
|
||||
let lowerArray: string[] = "abcdefghijklmnopqrstuvwxyz".split("");
|
||||
let upperArray: string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
|
||||
let specialArray: string[] = "!@#$%^&*()_+[]{}|;:,.<>?".split("");
|
||||
let passwordArray: string[] = [];
|
||||
|
||||
if(data.numb){
|
||||
passwordArray = passwordArray.concat(numArray);
|
||||
}
|
||||
|
||||
if(data.lower){
|
||||
passwordArray = passwordArray.concat(lowerArray);
|
||||
}
|
||||
|
||||
if(data.upper){
|
||||
passwordArray = passwordArray.concat(upperArray);
|
||||
}
|
||||
|
||||
if(data.special){
|
||||
passwordArray = passwordArray.concat(specialArray);
|
||||
}
|
||||
|
||||
let password = "";
|
||||
|
||||
for (let i = 0; i < +data.length; i++) {
|
||||
const ind: number = Math.floor(Math.random() * passwordArray.length);
|
||||
const randomElement: string = passwordArray[ind];
|
||||
password = password.concat(randomElement);
|
||||
}
|
||||
|
||||
reply.header("Content-Type", "text/plain").status(200).send(password);
|
||||
} catch (error) {
|
||||
console.error("Convert error:", error);
|
||||
reply.status(500).send({ error: "Error while converting!" });
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue