summaryrefslogtreecommitdiff
path: root/passlib/handlers/bcrypt.py
diff options
context:
space:
mode:
Diffstat (limited to 'passlib/handlers/bcrypt.py')
-rw-r--r--passlib/handlers/bcrypt.py38
1 files changed, 17 insertions, 21 deletions
diff --git a/passlib/handlers/bcrypt.py b/passlib/handlers/bcrypt.py
index 9a1f595..7a698d4 100644
--- a/passlib/handlers/bcrypt.py
+++ b/passlib/handlers/bcrypt.py
@@ -27,8 +27,8 @@ except ImportError: #pragma: no cover - though should run whole suite w/o bcrypt
bcryptor_engine = None
#libs
from passlib.exc import PasslibHandlerWarning
-from passlib.utils import BCRYPT_CHARS as BCHARS, safe_os_crypt, \
- classproperty, rng, getrandstr
+from passlib.utils import BCRYPT_CHARS as BCHARS, safe_crypt, \
+ classproperty, rng, getrandstr, test_crypt
from passlib.utils.compat import bytes, u, uascii_to_str, unicode
import passlib.utils.handlers as uh
@@ -144,9 +144,10 @@ class bcrypt(uh.HasManyIdents, uh.HasRounds, uh.HasSalt, uh.HasManyBackends, uh.
strict=strict and bool(chk),
)
- def to_string(self, native=True):
- hash = u("%s%02d$%s%s") % (self.ident, self.rounds, self.salt, self.checksum or u(''))
- return uascii_to_str(hash) if native else hash
+ def to_string(self):
+ hash = u("%s%02d$%s%s") % (self.ident, self.rounds, self.salt,
+ self.checksum or u(''))
+ return uascii_to_str(hash)
#=========================================================
# specialized salt generation - fixes passlib issue 25
@@ -228,18 +229,17 @@ class bcrypt(uh.HasManyIdents, uh.HasRounds, uh.HasSalt, uh.HasManyBackends, uh.
@classproperty
def _has_backend_os_crypt(cls):
- h1 = u('$2$04$......................1O4gOrCYaqBG3o/4LnT2ykQUt1wbyju')
- h2 = u('$2a$04$......................qiOQjkB8hxU8OzRhS.GhRMa4VUnkPty')
- return bool(safe_os_crypt and safe_os_crypt(u("test"),h1)[1]==h1 and
- safe_os_crypt(u("test"), h2)[1]==h2)
+ h1 = '$2$04$......................1O4gOrCYaqBG3o/4LnT2ykQUt1wbyju'
+ h2 = '$2a$04$......................qiOQjkB8hxU8OzRhS.GhRMa4VUnkPty'
+ return test_crypt("test",h1) and test_crypt("test", h2)
@classmethod
def _no_backends_msg(cls):
return "no BCrypt backends available - please install pybcrypt or bcryptor for BCrypt support"
def _calc_checksum_os_crypt(self, secret):
- ok, hash = safe_os_crypt(secret, self.to_string(native=False))
- if ok:
+ hash = safe_crypt(secret, self.to_string())
+ if hash:
return hash[-31:]
else:
#NOTE: not checking backends since this is lowest priority,
@@ -249,26 +249,22 @@ class bcrypt(uh.HasManyIdents, uh.HasRounds, uh.HasSalt, uh.HasManyBackends, uh.
def _calc_checksum_pybcrypt(self, secret):
#pybcrypt behavior:
- # py2: unicode secret -> ascii bytes (we override this)
- # unicode hash -> ascii bytes (we provide ascii bytes)
- # returns ascii bytes
+ # py2: unicode secret/hash encoded as ascii bytes before use,
+ # bytes takes as-is; returns ascii bytes.
# py3: can't get to install
if isinstance(secret, unicode):
secret = secret.encode("utf-8")
- hash = pybcrypt_hashpw(secret,
- self.to_string(native=False))
+ hash = pybcrypt_hashpw(secret, self.to_string())
return hash[-31:].decode("ascii")
def _calc_checksum_bcryptor(self, secret):
#bcryptor behavior:
- # py2: unicode secret -> ascii bytes (we have to override)
- # unicode hash -> ascii bytes (we provide ascii bytes)
- # returns ascii bytes
+ # py2: unicode secret/hash encoded as ascii bytes before use,
+ # bytes takes as-is; returns ascii bytes.
# py3: can't get to install
if isinstance(secret, unicode):
secret = secret.encode("utf-8")
- hash = bcryptor_engine(False).hash_key(secret,
- self.to_string(native=False))
+ hash = bcryptor_engine(False).hash_key(secret, self.to_string())
return hash[-31:].decode("ascii")
def _calc_checksum_builtin(self, secret):