Génération de mots de passe

Le script suivant, écrit en python, permet de générer un ensemble de mots de passe mis dans un fichier et de créer leurs hash aussi mis dans un autre fichier :

from random import *
import threading
import hashlib

element = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9']
passwd = ""

def factoriel(n):
	if n == 0 or n == 1:
		return 1
	return n*factoriel(n - 1)


def ouvrir(file):
	with open(file, 'r') as f:
		my_list = f.read().splitlines()
		return my_list

def hash_word(file, mot):
	with open('test.txt', 'w') as fle:
		fle.write(mot)
	with open('test.txt', 'rb') as fle:
		bytes = fle.read()
		readable_hash = hashlib.sha256(bytes).hexdigest()
		with open(file, 'a') as f:
			f.write(readable_hash + '\n')

def motdp(longueur):
	mdp=str()
	shuffle(element)
	for x in range(longueur):
		mdp+=element[x]
	with open('MotDePasse.txt', 'a') as f:
		hash_word('Hashes_Words.txt',mdp)
		f.write(mdp + '\n')


for i in range(6,16):
	nb_comb = factoriel(61 + i) // (factoriel(i) * factoriel(61))
	for x in range(nb_comb):
		motdp(i)

Explication du script :

En effet, l’idée est de générer un ensemble de chaines de caractères formées à partir des lettres de l’alphabet et des chiffres de 0 à 9.

Ce script va générer toutes les chaines, avec la fonction motdp(longueur), de longueur 6 jusqu’à 16 caractères :

for i in range(6,16):
	nb_comb = factoriel(61 + i) // (factoriel(i) * factoriel(61))
	for x in range(nb_comb):
		motdp(i)

Fonction motdp(longueur) :

def motdp(longueur):
	mdp=str()
	shuffle(element)
	for x in range(longueur):
		mdp+=element[x]
	with open('MotDePasse.txt', 'a') as f:
		hash_word('Hashes_Words.txt',mdp)
		f.write(mdp + '\n')

Ces chaines vont être mises dans un fichier texte nommé MotDePasse.txt :

with open('MotDePasse.txt', 'a') as f:
	f.write(mdp + '\n')

Le bout de code nb_comb = factoriel(61 + i) // (factoriel(i) * factoriel(61)), qui se trouve dans la boucle for i in range(6,16), permet de calculer le nombre de mots de i caractères.

Pour chaque chaine se trouvant dans ce fichier, son hash sera créé (en hexadécimal) et mis dans un fichier nommé Hashes_Words.txt :

with open('MotDePasse.txt', 'a') as f:
		#if mdp not in my_list:
		hash_word('Hashes_Words.txt',mdp)
		#	print(mdp)
		f.write(mdp + '\n')

Avec la fonction hash_word() :

def hash_word(file, mot):
	with open('test.txt', 'w') as fle:
		fle.write(mot)
	with open('test.txt', 'rb') as fle:
		bytes = fle.read()
		readable_hash = hashlib.sha256(bytes).hexdigest()
		#print(readable_hash)
		with open(file, 'a') as f:
			f.write(readable_hash + '\n')