Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions owasp-top10-2021-apps/a3/gossip-world/app/model/password.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import hashlib
import os


class Password:
Expand All @@ -10,10 +11,22 @@ def get_hashed_password(self):
return self._make_hash(self.password)

def validate_password(self, hashed_password):
return self._compare_password(hashed_password, self._make_hash(self.password))
try:
salt, _ = hashed_password.split('$', 1)
except ValueError:
return False
# Recreate hash with extracted salt and compare full stored string
return hashed_password == self._make_hash(self.password, salt)

def _make_hash(self, string):
return hashlib.sha256(string).hexdigest()
def _make_hash(self, string, salt=None):
# Use PBKDF2-HMAC with a random salt and key stretching
if salt is None:
salt = os.urandom(16)
else:
salt = bytes.fromhex(salt)
dk = hashlib.pbkdf2_hmac('sha256', string.encode('utf-8'), salt, 100000)
# Store salt and derived key in hex separated by $
return salt.hex() + '$' + dk.hex()

def _compare_password(self, password_1, password_2):
return password_1 == password_2