added backup and email client
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// ==================== PASSWORD GENERATOR ====================
|
||||
// Generiert sichere, zufällige Passwörter
|
||||
|
||||
import { randomBytes } from 'crypto';
|
||||
|
||||
// Zeichensätze für Passwort-Generierung
|
||||
const LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
|
||||
const UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
const NUMBERS = '0123456789';
|
||||
const SPECIAL = '!@#$%^&*()_+-=[]{}|;:,.<>?';
|
||||
|
||||
// Standard-Passwortlänge
|
||||
const DEFAULT_LENGTH = 16;
|
||||
|
||||
export interface PasswordOptions {
|
||||
length?: number;
|
||||
includeLowercase?: boolean;
|
||||
includeUppercase?: boolean;
|
||||
includeNumbers?: boolean;
|
||||
includeSpecial?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generiert ein kryptografisch sicheres Passwort
|
||||
*/
|
||||
export function generateSecurePassword(options: PasswordOptions = {}): string {
|
||||
const {
|
||||
length = DEFAULT_LENGTH,
|
||||
includeLowercase = true,
|
||||
includeUppercase = true,
|
||||
includeNumbers = true,
|
||||
includeSpecial = true,
|
||||
} = options;
|
||||
|
||||
// Zeichensatz zusammenstellen
|
||||
let charset = '';
|
||||
const requiredChars: string[] = [];
|
||||
|
||||
if (includeLowercase) {
|
||||
charset += LOWERCASE;
|
||||
requiredChars.push(getRandomChar(LOWERCASE));
|
||||
}
|
||||
if (includeUppercase) {
|
||||
charset += UPPERCASE;
|
||||
requiredChars.push(getRandomChar(UPPERCASE));
|
||||
}
|
||||
if (includeNumbers) {
|
||||
charset += NUMBERS;
|
||||
requiredChars.push(getRandomChar(NUMBERS));
|
||||
}
|
||||
if (includeSpecial) {
|
||||
charset += SPECIAL;
|
||||
requiredChars.push(getRandomChar(SPECIAL));
|
||||
}
|
||||
|
||||
if (charset.length === 0) {
|
||||
throw new Error('Mindestens ein Zeichensatz muss aktiviert sein');
|
||||
}
|
||||
|
||||
// Restliche Zeichen auffüllen
|
||||
const remainingLength = Math.max(0, length - requiredChars.length);
|
||||
const randomChars: string[] = [];
|
||||
|
||||
for (let i = 0; i < remainingLength; i++) {
|
||||
randomChars.push(getRandomChar(charset));
|
||||
}
|
||||
|
||||
// Alle Zeichen mischen (Fisher-Yates Shuffle)
|
||||
const allChars = [...requiredChars, ...randomChars];
|
||||
for (let i = allChars.length - 1; i > 0; i--) {
|
||||
const j = getRandomInt(i + 1);
|
||||
[allChars[i], allChars[j]] = [allChars[j], allChars[i]];
|
||||
}
|
||||
|
||||
return allChars.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generiert ein einfaches Passwort ohne Sonderzeichen (für APIs die das nicht mögen)
|
||||
*/
|
||||
export function generateSimplePassword(length = 12): string {
|
||||
return generateSecurePassword({
|
||||
length,
|
||||
includeLowercase: true,
|
||||
includeUppercase: true,
|
||||
includeNumbers: true,
|
||||
includeSpecial: false,
|
||||
});
|
||||
}
|
||||
|
||||
// Kryptografisch sichere Zufallszahl
|
||||
function getRandomInt(max: number): number {
|
||||
const bytes = randomBytes(4);
|
||||
const value = bytes.readUInt32BE(0);
|
||||
return value % max;
|
||||
}
|
||||
|
||||
// Zufälliges Zeichen aus einem Zeichensatz
|
||||
function getRandomChar(charset: string): string {
|
||||
return charset[getRandomInt(charset.length)];
|
||||
}
|
||||
Reference in New Issue
Block a user