Sicheres Passwort

Kleiner Passwort Generator

<?php
function generiereSicheresPasswort($laenge) {
    $zeichen = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+{}[]|;:<>,.?/';
    $zeichenLaenge = strlen($zeichen);
    $passwort = '';
    
    for ($i = 0; $i < $laenge; $i++) {
        $passwort .= $zeichen[rand(0, $zeichenLaenge - 1)];
    }
    
    return $passwort;
}

function berechnePasswortStaerke($passwort) {
    $laenge = strlen($passwort);
    $vielfalt = 0;
    if (preg_match('/[0-9]/', $passwort)) $vielfalt += 10;
    if (preg_match('/[a-z]/', $passwort)) $vielfalt += 26;
    if (preg_match('/[A-Z]/', $passwort)) $vielfalt += 26;
    if (preg_match('/[!@#$%^&*()-_=+{}[\]|;:<>,.?\/]/', $passwort)) $vielfalt += 30;

    $staerke = $laenge * log($vielfalt);
    return min($staerke, 100); // Maximale Stärke auf 100 setzen
}

$passwort = '';
$staerke = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $laenge = intval($_POST['laenge']);
    if ($laenge < 8) {
        $passwort = "Das Passwort sollte mindestens 8 Zeichen lang sein.";
    } else {
        $passwort = generiereSicheresPasswort($laenge);
        $staerke = berechnePasswortStaerke($passwort);
    }
}
?>

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sicheres Passwort generieren</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
            width: 100%;
            max-width: 400px;
        }

        h1 {
            margin-bottom: 20px;
            color: #333;
        }

        label {
            display: block;
            margin-bottom: 10px;
            font-weight: bold;
        }

        input[type="number"] {
            width: calc(100% - 22px);
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        input[type="submit"] {
            background-color: #28a745;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        input[type="submit"]:hover {
            background-color: #218838;
        }

        .passwort-output {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
            cursor: pointer;
            user-select: none;
        }

        .passwort-output:hover {
            color: #007bff;
        }

        .staerke-bar {
            margin-top: 20px;
            height: 20px;
            width: 100%;
            background-color: #ccc;
            border-radius: 5px;
            overflow: hidden;
        }

        .staerke-bar-inner {
            height: 100%;
            width: 0;
            transition: width 0.3s;
            border-radius: 5px;
        }

        .staerke-label {
            margin-top: 10px;
            font-weight: bold;
        }

        .kopiert-hinweis {
            color: #28a745;
            font-size: 14px;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Sicheres Passwort generieren</h1>
        <form method="post" action="">
            <label for="laenge">Passwortlänge:</label>
            <input type="number" id="laenge" name="laenge" min="8" required>
            <input type="submit" value="Generieren">
        </form>
        <?php if ($passwort && !is_numeric($passwort)): ?>
            <div class="passwort-output" id="passwortOutput" onclick="kopierePasswort()">
                <?php echo htmlspecialchars($passwort); ?>
            </div>
            <div class="staerke-bar">
                <div class="staerke-bar-inner" style="width: <?php echo $staerke; ?>%; background-color: <?php echo $staerke > 75 ? '#28a745' : ($staerke > 50 ? '#ffc107' : '#dc3545'); ?>;"></div>
            </div>
            <div class="staerke-label">
                Passwortstärke: <?php echo intval($staerke); ?> / 100
            </div>
            <div class="kopiert-hinweis" id="kopiertHinweis">
                Passwort in den Zwischenspeicher kopiert!
            </div>
        <?php elseif ($passwort): ?>
            <div class="passwort-output" style="color: red;">
                <?php echo htmlspecialchars($passwort); ?>
            </div>
        <?php endif; ?>
    </div>

    <script>
        function kopierePasswort() {
            var passwortText = document.getElementById("passwortOutput").innerText;
            navigator.clipboard.writeText(passwortText).then(function() {
                var hinweis = document.getElementById("kopiertHinweis");
                hinweis.style.display = "block";
                setTimeout(function() {
                    hinweis.style.display = "none";
                }, 2000);
            });
        }
    </script>
</body>
</html>
  • Speichere das Skript als .php-Datei auf deinem Server.
  • Öffne die Datei in einem Webbrowser, generiere ein Passwort, und klicke darauf, um es in den Zwischenspeicher zu kopieren. Der Hinweis zeigt an, dass das Passwort erfolgreich kopiert wurde.