Skip to content

Commit f992fa6

Browse files
authored
Add files via upload
1 parent e104d13 commit f992fa6

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

passwdmanager.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import mysql.connector
2+
import sqlite3
3+
import functools
4+
import operator
5+
import json
6+
import base64
7+
import os
8+
import cryptography
9+
from cryptography.fernet import Fernet
10+
from cryptography.hazmat.backends import default_backend
11+
from cryptography.hazmat.primitives import hashes
12+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
13+
14+
#connect to database
15+
mydb = mysql.connector.connect(
16+
host="localhost",
17+
user="root",
18+
database="db_password"
19+
)
20+
21+
#set cursor
22+
mycursor = mydb.cursor(buffered=True)
23+
d = mydb.cursor(buffered=True)
24+
i = mydb.cursor(buffered=True)
25+
26+
#interfaces
27+
print("\n\nWelcome to password manager python! what you want to do?(v to view all your password,i to insert,d to delete")
28+
cmd = input(">")
29+
30+
#view query
31+
if cmd == 'v' or cmd == 'V':
32+
mycursor.execute("SELECT id, name FROM tb_nap") #select id,name from database
33+
myresult = mycursor.fetchall()
34+
35+
if len(myresult)==0: #detect blank input
36+
print("Notthing here\n")
37+
else:
38+
print("What you wanna see?")
39+
for x in myresult :
40+
print(x)
41+
42+
icmd = input("Enter ID:")
43+
if icmd=='':
44+
print("Error id.")
45+
else:
46+
d.execute("SELECT id,name FROM tb_nap WHERE id= %s",(icmd,)) #select id,name from id input
47+
i.execute("SELECT password FROM tb_nap WHERE id= %s",(icmd,)) #select password from id input
48+
p = d.fetchall()
49+
i = i.fetchall()
50+
password = " , ".join( map(str, i) ) #transition list to string
51+
52+
k = ("key") #set key
53+
k_encode = k.encode() #encode key to byte
54+
p_encode = password.encode() #encode password to byte
55+
salt = b'`R\xf7\xc0\xf3+@\xdd~\xa4K1Ty\x83\x9a'
56+
kdf = PBKDF2HMAC(
57+
algorithm=hashes.SHA256(),
58+
length=32,
59+
salt=salt,
60+
iterations=100000,
61+
backend=default_backend()
62+
)
63+
key = base64.urlsafe_b64encode(kdf.derive(k_encode))
64+
f = Fernet(key) #ready to decrypt
65+
decrypted = decrypted = f.decrypt(p_encode) #decrypted
66+
ogpasswd = decrypted.decode() #decode from byte to string
67+
68+
print("Password for",p,"is",ogpasswd) #show id,name,password
69+
70+
71+
#insert
72+
elif cmd == 'i' or cmd == 'I':
73+
print("Insert name and password")
74+
n = input("name>")
75+
p = input("password>")
76+
77+
if n=='' or p=='': #detect blank input
78+
print("Can't insert into database.")
79+
else:
80+
k = ("key") #set key
81+
k_encode = k.encode() #encode key to byte
82+
p_encode = p.encode() #encode password input to byte
83+
salt = b'`R\xf7\xc0\xf3+@\xdd~\xa4K1Ty\x83\x9a'
84+
kdf = PBKDF2HMAC(
85+
algorithm=hashes.SHA256(),
86+
length=32,
87+
salt=salt,
88+
iterations=100000,
89+
backend=default_backend()
90+
)
91+
key = base64.urlsafe_b64encode(kdf.derive(k_encode))
92+
93+
f = Fernet(key) #ready to encrypt
94+
encrypted = f.encrypt(p_encode) #encrpyted
95+
96+
sql = "INSERT INTO tb_nap (name, password) VALUES (%s, %s)" #insert to table query
97+
val = (n, encrypted)
98+
mycursor.execute(sql, val)
99+
mydb.commit() #confirm operation to database
100+
101+
print(mycursor.rowcount, "password inserted") #show number(s) of query that have inserted
102+
103+
#delete
104+
elif cmd == 'd' or cmd == 'D':
105+
mycursor.execute("SELECT id,name FROM tb_nap") #select id,name from db
106+
myresult = mycursor.fetchall()
107+
for x in myresult: #show id,name query in database
108+
print("What you want to delete")
109+
print(x)
110+
111+
i = input("Enter id:") #enter query id
112+
if i=='': #detect blank input
113+
print("Error receiving command.")
114+
else:
115+
sql = "DELETE FROM tb_nap WHERE id = %s" #delete from query id
116+
mycursor.execute(sql, (i,))
117+
mydb.commit() #confirm operation to database
118+
119+
print(mycursor.rowcount, "name and password deleted") #show number(s) of query that have deleted
120+
121+
#error
122+
else :
123+
print("\033[1;31;40m Error,Can't define command...plese try again \033[1;37;40m\n")

0 commit comments

Comments
 (0)