Skip to content
Snippets Groups Projects
Commit 8d55e676 authored by Brian Warner's avatar Brian Warner
Browse files

fix py3 bytes-vs-str problems

parent ee4e204e
Branches
Tags
No related merge requests found
...@@ -17,34 +17,38 @@ from nacl import c ...@@ -17,34 +17,38 @@ from nacl import c
import hashlib import hashlib
def tohex(b):
return hexlify(b).decode("ascii")
def test_hash(): def test_hash():
msg = "message" msg = b"message"
h1 = c.crypto_hash(msg) h1 = c.crypto_hash(msg)
assert len(h1) == c.crypto_hash_BYTES assert len(h1) == c.crypto_hash_BYTES
assert hexlify(h1) == ("f8daf57a3347cc4d6b9d575b31fe6077" assert tohex(h1) == ("f8daf57a3347cc4d6b9d575b31fe6077"
"e2cb487f60a96233c08cb479dbf31538" "e2cb487f60a96233c08cb479dbf31538"
"cc915ec6d48bdbaa96ddc1a16db4f4f9" "cc915ec6d48bdbaa96ddc1a16db4f4f9"
"6f37276cfcb3510b8246241770d5952c") "6f37276cfcb3510b8246241770d5952c")
assert hexlify(h1) == hashlib.sha512(msg).hexdigest() assert tohex(h1) == hashlib.sha512(msg).hexdigest()
h2 = c.crypto_hash_sha512(msg) h2 = c.crypto_hash_sha512(msg)
assert len(h2) == c.crypto_hash_sha512_BYTES assert len(h2) == c.crypto_hash_sha512_BYTES
assert hexlify(h2) == hexlify(h1) assert tohex(h2) == tohex(h1)
h3 = c.crypto_hash_sha256(msg) h3 = c.crypto_hash_sha256(msg)
assert len(h3) == c.crypto_hash_sha256_BYTES assert len(h3) == c.crypto_hash_sha256_BYTES
assert hexlify(h3) == ("ab530a13e45914982b79f9b7e3fba994" assert tohex(h3) == ("ab530a13e45914982b79f9b7e3fba994"
"cfd1f3fb22f71cea1afbf02b460c6d1d") "cfd1f3fb22f71cea1afbf02b460c6d1d")
assert hexlify(h3) == hashlib.sha256(msg).hexdigest() assert tohex(h3) == hashlib.sha256(msg).hexdigest()
def test_secretbox(): def test_secretbox():
key = "\x00" * c.crypto_secretbox_KEYBYTES key = b"\x00" * c.crypto_secretbox_KEYBYTES
msg = "message" msg = b"message"
nonce = "\x01" * c.crypto_secretbox_NONCEBYTES nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES
ct = c.crypto_secretbox(msg, nonce, key) ct = c.crypto_secretbox(msg, nonce, key)
assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES 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) msg2 = c.crypto_secretbox_open(ct, nonce, key)
assert msg2 == msg assert msg2 == msg
...@@ -58,15 +62,15 @@ def test_box(): ...@@ -58,15 +62,15 @@ def test_box():
k1 = c.crypto_box_beforenm(B_pubkey, A_secretkey) k1 = c.crypto_box_beforenm(B_pubkey, A_secretkey)
assert len(k1) == c.crypto_box_BEFORENMBYTES assert len(k1) == c.crypto_box_BEFORENMBYTES
k2 = c.crypto_box_beforenm(A_pubkey, B_secretkey) k2 = c.crypto_box_beforenm(A_pubkey, B_secretkey)
assert hexlify(k1) == hexlify(k2) assert tohex(k1) == tohex(k2)
message = "message" message = b"message"
nonce = "\x01" * c.crypto_box_NONCEBYTES nonce = b"\x01" * c.crypto_box_NONCEBYTES
ct1 = c.crypto_box_afternm(message, nonce, k1) ct1 = c.crypto_box_afternm(message, nonce, k1)
assert len(ct1) == len(message) + c.crypto_box_BOXZEROBYTES assert len(ct1) == len(message) + c.crypto_box_BOXZEROBYTES
ct2 = c.crypto_box(message, nonce, B_pubkey, A_secretkey) 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) m1 = c.crypto_box_open(ct1, nonce, A_pubkey, B_secretkey)
assert m1 == message assert m1 == message
...@@ -76,7 +80,7 @@ def test_box(): ...@@ -76,7 +80,7 @@ def test_box():
def test_sign(): def test_sign():
seed = "\x00" * c.crypto_sign_SEEDBYTES seed = b"\x00" * c.crypto_sign_SEEDBYTES
pubkey, secretkey = c.crypto_sign_seed_keypair(seed) pubkey, secretkey = c.crypto_sign_seed_keypair(seed)
assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES
assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES
...@@ -85,7 +89,7 @@ def test_sign(): ...@@ -85,7 +89,7 @@ def test_sign():
assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES
assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES
msg = "message" msg = b"message"
sigmsg = c.crypto_sign(msg, secretkey) sigmsg = c.crypto_sign(msg, secretkey)
assert len(sigmsg) == len(msg) + c.crypto_sign_BYTES assert len(sigmsg) == len(msg) + c.crypto_sign_BYTES
...@@ -106,4 +110,4 @@ def test_scalarmult(): ...@@ -106,4 +110,4 @@ def test_scalarmult():
y, ypub = secret_scalar() y, ypub = secret_scalar()
bx = c.crypto_scalarmult_base(x) bx = c.crypto_scalarmult_base(x)
assert hexlify(bx) == hexlify(xpub) assert tohex(bx) == tohex(xpub)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment