This commit is contained in:
@@ -1,16 +1,31 @@
|
||||
// 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('s.length:', s.length); // => 64
|
||||
console.log('buf:', buf); // => <Buffer 9a 28 4f d6 32 7a 6d 44 f0 3e>
|
||||
console.log('Array:', Array.from(buf)); // => [ 17, 181, 126, 182, 190, 212, 137, 233, 140, 75]
|
||||
const password = Array.from(buf)
|
||||
.map((byte) => s.charAt(byte % s.length))
|
||||
.map((byte) => s.charAt(byte % s.length)) // => kZ+!+pbMed
|
||||
.join('');
|
||||
|
||||
console.log(password);
|
||||
|
||||
const generatePassword = (length) => {
|
||||
const chars = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!.,;#$%/+*'.split('');
|
||||
return _.shuffle(chars)
|
||||
.slice(0, length + 1)
|
||||
.join('');
|
||||
};
|
||||
|
||||
const pwd = generatePassword(10);
|
||||
console.log('Password: ', pwd);
|
||||
|
||||
// Übung 28: Passwortgenerator
|
||||
|
||||
// Was macht der folgende Code?
|
||||
@@ -18,3 +33,7 @@ console.log(password);
|
||||
// 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»?
|
||||
|
||||
// const asciiLetters = Array.from({ length: 26 }, (_, i) =>
|
||||
// String.fromCharCode(65 + i) + String.fromCharCode(97 + i)
|
||||
// ).join(''); // "AaBbCc..." oder getrennt sortieren
|
||||
|
||||
Reference in New Issue
Block a user