nurjns

Simple Passwort Generator Website

Dec 19th, 2024 (edited)
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="de">
  3. <head>
  4.     <meta name="viewport" content="width=device-width, initial-scale=1">
  5.     <title>Passwort-Generator</title>
  6.     <style>
  7.         body {
  8.             font-family: Arial, sans-serif;
  9.             background-color: #f4f4f9;
  10.             color: #333;
  11.             display: flex;
  12.             justify-content: center;
  13.             align-items: flex-start;
  14.             margin: 0;
  15.             padding-top: 200px;
  16.         }
  17.  
  18.         #length-slider {
  19.             width: 300px;
  20.         }
  21.  
  22.         .container {
  23.             text-align: center;
  24.             background-color: #ffffff;
  25.             border-radius: 10px;
  26.             box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  27.             padding: 20px;
  28.             width: 350px;
  29.         }
  30.  
  31.         label, #length-display {
  32.             font-size: 18px;
  33.             font-weight: bold;
  34.             margin-bottom: 10px;
  35.             display: block;
  36.         }
  37.  
  38.         label {
  39.             font-weight: normal;
  40.         }
  41.  
  42.         #generate-button {
  43.             background-color: #007bff;
  44.             color: #fff;
  45.             border: none;
  46.             border-radius: 5px;
  47.             padding: 10px 20px;
  48.             font-size: 16px;
  49.             cursor: pointer;
  50.             transition: background-color 0.3s ease, transform 0.1s ease;
  51.         }
  52.  
  53.         #generate-button:hover {
  54.             background-color: #0056b3;
  55.         }
  56.  
  57.         #generate-button:active {
  58.             transform: scale(0.98);
  59.         }
  60.  
  61.         #password-field {
  62.             text-align: center;
  63.             width: 80%;
  64.             margin: 15px auto;
  65.             border: 1px solid #ccc;
  66.             border-radius: 5px;
  67.             padding: 10px;
  68.             box-sizing: border-box;
  69.             background-color: #f9f9f9;
  70.             font-family: monospace;
  71.             font-size: 14px;
  72.         }
  73.  
  74.         label:has(#simple-specials)  {
  75.             font-size: 16px;
  76.         }
  77.  
  78.         #copied-text {
  79.             font-size: 16px;
  80.             color: #28a745;
  81.             display: none;
  82.         }
  83.  
  84.         #copied-text.active {
  85.             display: block;
  86.         }
  87.     </style>
  88. </head>
  89. <body>
  90.     <div class="container">
  91.         <label>Passwortlänge:</label>
  92.         <input type="range" min="8" max="30" value="12" class="slider" id="length-slider">
  93.         <span id="length-display">12</span><br>
  94.         <label><input type="checkbox" id="simple-specials" checked> Nur einfache Sonderzeichen</label><br><br>
  95.         <button id="generate-button">Passwort generieren</button>
  96.         <input type="text" id="password-field" value="" readonly>
  97.         <div id="copied-text">Passwort kopiert!</div>
  98.     </div>
  99.  
  100.     <script>
  101.     function generatePassword() {
  102.         const useSimpleSpecials = document.querySelector('#simple-specials').checked;
  103.         const simpleSpecials = ['!', '-'];
  104.         const fullSpecials = ['!', '$', '&', '-', ';', '*'];
  105.         const specialCharacters = useSimpleSpecials ? simpleSpecials : fullSpecials;
  106.         const numberCharacters = [...'234678'];
  107.         const upperLetterCharacters = [...'ACDEFHJKMNPQRTUWX'];
  108.         const lowerLetterCharacters = [...'abcdefhjkmnpqrtuwx'];
  109.         const letterCharacters = [...upperLetterCharacters, ...lowerLetterCharacters];
  110.         const possibleCharacters = [
  111.             ...numberCharacters,
  112.             ...letterCharacters,
  113.             ...specialCharacters
  114.         ];
  115.  
  116.         const passwordLength = document.querySelector('#length-slider').value;
  117.         const password = new Array(passwordLength);
  118.         const randomFrom = (arr, filterFn = null) => {
  119.             let filtered = filterFn ? arr.filter(filterFn) : arr;
  120.             return filtered[Math.floor(Math.random() * filtered.length)];
  121.         };
  122.  
  123.         // Anfang und Ende sind keine Sonderzeichen
  124.         const isAlphanumeric = c => /[a-zA-Z0-9]/.test(c);
  125.         password[0] = randomFrom(possibleCharacters, isAlphanumeric);
  126.         password[passwordLength - 1] = randomFrom(possibleCharacters, isAlphanumeric);
  127.         for (let i = 1; i < passwordLength - 1; i++) {
  128.             const prevCharIsSpecial = specialCharacters.includes(password[i - 1]);
  129.             password[i] = prevCharIsSpecial
  130.                 ? randomFrom(possibleCharacters, isAlphanumeric)
  131.                 : randomFrom(possibleCharacters);
  132.         }
  133.  
  134.         // Mindestanzahl Sonderzeichen: 1 bei einfachen, sonst 2 ab 10 Zeichen, 3 ab 15, usw.
  135.         const minSpecials = useSimpleSpecials ? 1 : Math.max(1, Math.floor(passwordLength / 5));
  136.         const isSpecial = c => specialCharacters.includes(c);
  137.         let specialsCount = password.filter(isSpecial).length;
  138.         let attempts = 0;
  139.  
  140.         while (specialsCount < minSpecials && attempts < passwordLength * 10) {
  141.             const index = Math.floor(Math.random() * (passwordLength - 2)) + 1;
  142.             const neighborIsSpecial = isSpecial(password[index - 1]) || isSpecial(password[index + 1]);
  143.             if (!isSpecial(password[index]) && !neighborIsSpecial) {
  144.                 password[index] = randomFrom(specialCharacters);
  145.                 specialsCount++;
  146.             }
  147.             attempts++;
  148.         }
  149.  
  150.         // Sicherstellen, dass mindestens ein Buchstabe und eine Zahl vorhanden sind
  151.         const nonSpecialIndexes = () => password.map((c, i) => i).filter(i => !isSpecial(password[i]));
  152.         if (!password.some(c => letterCharacters.includes(c))) {
  153.             password[randomFrom(nonSpecialIndexes())] = randomFrom(letterCharacters);
  154.         }
  155.  
  156.         if (!password.some(c => numberCharacters.includes(c))) {
  157.             password[randomFrom(nonSpecialIndexes())] = randomFrom(numberCharacters);
  158.         }
  159.  
  160.         return password.join('');
  161.     }
  162.  
  163.     document.querySelector('#generate-button').addEventListener('click', () => {
  164.         document.querySelector('#password-field').value = generatePassword();
  165.     });
  166.  
  167.     document.addEventListener('DOMContentLoaded', () => {
  168.         document.querySelector('#password-field').value = generatePassword();
  169.     });
  170.  
  171.     document.querySelector('#length-slider').addEventListener('input', () => {
  172.         document.querySelector('#length-display').textContent = document.querySelector('#length-slider').value;
  173.         document.querySelector('#password-field').value = generatePassword();
  174.     });
  175.  
  176.     document.querySelector('#simple-specials').addEventListener('change', () => {
  177.         document.querySelector('#password-field').value = generatePassword();
  178.     });
  179.  
  180.     document.querySelector('#password-field').addEventListener('click', () => {
  181.         document.querySelector('#password-field').select();
  182.         document.execCommand('copy');
  183.         $('#copied-text').slideDown(250).delay(1000).slideUp(250);
  184.     });
  185.     </script>
  186.     <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
  187. </body>
  188. </html>
Advertisement
Add Comment
Please, Sign In to add comment