From e6133bdef2f13f5f29c61582c754985b79b8bce0 Mon Sep 17 00:00:00 2001 From: Vijayvarma115 <145138789+Vijayvarma115@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:36:46 +0530 Subject: [PATCH] Update password_manager.py Handle cases where the user inputs an invalid command or service name.Can implement try-except blocks to catch and handle potential exceptions --- PasswordManager/password_manager.py | 106 ++++++++++++++-------------- 1 file changed, 52 insertions(+), 54 deletions(-) diff --git a/PasswordManager/password_manager.py b/PasswordManager/password_manager.py index f8ec461..faf90a8 100644 --- a/PasswordManager/password_manager.py +++ b/PasswordManager/password_manager.py @@ -1,8 +1,6 @@ - import sqlite3 from hashlib import sha256 - ADMIN_PASSWORD = "123456" connect = input("What is your password?\n") @@ -12,57 +10,57 @@ if connect == "q": break -conn = sqlite3.connect('pass_manager.db') - -def create_password(pass_key, service, admin_pass): - return sha256(admin_pass.encode('utf-8') + service.lower().encode('utf-8') + pass_key.encode('utf-8')).hexdigest()[:15] - -def get_hex_key(admin_pass, service): - return sha256(admin_pass.encode('utf-8') + service.lower().encode('utf-8')).hexdigest() - -def get_password(admin_pass, service): - secret_key = get_hex_key(admin_pass, service) - cursor = conn.execute("SELECT * from KEYS WHERE PASS_KEY=" + '"' + secret_key + '"') - - file_string = "" - for row in cursor: - file_string = row[0] - return create_password(file_string, service, admin_pass) - -def add_password(service, admin_pass): - secret_key = get_hex_key(admin_pass, service) - - command = 'INSERT INTO KEYS (PASS_KEY) VALUES (%s);' %('"' + secret_key +'"') - conn.execute(command) - conn.commit() - return create_password(secret_key, service, admin_pass) - -if connect == ADMIN_PASSWORD: - try: - conn.execute('''CREATE TABLE KEYS - (PASS_KEY TEXT PRIMARY KEY NOT NULL);''') - print("Your safe has been created!\nWhat would you like to store in it today?") - except: - print("You have a safe, what would you like to do today?") +try: + conn = sqlite3.connect('pass_manager.db') + def create_password(pass_key, service, admin_pass): + return sha256(admin_pass.encode('utf-8') + service.lower().encode('utf-8') + pass_key.encode('utf-8')).hexdigest()[:15] + + def get_hex_key(admin_pass, service): + return sha256(admin_pass.encode('utf-8') + service.lower().encode('utf-8')).hexdigest() + + def get_password(admin_pass, service): + secret_key = get_hex_key(admin_pass, service) + cursor = conn.execute("SELECT * from KEYS WHERE PASS_KEY=" + '"' + secret_key + '"') + + file_string = "" + for row in cursor: + file_string = row[0] + return create_password(file_string, service, admin_pass) + + def add_password(service, admin_pass): + secret_key = get_hex_key(admin_pass, service) + + command = 'INSERT INTO KEYS (PASS_KEY) VALUES (%s);' %('"' + secret_key +'"') + conn.execute(command) + conn.commit() + return create_password(secret_key, service, admin_pass) + + if connect == ADMIN_PASSWORD: + try: + conn.execute('''CREATE TABLE KEYS + (PASS_KEY TEXT PRIMARY KEY NOT NULL);''') + print("Your safe has been created!\nWhat would you like to store in it today?") + except sqlite3.Error as e: + print("Error creating table:", e) + print("You have a safe, what would you like to do today?") - while True: - print("\n"+ "*"*15) - print("Commands:") - print("q = quit program") - print("gp = get password") - print("sp = store password") - print("*"*15) - input_ = input(":") - - if input_ == "q": - break - if input_ == "sp": - service = input("What is the name of the service?\n") - print("\n" + service.capitalize() + " password created:\n" + add_password(service, ADMIN_PASSWORD)) - if input_ == "gp": - service = input("What is the name of the service?\n") - print("\n" + service.capitalize() + " password:\n"+get_password(ADMIN_PASSWORD, service)) - - - + while True: + print("\n"+ "*"*15) + print("Commands:") + print("q = quit program") + print("gp = get password") + print("sp = store password") + print("*"*15) + input_ = input(":") + + if input_ == "q": + break + if input_ == "sp": + service = input("What is the name of the service?\n") + print("\n" + service.capitalize() + " password created:\n" + add_password(service, ADMIN_PASSWORD)) + if input_ == "gp": + service = input("What is the name of the service?\n") + print("\n" + service.capitalize() + " password:\n"+get_password(ADMIN_PASSWORD, service)) +except sqlite3.Error as e: + print("Database error:", e)