Skip to main content

Bindings for the scrypt key derivation function library

Project description

This is a set of Python bindings for the scrypt key derivation function.

Scrypt is useful when encrypting password as it is possible to specify a minimum amount of time to use when encrypting and decrypting. If, for example, a password takes 0.05 seconds to verify, a user won’t notice the slight delay when signing in, but doing a brute force search of several billion passwords will take a considerable amount of time. This is in contrast to more traditional hash functions such as MD5 or the SHA family which can be implemented extremely fast on cheap hardware.

Installation

You can install py-scrypt from this repository if you want the latest but possibly non-compiling version:

$ hg clone http://bitbucket.org/mhallin/py-scrypt
$ cd py-scrypt
$ python setup.py build

Become superuser (or use virtualenv):
# python setup.py install

Run tests after install:
$ python setup.py test

Or you can install the latest release from PyPi:

$ pip install scrypt

If you want py-scrypt for your Python 3 environment, just run the above commands with your Python 3 interpreter. Py-scrypt supports both Python 2 and 3.

>From version 0.6.0 (not available on PyPi yet), py-scrypt supports PyPy as well.

Usage

Fore encryption/decryption, the library exports two functions encrypt and decrypt:

>>> import scrypt
>>> data = scrypt.encrypt('a secret message', 'password', maxtime=0.1) # This will take at least 0.1 seconds
>>> data[:20]
'scrypt\x00\r\x00\x00\x00\x08\x00\x00\x00\x01RX9H'
>>> scrypt.decrypt(data, 'password', maxtime=0.1) # This will also take at least 0.1 seconds
'a secret message'
>>> scrypt.decrypt(data, 'password', maxtime=0.05) # scrypt won't be able to decrypt this data fast enough
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
scrypt.error: decrypting file would take too long
>>> scrypt.decrypt(data, 'wrong password', maxtime=0.1) # scrypt will throw an exception if the password is incorrect
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
scrypt.error: password is incorrect

>From these, one can make a simple password verifier using the following functions:

def hash_password(password, maxtime=0.5, datalength=64):
    return scrypt.encrypt(os.urandom(datalength), password, maxtime=maxtime)

def verify_password(hashed_password, guessed_password, maxtime=0.5):
    try:
        scrypt.decrypt(hashed_password, guessed_password, maxtime)
        return True
    except scrypt.error:
        return False

But, if you want output that is deterministic and constant in size, you can use the hash function:

>>> import scrypt
>>> h1 = scrypt.hash('password', 'random salt')
>>> len(h1)  # The hash will be 64 bytes by default, but is overridable.
64
>>> h1[:10]
'\xfe\x87\xf3hS\tUo\xcd\xc8'
>>> h2 = scrypt.hash('password', 'random salt')
>>> h1 == h2 # The hash function is deterministic
True

Acknowledgements

Scrypt was created by Colin Percival and is licensed as 2-clause BSD. Since scrypt does not normally build as a shared library, I have included the source for the currently latest version of the library in this repository. When a new version arrives, I will update these sources.

Kelvin Wong on Bitbucket provided changes to make the library available on Mac OS X 10.6 and earlier, as well as changes to make the library work more like the command-line version of scrypt by default. Kelvin also contributed with the unit tests, lots of cross platform testing and work on the hash function.

Burstaholic on Bitbucket provided the necessary changes to make the library build on Windows.

License

This library is licensed under the same license as scrypt; 2-clause BSD.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scrypt-0.7.1.tar.gz (30.4 kB view details)

Uploaded Source

Built Distributions

scrypt-0.7.1-pp32-none-macosx_10_10_x86_64.whl (19.1 kB view details)

Uploaded PyPy macOS 10.10+ x86-64

scrypt-0.7.1-pp27-none-macosx_10_10_x86_64.whl (19.1 kB view details)

Uploaded PyPy macOS 10.10+ x86-64

scrypt-0.7.1-cp34-none-win32.whl (20.6 kB view details)

Uploaded CPython 3.4 Windows x86

