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 randstr(length):
    return ''.join(chr(random.randint(0,255)) for i in range(length))

def hash_password(password, maxtime=0.5, datalength=64):
    return scrypt.encrypt(randstr(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.0.tar.gz (29.2 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.4 Windows x86

scrypt-0.7.0-cp34-cp34m-macosx_10_10_x86_64.whl (20.1 kB view details)

Uploaded CPython 3.4m macOS 10.10+ x86-64

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

Uploaded CPython 2.7 Windows x86

scrypt-0.7.0-cp27-none-macosx_10_10_intel.whl (31.9 kB view details)

Uploaded CPython 2.7 macOS 10.10+ intel

File details

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

File metadata

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

File hashes

Hashes for scrypt-0.7.0.tar.gz
Algorithm Hash digest
SHA256 a5c3dacd3770779ccaa7c81cf8611f4a25e256c2f10602972134cd68783aa56f
MD5 5be8026af27adc430a923d53e1114174
BLAKE2b-256 476d43520bedaac7c5c2b2a9e5a8229d82ca2c2242c34b46575ed7b988f248a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scrypt-0.7.0-cp34-none-win32.whl
Algorithm Hash digest
SHA256 0dd719282afd55c41afbed5e97fb4aff6e4ec085851baf67da6bb887ee9d087c
MD5 30f5172ff6ea2bfa5f05fc7804609bb2
BLAKE2b-256 f62aecab79a89c27a81a3b0b54ad8121604ad0a35f58d8df55e8ea878831b374

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scrypt-0.7.0-cp34-cp34m-macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 159c13addb9e5294847c708223363d25b49e1b91b01694b6617ae579516855e5
MD5 bae59486e88183f273b7d545a7714c59
BLAKE2b-256 3d1bfe4f56b3082674bb05eb21c7e824319b5749a6fcb5589085c5c3f269828b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scrypt-0.7.0-cp27-none-win32.whl
Algorithm Hash digest
SHA256 a64f4aef69fcf550463da279fc0232a56aaac8c6281ffcaa974681c9b94552c2
MD5 593acbc551b0ca45d3f311612ef011d1
BLAKE2b-256 2f9eee4ad9b83df1cc3dd354d7c4717766db0b92e92574a0e0944208c4948dbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scrypt-0.7.0-cp27-none-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 c279edcefa4ac0900b1b965fc002df0109c7eaf003de7942e57160859d5a17b1
MD5 73c2d7602a85865dc48d2c5664d85f5d
BLAKE2b-256 901ebc2ca1d01a4d43167bce9192e015727465582132585697dabcf90db3d6ae

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