40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
import mysql.connector
|
|
import bcrypt
|
|
from dotenv import load_dotenv
|
|
import os
|
|
|
|
# Charger les variables d'environnement depuis .env
|
|
load_dotenv()
|
|
|
|
conn = mysql.connector.connect(
|
|
host=os.getenv("DB_HOST"),
|
|
user=os.getenv("DB_USER"),
|
|
password=os.getenv("DB_PASSWORD"),
|
|
database=os.getenv("DB_NAME")
|
|
)
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("SELECT utilisateur, mot_de_passe FROM MotsDePasse")
|
|
utilisateurs = cursor.fetchall()
|
|
|
|
def hash_password(plain_text_password):
|
|
return bcrypt.hashpw(plain_text_password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
|
|
|
for utilisateur, mot_de_passe in utilisateurs:
|
|
if mot_de_passe.startswith('$2b$') or mot_de_passe.startswith('$2a$'):
|
|
print(f"{utilisateur} : déjà haché")
|
|
continue
|
|
|
|
hashe = hash_password(mot_de_passe)
|
|
cursor.execute("UPDATE MotsDePasse SET mot_de_passe = %s WHERE utilisateur = %s", (hashe, utilisateur))
|
|
print(f"{utilisateur} : mot de passe haché")
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
print("Mise à jour terminée.")
|
|
|
|
|
|
|
|
|