24
loading...
This website collects cookies to deliver better user experience
database.py
file and add the following code:import sqlite3
def init_database():
with sqlite3.connect("password_vault.db") as db:
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS master(
id INTEGER PRIMARY KEY,
password TEXT NOT NULL);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS vault(
id INTEGER PRIMARY KEY,
platform TEXT NOT NULL,
userid TEXT NOT NULL,
password TEXT NOT NULL);
""")
return db, cursor
init_database()
function. Inside the function, we are connecting to the password_vault.db
database using sqlite3.connect()
method. Note that, if the database is not present, it is automatically created. Further, we are using the db.cursor()
method that returns an object of the Cursor class, which we'll call cursor. Next, we are executing SQL queries using the cursor.execute()
method to create two tables - master and vault , if they don't exist.CREATE TABLE IF NOT EXISTS master(
id INTEGER PRIMARY KEY,
password TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS vault(
id INTEGER PRIMARY KEY,
platform TEXT NOT NULL,
userid TEXT NOT NULL,
password TEXT NOT NULL);
db
and cursor
to be used by other classes and methods. manager.py
that will have the following components:from database import init_database
from tkinter import Tk
class PasswordManager:
def __init__ (self):
self.db, self.cursor = init_database()
self.window = Tk()
self.window.update()
self.window.title("Password Manager")
self.window.geometry("650x350")
import hashlib
def encrypt_password(self, password):
password = password.encode("utf-8")
encoded_text = hashlib.md5(password).hexdigest()
return encoded_text
hexdigest()
method returns the encoded text as a string.def copy_text(self, text):
self.window.clipboard_clear()
self.window.clipboard_append(text)
copy_text()
method. from functools import partial
from tkinter import Button, Entry, Label, Tk
from tkinter.constants import CENTER
def welcome_new_user(self):
self.window.geometry("450x200")
label1 = Label(self.window, text="Create New Master Password")
label1.config(anchor=CENTER)
label1.pack(pady=10)
mp_entry_box = Entry(self.window, width=20, show="*")
mp_entry_box.pack()
mp_entry_box.focus()
label2 = Label(self.window, text="Enter the password again")
label2.config(anchor=CENTER)
label2.pack(pady=10)
rmp_entry_box = Entry(self.window, width=20, show="*")
rmp_entry_box.pack()
self.feedback = Label(self.window)
self.feedback.pack()
save_btn = Button(self.window, text="Create Password",
command=partial(self.save_master_password, mp_entry_box, rmp_entry_box))
save_btn.pack(pady=5)
show="*"
in the entry box object. Similarly, we are creating and packing another label with the text Enter the password again and a password entry box for the user to re-enter the password. Next, we have a save button to create the user. In the command, we are passing a save_master_password
method and the two entry boxes as arguments using the partial
function. Additionally, we have a feedback label to show feedback if the two passwords are not the same.def save_master_password(self, eb1, eb2):
password1 = eb1.get()
password2 = eb2.get()
if password1 == password2:
hashed_password = self.encrypt_password(password1)
insert_command = """INSERT INTO master(password)
VALUES(?) """
self.cursor.execute(insert_command, [hashed_password])
self.db.commit()
self.login_user()
else:
self.feedback.config(text="Passwords do not match", fg="red")
def login_user(self):
for widget in self.window.winfo_children():
widget.destroy()
self.window.geometry("450x200")
label1 = Label(self.window, text="Enter your master password")
label1.config(anchor=CENTER)
label1.place(x=150, y=50)
self.password_entry_box = Entry(self.window, width=20, show="*")
self.password_entry_box.place(x=160, y=80)
self.password_entry_box.focus()
self.feedback = Label(self.window)
self.feedback.place(x=170, y=105)
login_btn = Button(self.window, text="Log In", command=partial(
self.check_master_password, self.password_entry_box))
login_btn.place(x=200, y=130)
check_master_password
method in the command with the password entry box as the argument.def check_master_password(self, eb):
hashed_password = self.encrypt_password(eb.get())
self.cursor.execute(
"SELECT * FROM master WHERE id = 1 AND password = ?", [hashed_password])
if self.cursor.fetchall():
self.password_vault_screen()
else:
self.password_entry_box.delete(0, END)
self.feedback.config(text="Incorrect password", fg="red")
encrypt_password()
method. Then we check in the database if there exists any row which has id=1 and password=hashed_password. The id=1 is there because we're assuming only one person will be using this application. So whenever the user sets his master password, id 1 is assigned to it. We use the cursor.fetchall()
method to fetch all (remaining) rows of the query result. If we get any result, it means the correct password is entered, and hence we show the password vault screen to the user(we'll create this soon). In case it returns an empty list, we clear the entry box and add the error message in the feedback label.