diff --git a/nacl/nacl.py b/nacl/nacl.py index 57ca20101fa8b4582b835ad5378fc94cd28ee3f1..592b41738b2bce0e76212923632f16b3b73ed614 100644 --- a/nacl/nacl.py +++ b/nacl/nacl.py @@ -1,9 +1,9 @@ """ -CFFI interface to libsodium the library +CFFI interface to NaCl and libsodium library """ import functools -from cffi import FFI +from cffi import FFI, VerificationError __all__ = ["ffi", "lib"] @@ -26,7 +26,23 @@ ffi.cdef( ) -lib = ffi.verify("#include <sodium.h>", libraries=["sodium"]) +# Check to make sure that we have a compiled interface to one of our library +# backends. We prefer NaCl here because it has compiled speed ups. +# TODO: Include some way to specify which backend you want and hard fail if +# that one doesn't exist? +try: + # Try to compile the ffi interface with NaCl + lib = ffi.verify( + """ + #include "crypto_hash.h" + #include "crypto_hash_sha256.h" + #include "crypto_hash_sha512.h" + """, + libraries=["nacl"], + ) +except VerificationError: + # Try to compile the ffi interface with libsodium if NaCl wasn't available + lib = ffi.verify("#include <sodium.h>", libraries=["sodium"]) # A lot of the functions in nacl return 0 for success and a negative integer