summaryrefslogtreecommitdiff
path: root/tests/test_bcrypt.py
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2016-06-27 11:53:42 -0500
committerlvh <_@lvh.io>2016-06-27 11:53:42 -0500
commit394882d070390f25caeebb5fa27b73bc7666c7e4 (patch)
tree714e21bbbd9c8e26887b99dd3b0d5ca4d340838d /tests/test_bcrypt.py
parent2cc711262fc0353efcbe6f7b4d855fe3bd404c10 (diff)
downloadpy-bcrypt-git-394882d070390f25caeebb5fa27b73bc7666c7e4.tar.gz
Convert bcrypt to use OpenBSD code (#68)
* swap to using openbsd bcrypt * we should probably call this 3.0 * update tests to handle slight change in behavior, test better * strip out code we're not using * define this for linux * py3 fix * add a changelog to the readme * maybe work with windows * portable endian header, replace swaps, other windows fixes * handle older windows compilers properly, handle glibc < 2.9, retab * remove a todo, that's definitely the limit * make these definitions conditional since some BSDs may already have them * add $2a$ tests from crypt_blowfish-1.3 * update readme to note supported prefixes * almost pointless commit * add support for $2y$ test vectors from openwall crypt-blowfish1.3
Diffstat (limited to 'tests/test_bcrypt.py')
-rw-r--r--tests/test_bcrypt.py55
1 files changed, 51 insertions, 4 deletions
diff --git a/tests/test_bcrypt.py b/tests/test_bcrypt.py
index 32fa15f..90f1f66 100644
--- a/tests/test_bcrypt.py
+++ b/tests/test_bcrypt.py
@@ -41,9 +41,7 @@ def test_gensalt_rounds_valid(rounds, expected, monkeypatch):
@pytest.mark.parametrize("rounds", list(range(1, 4)))
-def test_gensalt_rounds_invalid(rounds, monkeypatch):
- monkeypatch.setattr(os, "urandom", lambda n: b"0000000000000000")
-
+def test_gensalt_rounds_invalid(rounds):
with pytest.raises(ValueError):
bcrypt.gensalt(rounds)
@@ -245,11 +243,59 @@ def test_hashpw_new(password, salt, expected):
b"J8eHUDuxBB520",
b"$2b$04$VvlCUKbTMjaxaYJ.k5juoecpG/7IzcH1AkmqKi.lIZMVIOLClWAk.",
),
+ (
+ b"U*U",
+ b"$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW",
+ ),
+ (
+ b"U*U*",
+ b"$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK",
+ ),
+ (
+ b"U*U*U",
+ b"$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a",
+ ),
+ (
+ b"0123456789abcdefghijklmnopqrstuvwxyz"
+ b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+ b"chars after 72 are ignored",
+ b"$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui",
+ ),
+ (
+ b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
+ b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
+ b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
+ b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
+ b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
+ b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
+ b"chars after 72 are ignored as usual",
+ b"$2a$05$/OK.fbVrR/bpIqNJ5ianF.swQOIzjOiJ9GHEPuhEkvqrUyvWhEMx6"
+ ),
+ (
+ b"\xa3",
+ b"$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq"
+ ),
])
def test_hashpw_existing(password, hashed):
assert bcrypt.hashpw(password, hashed) == hashed
+@pytest.mark.parametrize(("password", "hashed", "expected"), [
+ (
+ b"\xa3",
+ b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
+ b"$2b$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
+ ),
+ (
+ b"\xff\xff\xa3",
+ b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
+ b"$2b$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
+ ),
+])
+def test_hashpw_2y_prefix(password, hashed, expected):
+ assert bcrypt.hashpw(password, hashed) == expected
+
+
def test_hashpw_invalid():
with pytest.raises(ValueError):
bcrypt.hashpw(b"password", b"$2z$04$cVWp4XaNU8a4v1uMRum2SO")
@@ -272,5 +318,6 @@ def test_hashpw_str_salt():
def test_nul_byte():
+ salt = bcrypt.gensalt(4)
with pytest.raises(ValueError):
- bcrypt.hashpw(b"abc\0def", bcrypt.gensalt(0))
+ bcrypt.hashpw(b"abc\0def", salt)