ciel-b1/src/cours/CIEL1/01-bases-python/tp/code/mdp.py

69 lines
2.2 KiB
Python
Raw Normal View History

import secrets
import string
from eff_words import get_word_list, get_words_from_file
LETTRES = string.ascii_letters
CHIFFRES = string.digits
SPECIAL = string.punctuation
ALPHABET = LETTRES + CHIFFRES + SPECIAL
DICE_NB = 5
def generer_mdp(password_length = 14):
password = ""
for i in range(password_length):
password += secrets.choice(ALPHABET)
return password
def generer_mdp_with_constraints(password_length = 14, special = 1, digits = 1):
password = ""
contrainte_special_respectee = False
contrainte_digits_respectee = False
while not (contrainte_special_respectee and contrainte_digits_respectee):
password = generer_mdp(password_length=password_length)
contrainte_special_respectee = sum(char in SPECIAL for char in password) >= special
contrainte_digits_respectee = sum(char in CHIFFRES for char in password) >= digits
return password
def generer_mdp_personnalise():
password_length = int(input("Longueur du mot de passe: "))
return generer_mdp(password_length=password_length)
def generer_passphrase_v1(nb_words = 6):
words = get_word_list()
chosen_words = []
for _ in range(nb_words):
next_word = secrets.choice(words)
chosen_words.append(next_word)
return '-'.join(chosen_words)
def generer_passphrase_v2(nb_words = 6, add_digit=False, capitalize=True, sep='-'):
words = get_words_from_file()
chosen_words = []
for _ in range(nb_words):
dice = "".join(str(secrets.randbelow(6) + 1) for _ in range(DICE_NB))
next_word = words[dice]
if capitalize:
next_word = next_word.capitalize()
if add_digit:
next_word+=secrets.choice(CHIFFRES+SPECIAL)
chosen_words.append(next_word)
return sep.join(chosen_words)
def generer_passphrase_personnalise():
nb_words = int(input("Nombre de mots : "))
add_digit = input("Ajouter des chiffres ou caractères spéciaux ? (y/n) : ") in ["y","Y"]
capitalize = input("Ajouter une majuscule au début des mots ? (y/n) : ") in ["y","Y"]
sep = input("Séparateur de mots : ")
return generer_passphrase_v2(
nb_words=nb_words,
add_digit=add_digit,
capitalize=capitalize,
sep=sep
)