Beispiel:Passwort-Generator.html

Aus SELFHTML-Wiki
Wechseln zu: Navigation, Suche

<!DOCTYPE html> <html lang="de">

 <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width">

<title>Passwort-Generator</title> </head>

<body> <label>

 Länge:
 <input id="length" type="number" value="15" min="8" max="100">

</label>

<button id="generate">Passwort erzeugen</button>

<output id="password"></output>

<script> const chars =

 '0123456789' +
 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
 'abcdefghijklmnopqrstuvwxyz' +
 'äÄöÖüÜß' +
 '!§$%&/()=?`*+#,.;:-_';

const length = document.querySelector('#length');

function generatePassword(length) {

 const values = new Uint32Array(length);
 crypto.getRandomValues(values);
 return [...values]
   .map(value => chars[value % chars.length])
   .join();

}

generate.addEventListener('click', () => {

 password.textContent = generatePassword(Number(length.value));

}); </script> </body> </html>