0

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?

10
  • 3
    besides from every inch in body screaming don't store passwords in a .txt file and i am hoping you have some sort of hash. You could have a loop that searches for every filename like "password" and inspect the last digit this being count up and saved.. considering you have them all in the same folder Commented Apr 23 at 13:19
  • 1
    Do you want password.txt to support multiple passwords perhaps one per python *.py file? Commented Apr 23 at 13:48
  • 2
    It's hashed. So the question is basically how to do you save multiple strings in a text file? I'd probably go with a json of dict[username, hashed_password] but there are so many ways. Commented Apr 23 at 13:51
  • create an entry for each hashed user , scan the file to match the user entry .... using structured json as per @KennyOstrom advice Commented Apr 23 at 13:53
  • 1
    you should rather read it as for line in file: ... check if line has expected password ... instead of pas = file.read().strip(). OR at least lines = file.readlines() and later use for-loop to check if one of line has your password. But using JSON with dictionary {"login1":"password1", "login2":"password2", ...} could be simpler Commented Apr 23 at 14:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.