scrypt-0.7.1-cp34-cp34m-macosx_10_10_x86_64.whl (20.2 kB view details)

Uploaded CPython 3.4m macOS 10.10+ x86-64

scrypt-0.7.1-cp27-none-win32.whl (20.7 kB view details)

Uploaded CPython 2.7 Windows x86

scrypt-0.7.1-cp27-none-macosx_10_10_intel.whl (32.1 kB view details)

Uploaded CPython 2.7 macOS 10.10+ intel

File details

Details for the file scrypt-0.7.1.tar.gz.

File metadata

  • Download URL: scrypt-0.7.1.tar.gz
  • Upload date:
  • Size: 30.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for scrypt-0.7.1.tar.gz
Algorithm Hash digest
SHA256 f5f68c300c816178384b6531f071a07e705beb1ecd9b37ba8fe3960c20f61da3
MD5 9feb713f183e11caa940e8ec71cf1361
BLAKE2b-256 f6fa2e80258d58555d38ba0373db3c8947e03ceaaa91ad9f80725802bc05aa77

See more details on using hashes here.

File details

Details for the file scrypt-0.7.1-pp32-none-macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for scrypt-0.7.1-pp32-none-macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 334e0cf62c7224cbd7ed58c8fe30f24b6a1be1ad771d711fc06c05a696c93a15
MD5 7be988743e4d29359af316886cc5c6cb
BLAKE2b-256 84070d2231f3ecbac5199981d314f198a3baa8f4b6bc972f0e8a5da0f3cb6d4b

See more details on using hashes here.

File details

Details for the file scrypt-0.7.1-pp27-none-macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for scrypt-0.7.1-pp27-none-macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 5c70824afca40fc8ed220b81c105d171c929c3f50d3f8ef5538a1a7912db9231
MD5 289f620d6dcde1ccdae6ba8693fb3220
BLAKE2b-256 a16338bfdfddb3ecb943f34907632c2e521cec1d93552086a2ffa3b0a1fbb499

See more details on using hashes here.

File details

Details for the file scrypt-0.7.1-cp34-none-win32.whl.

File metadata

File hashes

Hashes for scrypt-0.7.1-cp34-none-win32.whl
Algorithm Hash digest
SHA256 fd70c029bde5fcef859ae6f4852fc702b24c2b78f262b082b8071cf950275039
MD5 f8c4a2aac0eb5343e3b4157083ef315e
BLAKE2b-256 7dfc3e65f21be05f5f1bafa8f2262fea474c6dd84cc7dc226d453dd488675305

See more details on using hashes here.

File details

Details for the file scrypt-0.7.1-cp34-cp34m-macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for scrypt-0.7.1-cp34-cp34m-macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 2ab0bb4d37e7f845badfc243efe43b04fcde98d558041e5514c0f834e996d393
MD5 5ccbb94e3935f3ebdde8a1f26ed2097f
BLAKE2b-256 d3e95913b06fdea667841eda569b980265e8fcd000cb47b8825a897a63bd7187

See more details on using hashes here.

File details

Details for the file scrypt-0.7.1-cp27-none-win32.whl.

File metadata

File hashes

Hashes for scrypt-0.7.1-cp27-none-win32.whl
Algorithm Hash digest
SHA256 e5891156b2b1bd07eb10a0bfe2322a3afee464c92f431247193f60a6d8650cf6
MD5 d6e422d640187eea9a2c742322c03b66
BLAKE2b-256 3bb163fd84d66e8479c622fe9c0063bb146f4f1f9aa346129b4aa6ac4d6f7b57

See more details on using hashes here.

File details

Details for the file scrypt-0.7.1-cp27-none-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for scrypt-0.7.1-cp27-none-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 5d26b9f6905fcd0f8f585400ac3d949e7cfc0865633be2ae6aff7a09af46b504
MD5 937e587c7ee2d34c136b59d7a6115cd9
BLAKE2b-256 5397b737fcaf8bbe4850a8cd3e2b0549190657820bc61012efb2d874272ebce6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page