-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.py
More file actions
26 lines (20 loc) · 949 Bytes
/
security.py
File metadata and controls
26 lines (20 loc) · 949 Bytes
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
# base imports
import os
import hashlib
import binascii
# This function hashes a given password using the sha512 hash function.
def hash_password(password):
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt, 100000)
pwdhash = binascii.hexlify(pwdhash)
return (salt + pwdhash).decode('ascii')
# This function uses the same 'salt' as the previous password to compare
# a provided password with the stored password and say whether they match
def verify_password(stored_password, provided_password):
if not stored_password:
return False
salt = stored_password[0][0][:64]
stored_password = stored_password[0][0][64:]
pwdhash = hashlib.pbkdf2_hmac('sha512', provided_password.encode('utf-8'), salt.encode('ascii'), 100000)
pwdhash = binascii.hexlify(pwdhash).decode('ascii')
return pwdhash == stored_password