diff options
author | Donald Stufft <donald@stufft.io> | 2013-05-11 00:20:52 -0400 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2013-05-11 00:20:52 -0400 |
commit | 6c74d9236d11cc146d82ee72cbebe65fcd03ca32 (patch) | |
tree | f5b4e04c7cee47ac3cc43910c74cb7ddca41af8b | |
parent | 4ac208442047d925b8e972e22af5d68499c48cf0 (diff) | |
download | py-bcrypt-git-6c74d9236d11cc146d82ee72cbebe65fcd03ca32.tar.gz |
Give a better error message when passing a text type to hashpw
-rw-r--r-- | bcrypt/__init__.py | 13 | ||||
-rw-r--r-- | tests/test_bcrypt.py | 10 |
2 files changed, 23 insertions, 0 deletions
diff --git a/bcrypt/__init__.py b/bcrypt/__init__.py index a166d71..af87159 100644 --- a/bcrypt/__init__.py +++ b/bcrypt/__init__.py @@ -18,6 +18,7 @@ from __future__ import division from __future__ import unicode_literals import os +import sys from cffi import FFI @@ -28,6 +29,15 @@ from .__about__ import * __all__ = ["gensalt", "hashpw"] + __about__.__all__ +# True if we are running on Python 3. +PY3 = sys.version_info[0] == 3 + +if PY3: + text_type = str +else: + text_type = unicode + + _bundled_dir = os.path.join(os.path.dirname(__file__), "crypt_blowfish-1.2") _ffi = FFI() @@ -64,6 +74,9 @@ def gensalt(rounds=12): def hashpw(password, salt): + if isinstance(password, text_type) or isinstance(salt, text_type): + raise TypeError("Unicode-objects must be encoded before hashing") + hashed = _ffi.new("unsigned char[]", 128) retval = _bcrypt_lib.crypt_rn(password, salt, hashed, len(hashed)) diff --git a/tests/test_bcrypt.py b/tests/test_bcrypt.py index 476f919..41f5884 100644 --- a/tests/test_bcrypt.py +++ b/tests/test_bcrypt.py @@ -105,3 +105,13 @@ def test_hashpw_existing(password, hashed): def test_hashpw_invalid(): with pytest.raises(ValueError): bcrypt.hashpw(b"password", b"$2z$04$cVWp4XaNU8a4v1uMRum2SO") + + +def test_hashpw_str_password(): + with pytest.raises(TypeError): + bcrypt.hashpw(u"password", b"$2a$04$cVWp4XaNU8a4v1uMRum2SO") + + +def test_hashpw_str_salt(): + with pytest.raises(TypeError): + bcrypt.hashpw(b"password", u"$2a$04$cVWp4XaNU8a4v1uMRum2SO") |