Skip to content
Snippets Groups Projects
Commit bc6325b7 authored by Donald Stufft's avatar Donald Stufft
Browse files

Fix up the flake8 output and include it in the test matrix

parent af424262
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,7 @@ env:
- TOXENV=py33 SODIUM_INSTALL=system CC=clang
- TOXENV=pypy SODIUM_INSTALL=system CC=clang
- TOXENV=docs
- TOXENV=pep8
install:
# Add the PyPy repository
......
......@@ -20,7 +20,8 @@ __all__ = [
]
__title__ = "PyNaCl"
__summary__ = "Python binding to the Networking and Cryptography (NaCl) library"
__summary__ = ("Python binding to the Networking and Cryptography (NaCl) "
"library")
__uri__ = "https://github.com/pyca/pynacl/"
__version__ = "0.1.0"
......
......@@ -16,11 +16,9 @@ from __future__ import absolute_import, division, print_function
import glob
import os.path
import six
# We need to import this prior to importing cffi to fix prebuilding the
# extension modules
from nacl import _cffi_fix
from nacl import _cffi_fix # noqa
from cffi import FFI
from cffi.verifier import Verifier
......@@ -45,7 +43,9 @@ for header in HEADERS:
# TODO: Can we use the ABI of libsodium for this instead?
ffi.verifier = Verifier(ffi,
ffi.verifier = Verifier(
ffi,
"#include <sodium.h>",
# We need to link to the sodium library
......
......@@ -161,10 +161,10 @@ class Box(encoding.Encodable, StringFixer, object):
encoded_ciphertext = encoder.encode(ciphertext)
return EncryptedMessage._from_parts(
encoded_nonce,
encoded_ciphertext,
encoder.encode(nonce + ciphertext),
)
encoded_nonce,
encoded_ciphertext,
encoder.encode(nonce + ciphertext),
)
def decrypt(self, ciphertext, nonce=None, encoder=encoding.RawEncoder):
"""
......
......@@ -47,8 +47,10 @@ class SecretBox(encoding.Encodable, StringFixer, object):
key = encoder.decode(key)
if len(key) != self.KEY_SIZE:
raise ValueError("The key must be exactly %s bytes long" %
nacl.lib.crypto_secretbox_KEYBYTES)
raise ValueError(
"The key must be exactly %s bytes long" %
nacl.lib.crypto_secretbox_KEYBYTES,
)
self._key = key
......@@ -72,8 +74,9 @@ class SecretBox(encoding.Encodable, StringFixer, object):
:rtype: [:class:`nacl.utils.EncryptedMessage`]
"""
if len(nonce) != self.NONCE_SIZE:
raise ValueError("The nonce must be exactly %s bytes long" %
self.NONCE_SIZE)
raise ValueError(
"The nonce must be exactly %s bytes long" % self.NONCE_SIZE,
)
ciphertext = nacl.c.crypto_secretbox(self._key, plaintext, nonce)
......@@ -81,10 +84,10 @@ class SecretBox(encoding.Encodable, StringFixer, object):
encoded_ciphertext = encoder.encode(ciphertext)
return EncryptedMessage._from_parts(
encoded_nonce,
encoded_ciphertext,
encoder.encode(nonce + ciphertext),
)
encoded_nonce,
encoded_ciphertext,
encoder.encode(nonce + ciphertext),
)
def decrypt(self, ciphertext, nonce=None, encoder=encoding.RawEncoder):
"""
......@@ -106,8 +109,9 @@ class SecretBox(encoding.Encodable, StringFixer, object):
ciphertext = ciphertext[self.NONCE_SIZE:]
if len(nonce) != self.NONCE_SIZE:
raise ValueError("The nonce must be exactly %s bytes long" %
self.NONCE_SIZE)
raise ValueError(
"The nonce must be exactly %s bytes long" % self.NONCE_SIZE,
)
plaintext = nacl.c.crypto_secretbox_open(self._key, ciphertext, nonce)
......
......@@ -64,8 +64,10 @@ class VerifyKey(encoding.Encodable, StringFixer, object):
key = encoder.decode(key)
if len(key) != nacl.c.crypto_sign_PUBLICKEYBYTES:
raise ValueError("The key must be exactly %s bytes long" %
nacl.c.crypto_sign_PUBLICKEYBYTES)
raise ValueError(
"The key must be exactly %s bytes long" %
nacl.c.crypto_sign_PUBLICKEYBYTES,
)
self._key = key
......@@ -122,8 +124,10 @@ class SigningKey(encoding.Encodable, StringFixer, object):
# Verify that our seed is the proper size
if len(seed) != nacl.c.crypto_sign_SEEDBYTES:
raise ValueError("The seed must be exactly %d bytes long" %
nacl.c.crypto_sign_SEEDBYTES)
raise ValueError(
"The seed must be exactly %d bytes long" %
nacl.c.crypto_sign_SEEDBYTES
)
secret_key, public_key = nacl.c.crypto_sign_seed_keypair(seed)
......
......@@ -27,8 +27,15 @@ VECTORS = [
b"5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb",
b"de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f",
b"69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37",
b"be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffce5ecbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb310e3be8250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde048977eb48f59ffd4924ca1c60902e52f0a089bc76897040e082f937763848645e0705",
b"f3ffc7703f9400e52a7dfb4b3d3305d98e993b9f48681273c29650ba32fc76ce48332ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74e355a5",
(b"be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffce5e"
b"cbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb310e3be8"
b"250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde048977eb4"
b"8f59ffd4924ca1c60902e52f0a089bc76897040e082f937763848645e0705"),
(b"f3ffc7703f9400e52a7dfb4b3d3305d98e993b9f48681273c29650ba32fc76ce483"
b"32ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c97271d2c2"
b"0f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae902243685"
b"17acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b37973f622a43d"
b"14a6599b1f654cb45a74e355a5"),
),
]
......@@ -45,29 +52,43 @@ def test_box_creation():
Box(pk, sk)
@pytest.mark.parametrize(("skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext", "ciphertext"), VECTORS)
def test_box_encryption(skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
@pytest.mark.parametrize(
(
"skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext",
"ciphertext",
),
VECTORS,
)
def test_box_encryption(
skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
pkalice = PublicKey(pkalice, encoder=HexEncoder)
skbob = PrivateKey(skbob, encoder=HexEncoder)
box = Box(skbob, pkalice)
encrypted = box.encrypt(
binascii.unhexlify(plaintext),
binascii.unhexlify(nonce),
encoder=HexEncoder,
)
binascii.unhexlify(plaintext),
binascii.unhexlify(nonce),
encoder=HexEncoder,
)
expected = binascii.hexlify(
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext),
)
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext),
)
assert encrypted == expected
assert encrypted.nonce == nonce
assert encrypted.ciphertext == ciphertext
@pytest.mark.parametrize(("skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext", "ciphertext"), VECTORS)
def test_box_decryption(skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
@pytest.mark.parametrize(
(
"skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext",
"ciphertext",
),
VECTORS,
)
def test_box_decryption(
skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
pkbob = PublicKey(pkbob, encoder=HexEncoder)
skalice = PrivateKey(skalice, encoder=HexEncoder)
......@@ -75,27 +96,43 @@ def test_box_decryption(skalice, pkalice, skbob, pkbob, nonce, plaintext, cipher
nonce = binascii.unhexlify(nonce)
decrypted = binascii.hexlify(
box.decrypt(ciphertext, nonce, encoder=HexEncoder))
box.decrypt(ciphertext, nonce, encoder=HexEncoder),
)
assert decrypted == plaintext
@pytest.mark.parametrize(("skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext", "ciphertext"), VECTORS)
def test_box_decryption_combined(skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
@pytest.mark.parametrize(
(
"skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext",
"ciphertext",
),
VECTORS,
)
def test_box_decryption_combined(
skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
pkbob = PublicKey(pkbob, encoder=HexEncoder)
skalice = PrivateKey(skalice, encoder=HexEncoder)
box = Box(skalice, pkbob)
combined = binascii.hexlify(
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext))
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext),
)
decrypted = binascii.hexlify(box.decrypt(combined, encoder=HexEncoder))
assert decrypted == plaintext
@pytest.mark.parametrize(("skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext", "ciphertext"), VECTORS)
def test_box_failed_decryption(skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
@pytest.mark.parametrize(
(
"skalice", "pkalice", "skbob", "pkbob", "nonce", "plaintext",
"ciphertext",
),
VECTORS,
)
def test_box_failed_decryption(
skalice, pkalice, skbob, pkbob, nonce, plaintext, ciphertext):
pkbob = PublicKey(pkbob, encoder=HexEncoder)
skbob = PrivateKey(skbob, encoder=HexEncoder)
......
......@@ -34,11 +34,13 @@ def test_sha256_hex(inp, expected):
@pytest.mark.parametrize(("inp", "expected"), [
(
b"The quick brown fox jumps over the lazy dog.",
b"\xEFS\x7F%\xC8\x95\xBF\xA7\x82Re)\xA9\xB6=\x97\xAAc\x15d\xD5\xD7\x89\xC2\xB7eD\x8C\x865\xFBl",
(b"\xEFS\x7F%\xC8\x95\xBF\xA7\x82Re)\xA9\xB6=\x97\xAAc\x15d\xD5\xD7"
b"\x89\xC2\xB7eD\x8C\x865\xFBl"),
),
(
b"",
b"\xe3\xb0\xc4B\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99o\xb9$'\xaeA\xe4d\x9b\x93L\xa4\x95\x99\x1bxR\xb8U",
(b"\xe3\xb0\xc4B\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99o\xb9$'\xaeA\xe4d"
b"\x9b\x93L\xa4\x95\x99\x1bxR\xb8U"),
)
])
def test_sha256_binary(inp, expected):
......@@ -48,11 +50,13 @@ def test_sha256_binary(inp, expected):
@pytest.mark.parametrize(("inp", "expected"), [
(
b"The quick brown fox jumps over the lazy dog.",
b"91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed",
(b"91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c"
b"7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed"),
),
(
b"",
b"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
(b"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d"
b"0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"),
)
])
def test_sha512_hex(inp, expected):
......@@ -62,11 +66,16 @@ def test_sha512_hex(inp, expected):
@pytest.mark.parametrize(("inp", "expected"), [
(
b"The quick brown fox jumps over the lazy dog.",
b"\x91\xEA\x12E\xF2\rF\xAE\x9A\x03z\x98\x9FT\xF1\xF7\x90\xF0\xA4v\a\xEE\xB8\xA1M\x12\x89\f\xEAw\xA1\xBB\xC6\xC7\xED\x9C\xF2\x05\xE6{\x7F+\x8F\xD4\xC7\xDF\xD3\xA7\xA8a~E\xF3\xC4c\xD4\x81\xC7\xE5\x86\xC3\x9A\xC1\xED",
(b"\x91\xEA\x12E\xF2\rF\xAE\x9A\x03z\x98\x9FT\xF1\xF7\x90\xF0\xA4v\a"
b"\xEE\xB8\xA1M\x12\x89\f\xEAw\xA1\xBB\xC6\xC7\xED\x9C\xF2\x05\xE6{"
b"\x7F+\x8F\xD4\xC7\xDF\xD3\xA7\xA8a~E\xF3\xC4c\xD4\x81\xC7\xE5\x86"
b"\xC3\x9A\xC1\xED"),
),
(
b"",
b"\xCF\x83\xE15~\xEF\xB8\xBD\xF1T(P\xD6m\x80\a\xD6 \xE4\x05\vW\x15\xDC\x83\xF4\xA9!\xD3l\xE9\xCEG\xD0\xD1<]\x85\xF2\xB0\xFF\x83\x18\xD2\x87~\xEC/c\xB91\xBDGAz\x81\xA582z\xF9'\xDA>",
(b"\xCF\x83\xE15~\xEF\xB8\xBD\xF1T(P\xD6m\x80\a\xD6 \xE4\x05\vW\x15"
b"\xDC\x83\xF4\xA9!\xD3l\xE9\xCEG\xD0\xD1<]\x85\xF2\xB0\xFF\x83\x18"
b"\xD2\x87~\xEC/c\xB91\xBDGAz\x81\xA582z\xF9'\xDA>"),
)
])
def test_sha512_binary(inp, expected):
......
......@@ -23,8 +23,15 @@ VECTORS = [
(
b"1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389",
b"69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37",
b"be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffce5ecbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb310e3be8250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde048977eb48f59ffd4924ca1c60902e52f0a089bc76897040e082f937763848645e0705",
b"f3ffc7703f9400e52a7dfb4b3d3305d98e993b9f48681273c29650ba32fc76ce48332ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74e355a5",
(b"be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffce5e"
b"cbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb310e3be8"
b"250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde048977eb4"
b"8f59ffd4924ca1c60902e52f0a089bc76897040e082f937763848645e0705"),
(b"f3ffc7703f9400e52a7dfb4b3d3305d98e993b9f48681273c29650ba32fc76ce483"
b"32ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c97271d2c2"
b"0f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae902243685"
b"17acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b37973f622a43d"
b"14a6599b1f654cb45a74e355a5"),
),
]
......@@ -40,14 +47,14 @@ def test_secret_box_creation():
def test_secret_box_encryption(key, nonce, plaintext, ciphertext):
box = SecretBox(key, encoder=HexEncoder)
encrypted = box.encrypt(
binascii.unhexlify(plaintext),
binascii.unhexlify(nonce),
encoder=HexEncoder,
)
binascii.unhexlify(plaintext),
binascii.unhexlify(nonce),
encoder=HexEncoder,
)
expected = binascii.hexlify(
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext),
)
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext),
)
assert encrypted == expected
assert encrypted.nonce == nonce
......@@ -60,7 +67,8 @@ def test_secret_box_decryption(key, nonce, plaintext, ciphertext):
nonce = binascii.unhexlify(nonce)
decrypted = binascii.hexlify(
box.decrypt(ciphertext, nonce, encoder=HexEncoder))
box.decrypt(ciphertext, nonce, encoder=HexEncoder),
)
assert decrypted == plaintext
......@@ -70,7 +78,8 @@ def test_secret_box_decryption_combined(key, nonce, plaintext, ciphertext):
box = SecretBox(key, encoder=HexEncoder)
combined = binascii.hexlify(
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext))
binascii.unhexlify(nonce) + binascii.unhexlify(ciphertext),
)
decrypted = binascii.hexlify(box.decrypt(combined, encoder=HexEncoder))
assert decrypted == plaintext
......@@ -37,7 +37,9 @@ def ed25519_known_answers():
"public_key": x[1].encode("ascii"),
"message": x[2].encode("ascii"),
"signed": x[3].encode("ascii"),
"signature": binascii.hexlify(binascii.unhexlify(x[3].encode("ascii"))[:64]),
"signature": binascii.hexlify(
binascii.unhexlify(x[3].encode("ascii"))[:64],
),
})
return answers
......@@ -54,13 +56,22 @@ class TestSigningKey:
def test_initialization_with_seed(self, seed):
nacl.signing.SigningKey(seed, encoder=nacl.encoding.HexEncoder)
@pytest.mark.parametrize(("seed", "message", "signature", "expected"),
[(x["seed"], x["message"], x["signature"], x["signed"])
for x in ed25519_known_answers()]
)
@pytest.mark.parametrize(
("seed", "message", "signature", "expected"),
[
(x["seed"], x["message"], x["signature"], x["signed"])
for x in ed25519_known_answers()
],
)
def test_message_signing(self, seed, message, signature, expected):
signing_key = nacl.signing.SigningKey(seed, encoder=nacl.encoding.HexEncoder)
signed = signing_key.sign(binascii.unhexlify(message), encoder=nacl.encoding.HexEncoder)
signing_key = nacl.signing.SigningKey(
seed,
encoder=nacl.encoding.HexEncoder,
)
signed = signing_key.sign(
binascii.unhexlify(message),
encoder=nacl.encoding.HexEncoder,
)
assert signed == expected
assert signed.message == message
......@@ -69,14 +80,26 @@ class TestSigningKey:
class TestVerifyKey:
@pytest.mark.parametrize(("public_key", "signed", "message", "signature"),
[(x["public_key"], x["signed"], x["message"], x["signature"]) for x in ed25519_known_answers()]
@pytest.mark.parametrize(
("public_key", "signed", "message", "signature"),
[
(x["public_key"], x["signed"], x["message"], x["signature"])
for x in ed25519_known_answers()
]
)
def test_valid_signed_message(self, public_key, signed, message, signature):
key = nacl.signing.VerifyKey(public_key, encoder=nacl.encoding.HexEncoder)
def test_valid_signed_message(
self, public_key, signed, message, signature):
key = nacl.signing.VerifyKey(
public_key,
encoder=nacl.encoding.HexEncoder,
)
assert binascii.hexlify(key.verify(signed, encoder=nacl.encoding.HexEncoder)) == message
assert binascii.hexlify(key.verify(message, signature, encoder=nacl.encoding.HexEncoder)) == message
assert binascii.hexlify(
key.verify(signed, encoder=nacl.encoding.HexEncoder),
) == message
assert binascii.hexlify(
key.verify(message, signature, encoder=nacl.encoding.HexEncoder),
) == message
def test_invalid_signed_message(self):
skey = nacl.signing.SigningKey.generate()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment