Skip to content

Commit

Permalink
Always define the BCRYPTHashingScheme class.
Browse files Browse the repository at this point in the history
  * Conditionally register the scheme dependant on bcrypt being
    available.
  • Loading branch information
Matt Russell committed Oct 2, 2016
1 parent a5d9a1f commit ef0cb50
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/AuthEncoding/AuthEncoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,31 +170,27 @@ def validate(self, reference, attempt):
bcrypt = None


if bcrypt is not None:

# bcrypt routines require input as bytes.

def _ensure_bytes(pw, encoding='utf-8'):
"""Ensures the given password `pw` is returned as bytes.
"""
if isinstance(pw, six.text_type):
pw = pw.encode(encoding)
return pw

class BCRYPTHashingScheme:
"""A BCRYPT hashing scheme."""

def encrypt(self, pw):
return bcrypt.hashpw(_ensure_bytes(pw), bcrypt.gensalt())

def validate(self, reference, attempt):
try:
valid = bcrypt.checkpw(_ensure_bytes(attempt), reference)
except ValueError:
# Usually due to an invalid salt
valid = False
return valid
class BCRYPTHashingScheme:
"""A BCRYPT hashing scheme."""

@staticmethod
def _ensure_bytes(pw, encoding='utf-8'):
"""Ensures the given password `pw` is returned as bytes."""
if isinstance(pw, six.text_type):
pw = pw.encode(encoding)
return pw

def encrypt(self, pw):
return bcrypt.hashpw(self._ensure_bytes(pw), bcrypt.gensalt())

def validate(self, reference, attempt):
try:
return bcrypt.checkpw(self._ensure_bytes(attempt), reference)
except ValueError:
# Usually due to an invalid salt
return False

if bcrypt is not None:
registerScheme(u'BCRYPT', BCRYPTHashingScheme())


Expand Down

0 comments on commit ef0cb50

Please sign in to comment.