Scrypt for Python
There are a lot of different scrypt modules for Python, but none of them have everything that I’d like, so here’s One More1.
- Uses hashlib.scrypt on Python 3.6 and OpenSSL 1.1.
- Uses system libscrypt2 as the next choice.
- If neither is available, tries the scrypt Python module3 or libsodium4.
- Offers a pure Python scrypt implementation for when there is no C scrypt.
- Not unusably slow, even in pure Python… at least with pypy5.
With PyPy as the interpreter the Python implementation is around one fifth the speed of C scrypt. With CPython it is between about 50x and 250x slower.
- Python 2.7 or 3.4 or so. Pypy 2.2 also works. Older versions may or may not.
- If you want speed, you should use one of:
- libscrypt 1.8+ (older may work)
- py-scrypt 0.6+ (pip install scrypt)
- libsodium 0.6+
- Python 3.6+ with OpenSSL 1.1+
You can install the most recent release from PyPi using:
pip install pylibscrypt
You most likely want to create MCF hashes and store them somewhere, then check user-entered passwords against those hashes. For that you only need to use two functions from the API:
from pylibscrypt import scrypt_mcf, scrypt_mcf_check # Generate an MCF hash with random salt mcf = scrypt_mcf('Hello World') # Test it print(scrypt_mcf_check(mcf, 'Hello World')) # prints True print(scrypt_mcf_check(mcf, 'HelloPyWorld')) # prints False
For full API, you can try help(pylibscrypt) from python after importing.
It is highly recommended that you use a random salt, i.e. don’t pass one.
The package has a version number that can be read from python like so:
The version number is of the form X.Y.Z, following Semantic Versioning6. Releases are tagged vX.Y.Z and release branches bX.Y.x when they differ from master.
Development happens on GitHub7. If you find a bug, please open an issue there.
Running pylibscrypt.tests will test all implementations with some quick tests. Running any implementation directly (e.g. pylibscrypt.pylibsodium) will also compare to scrypt test vectors from the paper but this is slow for the pure Python version (pypyscrypt) unless running with pypy.
Running ‘make coverage’ will run coverage tests. This requires coverage.py installed for both Python 2 and Python 3. That would be a good idea on anything pushed for a pull request.
Pull requests should be automatically tested and will not be merged if broken.