|
| 1 | +import getpass |
| 2 | +import hashlib |
| 3 | +from config import * |
| 4 | +import psycopg2 |
| 5 | +import encrypt_funcs |
| 6 | + |
| 7 | + |
| 8 | +def listfunc(decrypt, dbcursor): |
| 9 | + dbcursor.execute('SELECT * FROM ' + DB_TABLE_NAME) |
| 10 | + |
| 11 | + rows = dbcursor.fetchall() |
| 12 | + |
| 13 | + if rows: |
| 14 | + for row in rows: |
| 15 | + print("-" * 20) |
| 16 | + print("Website:", row[0]) |
| 17 | + print("Username:", row[1]) |
| 18 | + |
| 19 | + if decrypt: |
| 20 | + |
| 21 | + print("Password:", |
| 22 | + encrypt_funcs.decrypt(row[2], MASTER_PWD_HASH)) |
| 23 | + |
| 24 | + else: |
| 25 | + print("Password:", row[2]) |
| 26 | + print("-" * 20) |
| 27 | + |
| 28 | + else: |
| 29 | + |
| 30 | + print("No Records Found!") |
| 31 | + |
| 32 | + |
| 33 | +def list_encrypted_records(dbcursor): |
| 34 | + listfunc(False, dbcursor) |
| 35 | + |
| 36 | + |
| 37 | +def list_decrypted_records(dbcursor): |
| 38 | + user_pwd = getpass.getpass("\nEnter Master Password:").encode() |
| 39 | + if hashlib.sha256(user_pwd).hexdigest() == MASTER_PWD_HASH: |
| 40 | + listfunc(True, dbcursor) |
| 41 | + else: |
| 42 | + print("Sorry, wrong password!") |
| 43 | + |
| 44 | + |
| 45 | +def add_record(dbcursor): |
| 46 | + website = input("\nWebsite: ") |
| 47 | + name = input("Name: ") |
| 48 | + password = input("Password: ") |
| 49 | + |
| 50 | + dbcursor.execute(f'INSERT INTO {DB_TABLE_NAME} VALUES (%s, %s, %s)', |
| 51 | + (website, name, |
| 52 | + encrypt_funcs.encrypt(password, MASTER_PWD_HASH))) |
| 53 | + |
| 54 | + print("Record Successfully Added!\n") |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + |
| 59 | + print("\nSQL Password Manager") |
| 60 | + |
| 61 | + user_master = getpass.getpass("\nEnter Master Password:").encode() |
| 62 | + |
| 63 | + if hashlib.sha256(user_master).hexdigest() == MASTER_PWD_HASH: |
| 64 | + |
| 65 | + connection = psycopg2.connect(dbname=DB_NAME, user=DB_UNAME, |
| 66 | + password=DB_PWD) |
| 67 | + |
| 68 | + cursor = connection.cursor() |
| 69 | + |
| 70 | + print("1. List Encrypted Records") |
| 71 | + print("2. List Decrypted Records") |
| 72 | + print("3. Add Record\n") |
| 73 | + |
| 74 | + userChoice = input("Enter Option: ") |
| 75 | + if userChoice == "1": |
| 76 | + list_encrypted_records(cursor) |
| 77 | + elif userChoice == "2": |
| 78 | + list_decrypted_records(cursor) |
| 79 | + elif userChoice == "3": |
| 80 | + add_record(cursor) |
| 81 | + else: |
| 82 | + print("Invalid Option!") |
| 83 | + |
| 84 | + connection.commit() |
| 85 | + connection.close() |
| 86 | + |
| 87 | + else: |
| 88 | + print("Wrong Master Password!") |
0 commit comments