-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.py
55 lines (42 loc) · 1.48 KB
/
add.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from utils.dbconfig import dbconfig
import utils.aesutil
from getpass import getpass
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Hash import SHA512
from Crypto.Random import get_random_bytes
import base64
from rich import print as printc
from rich.console import Console
def computeMasterKey(mp,ds):
password = mp.encode()
salt = ds.encode()
key = PBKDF2(password, salt, 32, count=1000000, hmac_hash_module=SHA512)
return key
def checkEntry(sitename, siteurl, email, username):
db = dbconfig()
cursor = db.cursor()
query = f"SELECT * FROM pm.entries WHERE sitename = '{sitename}' AND siteurl = '{siteurl}' AND email = '{email}' AND username = '{username}'"
cursor.execute(query)
results = cursor.fetchall()
if len(results)!=0:
return True
return False
def addEntry(mp, ds, sitename, siteurl, email, username):
# Check if the entry already exists
if checkEntry(sitename, siteurl, email, username):
printc("[yellow][-][/yellow] Entry with these details already exists")
return
# Input Password
password = getpass("Password: ")
# compute master key
mk = computeMasterKey(mp,ds)
# encrypt password with mk
encrypted = utils.aesutil.encrypt(key=mk, source=password, keyType="bytes")
# Add to db
db = dbconfig()
cursor = db.cursor()
query = "INSERT INTO pm.entries (sitename, siteurl, email, username, password) values (%s, %s, %s, %s, %s)"
val = (sitename,siteurl,email,username,encrypted)
cursor.execute(query, val)
db.commit()
printc("[green][+][/green] Added entry ")