-
Notifications
You must be signed in to change notification settings - Fork 2
/
password.py
56 lines (37 loc) · 1.13 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
50
51
'''
Module for password encryption using the bcrypt library
See: https://pypi.python.org/pypi/bcrypt/3.1.0
'''
import bcrypt
class Password:
'''
Purpose:
This class handles encryption process of the password
Methods:
__init__(self, password)
get_hashed_password(self)
Author:
@asimonia
'''
def __init__(self, password):
"""Takes a password as string, specified by user and initializes a hashed
password as bytes for database storage.
"""
# convert the string into bytes
self.password = str.encode(password)
# Hash a password for the first time, with a randomly-generated salt
self.hashed = bcrypt.hashpw(self.password, bcrypt.gensalt())
def get_hashed_password(self):
"""Returns the hashed password generated by the Bcrypt library"""
return self.hashed.decode('utf-8')
@staticmethod
def check_hashed_password(password, hashed):
"""Compares the given password to the hashed password
Return True if matches, else False"""
# Convert the string -> bytes
password = str.encode(password)
hashed = str.encode(hashed)
if bcrypt.checkpw(password, hashed):
return True
else:
return False