-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser.py
38 lines (30 loc) · 903 Bytes
/
user.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
import re
import hmac
import random
import hashlib
secret = "whatthehell"
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
PASS_RE = re.compile(r"^.{3,20}$")
EMAIL_RE = re.compile(r"^[\S]+@[\S]+.[\S]+$")
def valid_username(username):
return USER_RE.match(username)
def valid_password(password):
return PASS_RE.match(password)
def hash_str(s):
return hmac.new(secret,s).hexdigest()
def make_secure_val(s):
return '%s|%s' %(s,hash_str(s))
def check_secure_val(h):
val = h.split('|')[0]
if h == make_secure_val(val):
return val
def make_salt():
return ''.join(random.choice(string.letters) for x in range(5))
def make_pw_hash(name, pw, salt = None):
if not salt:
salt = make_salt()
h = hashlib.sha256(name + pw + salt).hexdigest()
return '%s,%s' % (h,salt)
def valid_pw(name, pw, h):
salt = h.split(',')[1]
return h == make_pw_hash(name, pw, salt)