forked from wowpin/dumserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
password.py
50 lines (35 loc) · 1.12 KB
/
password.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
__filename__ = "password.py"
__author__ = "Chris Byrd"
__credits__ = ["Chris Byrd"]
__license__ = "MIT"
__version__ = "0.7.1"
__maintainer__ = "Bartek Radwanski"
__email__ = "[email protected]"
__status__ = "Stable"
"""Password hashing and authentication logic for DUM server."""
import binascii
import hashlib
import os
def hash_password(pwd):
"""Hash and return salted password.
Args:
pwd {string}
Returns:
string password hash
"""
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
_hash = hashlib.pbkdf2_hmac('sha512', pwd.encode('utf-8'), salt, 100000)
_hash = binascii.hexlify(_hash)
return (salt + _hash).decode('ascii')
def check_password(stored, provided):
"""Check that the provided password matches what we have for the user.
Args:
pwd {string}
Returns:
True if user authenticate, else False
"""
salt = stored[:64]
_pwd = stored[64:]
_hash = hashlib.pbkdf2_hmac('sha512', provided.encode('utf-8'), salt.encode('ascii'), 100000)
_hash = binascii.hexlify(_hash).decode('ascii')
return _hash == _pwd