From 8d55e676bbd56cd578bdd8af0125bacf8578245a Mon Sep 17 00:00:00 2001 From: Brian Warner <warner@lothar.com> Date: Sat, 21 Jun 2014 12:13:53 -0700 Subject: [PATCH] fix py3 bytes-vs-str problems --- tests/test_raw.py | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/tests/test_raw.py b/tests/test_raw.py index 2dd4b417..a3a0ed4f 100644 --- a/tests/test_raw.py +++ b/tests/test_raw.py @@ -17,34 +17,38 @@ from nacl import c import hashlib +def tohex(b): + return hexlify(b).decode("ascii") + + def test_hash(): - msg = "message" + msg = b"message" h1 = c.crypto_hash(msg) assert len(h1) == c.crypto_hash_BYTES - assert hexlify(h1) == ("f8daf57a3347cc4d6b9d575b31fe6077" - "e2cb487f60a96233c08cb479dbf31538" - "cc915ec6d48bdbaa96ddc1a16db4f4f9" - "6f37276cfcb3510b8246241770d5952c") - assert hexlify(h1) == hashlib.sha512(msg).hexdigest() + assert tohex(h1) == ("f8daf57a3347cc4d6b9d575b31fe6077" + "e2cb487f60a96233c08cb479dbf31538" + "cc915ec6d48bdbaa96ddc1a16db4f4f9" + "6f37276cfcb3510b8246241770d5952c") + assert tohex(h1) == hashlib.sha512(msg).hexdigest() h2 = c.crypto_hash_sha512(msg) assert len(h2) == c.crypto_hash_sha512_BYTES - assert hexlify(h2) == hexlify(h1) + assert tohex(h2) == tohex(h1) h3 = c.crypto_hash_sha256(msg) assert len(h3) == c.crypto_hash_sha256_BYTES - assert hexlify(h3) == ("ab530a13e45914982b79f9b7e3fba994" - "cfd1f3fb22f71cea1afbf02b460c6d1d") - assert hexlify(h3) == hashlib.sha256(msg).hexdigest() + assert tohex(h3) == ("ab530a13e45914982b79f9b7e3fba994" + "cfd1f3fb22f71cea1afbf02b460c6d1d") + assert tohex(h3) == hashlib.sha256(msg).hexdigest() def test_secretbox(): - key = "\x00" * c.crypto_secretbox_KEYBYTES - msg = "message" - nonce = "\x01" * c.crypto_secretbox_NONCEBYTES + key = b"\x00" * c.crypto_secretbox_KEYBYTES + msg = b"message" + nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES ct = c.crypto_secretbox(msg, nonce, key) assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES - assert hexlify(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a" + assert tohex(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a" msg2 = c.crypto_secretbox_open(ct, nonce, key) assert msg2 == msg @@ -58,15 +62,15 @@ def test_box(): k1 = c.crypto_box_beforenm(B_pubkey, A_secretkey) assert len(k1) == c.crypto_box_BEFORENMBYTES k2 = c.crypto_box_beforenm(A_pubkey, B_secretkey) - assert hexlify(k1) == hexlify(k2) + assert tohex(k1) == tohex(k2) - message = "message" - nonce = "\x01" * c.crypto_box_NONCEBYTES + message = b"message" + nonce = b"\x01" * c.crypto_box_NONCEBYTES ct1 = c.crypto_box_afternm(message, nonce, k1) assert len(ct1) == len(message) + c.crypto_box_BOXZEROBYTES ct2 = c.crypto_box(message, nonce, B_pubkey, A_secretkey) - assert hexlify(ct2) == hexlify(ct1) + assert tohex(ct2) == tohex(ct1) m1 = c.crypto_box_open(ct1, nonce, A_pubkey, B_secretkey) assert m1 == message @@ -76,7 +80,7 @@ def test_box(): def test_sign(): - seed = "\x00" * c.crypto_sign_SEEDBYTES + seed = b"\x00" * c.crypto_sign_SEEDBYTES pubkey, secretkey = c.crypto_sign_seed_keypair(seed) assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES @@ -85,7 +89,7 @@ def test_sign(): assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES - msg = "message" + msg = b"message" sigmsg = c.crypto_sign(msg, secretkey) assert len(sigmsg) == len(msg) + c.crypto_sign_BYTES @@ -106,4 +110,4 @@ def test_scalarmult(): y, ypub = secret_scalar() bx = c.crypto_scalarmult_base(x) - assert hexlify(bx) == hexlify(xpub) + assert tohex(bx) == tohex(xpub) -- GitLab