-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManageUsers.py
50 lines (44 loc) · 1.3 KB
/
ManageUsers.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
import sqlite3
import Crypto.Random
import Crypto.Protocol.KDF
def add_user(curs, username, password):
salt = Crypto.Random.get_random_bytes(8)
hash = Crypto.Protocol.KDF.PBKDF2(password, salt)
print('\nADDING...')
print(username)
print(salt.hex())
print(hash.hex())
curs.execute('INSERT INTO users VALUES (?,?,?)',
(username, salt.hex(), hash.hex()))
def authenticate_user(curs, username, password):
results = curs.execute('SELECT salt, hash FROM users WHERE username=?',
(username,))
row = results.fetchone()
salt = bytes.fromhex(row[0])
hash = bytes.fromhex(row[1])
if hash == Crypto.Protocol.KDF.PBKDF2(password, salt):
return True
else:
return False
Crypto.Random.new()
conn = sqlite3.connect('test0.db')
curs = conn.cursor()
try:
username = input('enter new username: ')
password = input('enter new password: ')
add_user(curs, username, password)
print('SUCCESS\n')
conn.commit()
except:
print('FAILED\n')
conn.rollback()
try:
username = input('enter existing username: ')
password = input('enter existing password: ')
if authenticate_user(curs, username, password):
print('VALID\n')
else:
print('NOT VALID\n')
except:
print('FAILED\n')
conn.close()