DIF_Team_13/uebung_03/1. Ansatz/Programmcode.md

42 lines
1.3 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

**passwordgenerator.py**
```
from itertools import product
def generate_passwords(digit_chars, letter_chars, pattern_structure):
"""
Generiert Passwörter anhand einer Strukturangabe.
:param digit_chars: Erlaubte Ziffern (z.B. '01234')
:param letter_chars: Erlaubte Buchstaben (z.B. 'defghi')
:param pattern_structure: Liste mit Symbolen: 'D' für Ziffer, 'L' für Buchstabe
:return: Generator für Passwörter
"""
charset_map = {
'D': digit_chars,
'L': letter_chars
}
# Erzeuge eine Liste von Zeichensätzen für jede Position im Muster
charsets = [charset_map[symbol] for symbol in pattern_structure]
# Erzeuge alle Kombinationen
for combo in product(*charsets):
yield ''.join(combo)
def save_passwords_to_file(password_generator, output_path):
"""
Speichert generierte Passwörter in eine Datei.
"""
with open(output_path, 'w') as file:
for pw in password_generator:
file.write(pw + '\n')
if __name__ == "__main__":
digits = "01234"
letters = "defghi"
structure = ['D', 'D', 'L', 'L', 'L', 'D', 'D'] # Beispiel: 2 Zahlen, 3 Buchstaben, 2 Zahlen
pw_gen = generate_passwords(digits, letters, structure)
save_passwords_to_file(pw_gen, "wordlist.txt")
```