I have a file called password.txt and I want to store some passwords in it, I use it in a file of functions but I don't know how to make it usable for more than one file, every one having his specific password.
Like this:
file1.py: password1
file2.py: password2 ...
I already have some functions doing that but they can only handle one password:
import tkinter as tk
import hashlib
import sys
import os
from tkinter import simpledialog, messagebox
FILE_PASSWORD = "password.txt"
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
def save_password(password):
with open(FILE_PASSWORD, "w") as file:
file.write(hash_password(password))
def check_password():
global pas
if not os.path.exists(FILE_PASSWORD):
return False
with open(FILE_PASSWORD, "r") as file:
pas = file.read().strip()
def ask_password():
if not os.path.exists(FILE_PASSWORD):
new_password = simpledialog.askstring("Imposta Password", "Crea una nuova password:", show='*')
if new_password:
save_password(new_password)
messagebox.showinfo("Successo", "Password salvata!")
else:
messagebox.showerror("Errore", "Password non valida!")
sys.exit()
else:
check_password()
def change_password():
new = simpledialog.askstring("Cambia password", "Inserisci la nuova password:", show='*')
if new:
save_password(new)
messagebox.showinfo("Successo", "Password cambiata!")
else:
messagebox.showerror("Errore", "Password non valida.")
How can I make it for more files? Maybe I should use the __name__ variable?
password.txtto support multiple passwords perhaps one per python*.pyfile?for line in file: ... check if line has expected password ...instead ofpas = file.read().strip(). OR at leastlines = file.readlines()and later usefor-loop to check if one of line has your password. But usingJSONwith dictionary{"login1":"password1", "login2":"password2", ...}could be simpler