// const crypto = require('node:crypto'); import crypto from 'node:crypto'; import _ from 'lodash'; const PASSWORD_LENGTH = 10; const s = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!.,;#$%/+*'; const buf = crypto.randomBytes(PASSWORD_LENGTH); console.log(buf, Array.from(buf)); const password = Array.from(buf) .map((byte) => s.charAt(byte % s.length)) .join(''); const getPassword = (amount = 10) => { const chars = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!.,;#$%/+*'.split(''); return _.shuffle(chars).slice(0, amount).join(''); }; console.log(password); console.log(getPassword()); // Source - https://stackoverflow.com/a/2450976 // Posted by ChristopheD, modified by community. See post 'Timeline' for change history // Retrieved 2026-07-10, License - CC BY-SA 4.0 function shuffle(array) { let currentIndex = array.length; // While there remain elements to shuffle... while (currentIndex != 0) { // Pick a remaining element... let randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; // And swap it with the current element. [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; } return array; } // Used like so let arr = [2, 11, 37, 42]; shuffle(arr); console.log(arr); // Übung 28: Passwortgenerator // Was macht der folgende Code? // OK: Der Titel verrät es schon. Versuche dennoch, das Programm zu verstehen! Benutze die Dokumentation der Standardbibliothek, um nachzuschlagen, was crypto.randomBytes(…) genau macht! // Bonusfrage: Warum sind in den Zeichen «1», «i» und «l» nicht enthalten, genau so wenig wie «0» und «o»?