Skip to content

Commit

Permalink
Inputing a 2y salt should output a 2y hash (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstufft authored and reaperhulk committed Jun 30, 2016
1 parent c9c7621 commit c959669
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Changelog
3.1.0
-----
* Added support for ``checkpw`` as another method of verifying a password.
* Ensure that you get a ``$2y$`` hash when you input a ``$2y$`` salt.

3.0.0
-----
Expand Down
23 changes: 14 additions & 9 deletions src/bcrypt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
_normalize_re = re.compile(b"^\$2y\$")


def _normalize_prefix(salt):
return _normalize_re.sub(b"$2b$", salt)


def gensalt(rounds=12, prefix=b"2b"):
if prefix not in (b"2a", b"2b"):
raise ValueError("Supported prefixes are b'2a' or b'2b'")
Expand Down Expand Up @@ -75,15 +71,27 @@ def hashpw(password, salt):
# on $2a$, so we do it here to preserve compatibility with 2.0.0
password = password[:72]

salt = _normalize_prefix(salt)
# When the original 8bit bug was found the original library we supported
# added a new prefix, $2y$, that fixes it. This prefix is exactly the same
# as the $2b$ prefix added by OpenBSD other than the name. Since the
# OpenBSD library does not support the $2y$ prefix, if the salt given to us
# is for the $2y$ prefix, we'll just mugne it so that it's a $2b$ prior to
# passing it into the C library.
original_salt, salt = salt, _normalize_re.sub(b"$2b$", salt)

hashed = _bcrypt.ffi.new("unsigned char[]", 128)
retval = _bcrypt.lib.bcrypt_hashpass(password, salt, hashed, len(hashed))

if retval != 0:
raise ValueError("Invalid salt")

return _bcrypt.ffi.string(hashed)
# Now that we've gotten our hashed password, we want to ensure that the
# prefix we return is the one that was passed in, so we'll use the prefix
# from the original salt and concatenate that with the return value (minus
# the return value's prefix). This will ensure that if someone passed in a
# salt with a $2y$ prefix, that they get back a hash with a $2y$ prefix
# even though we munged it to $2b$.
return original_salt[:4] + _bcrypt.ffi.string(hashed)[4:]


def checkpw(password, hashed_password):
Expand All @@ -96,9 +104,6 @@ def checkpw(password, hashed_password):
"password and hashed_password may not contain NUL bytes"
)

# If the user supplies a $2y$ prefix we normalize to $2b$
hashed_password = _normalize_prefix(hashed_password)

ret = hashpw(password, hashed_password)

return _bcrypt.lib.timingsafe_bcmp(ret, hashed_password, len(ret)) == 0
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bcrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@
(
b"\xa3",
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
b"$2b$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
),
(
b"\xff\xff\xa3",
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
b"$2b$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
),
]

Expand Down

0 comments on commit c959669

Please sign in to comment.