Skip to content

Commit 1df877c

Browse files
authored
Added initial files
0 parents  commit 1df877c

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MASTER_PWD_HASH = "YOUR MASTER PWD HASH GOES HERE"
2+
DB_NAME = "YOUR POSTGRESQL DB NAME GOES HERE"
3+
DB_UNAME = "YOUR POSTGRESQL USERNAME GOES HERE" # "postgres" by default
4+
DB_PWD = "YOUR POSTGRESQL DB PASSWORD GOES HERE"
5+
DB_TABLE_NAME = 'YOUR DB TABLE NAME GOES HERE' # Ex: 'public."Table"'
6+
SALT = b"" # Feel free to put any binary string here Ex: b".Y\x20\xBF\x045\xAC"

encrypt_funcs.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from Crypto.Cipher import AES
2+
from pbkdf2 import PBKDF2
3+
from base64 import *
4+
from config import SALT
5+
6+
7+
def encrypt(pass_to_encrypt, master_hash):
8+
derived_key = PBKDF2(str(master_hash), SALT).read(32)
9+
10+
convert = pass_to_encrypt.encode()
11+
12+
cipher = AES.new(derived_key, AES.MODE_EAX)
13+
14+
nonce = cipher.nonce # Unique to every transaction
15+
16+
ciphertext, tag = cipher.encrypt_and_digest(convert)
17+
18+
nonce_added = ciphertext + nonce
19+
20+
encoded_ciphertext = b64encode(nonce_added).decode()
21+
22+
return encoded_ciphertext
23+
24+
25+
def decrypt(pass_to_dec, master_hash):
26+
pass_to_dec += "=" * (4 - len(pass_to_dec) % 4)
27+
28+
convert = b64decode(pass_to_dec)
29+
30+
key = PBKDF2(str(master_hash), SALT).read(32)
31+
32+
nonce = convert[-16:]
33+
34+
cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
35+
36+
plaintext = cipher.decrypt(convert[:-16]) # Excluding nonce
37+
38+
return plaintext.decode()

main.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)