-
Notifications
You must be signed in to change notification settings - Fork 17
/
adduser.py
39 lines (30 loc) · 912 Bytes
/
adduser.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
import psycopg2
import sys
import json
import hashlib
import base64
if __name__ == "__main__":
with open(sys.argv[1],'r') as fin:
conf = json.load(fin)
conn = psycopg2.connect(**conf['webapi']['postgresql'])
cur = conn.cursor()
sys.stdout.write('Username:')
username = raw_input()
sys.stdout.write('Password:')
password = raw_input()
pwHash = hashlib.sha512()
pwHash.update(password)
storedHash = hashlib.sha512()
storedHash.update(conf['salt'])
storedHash.update(pwHash.digest())
secretKey = hashlib.sha512()
with open('/dev/urandom') as randomIn:
randomValue = randomIn.read(1024)
secretKey.update(randomValue)
cur.execute("INSERT into users (name,password,secretKey) VALUES(%s,%s,%s);",
(username,
base64.urlsafe_b64encode(storedHash.digest()).replace('=',''),
base64.urlsafe_b64encode(secretKey.digest()).replace('=',''),) )
conn.commit()
cur.close()
conn.close()