Giter VIP home page Giter VIP logo

py-scrypt's Introduction

Python scrypt bindings

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

$ 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 tests/scrypt-tests.py

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.

Usage

The bindings are very simple -- there is an encrypt and a decrypt method on the scrypt module:

>>> 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

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.

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

[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.

License

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

py-scrypt's People

Contributors

namtiny avatar entequak avatar

Stargazers

Oleg Anashkin avatar

Watchers

 avatar Adrian avatar Joel McGrady avatar Matthew Sun avatar Rob avatar  avatar jyen avatar  avatar  avatar  avatar  avatar  avatar Hyungchul  avatar  avatar Abhishek Nath avatar  avatar James Cloos avatar  avatar Michael Hamilton avatar  avatar Hillary Hopper avatar Taymoor Khan avatar  avatar  avatar Oscar Espinoza avatar  avatar  avatar  avatar Dmitri Barabanov avatar Cory Diers avatar  avatar Lamar avatar  avatar  avatar  avatar  avatar Dmitri avatar  avatar Scott Bauer avatar  avatar  avatar  avatar  avatar Seth avatar  avatar Scott Byrne (English Scott) avatar Eric Penzel avatar David Leistiko avatar  avatar Nicholas Kim avatar  avatar Jordan Joyner avatar Casey Kirkruff avatar  avatar  avatar  avatar  avatar Ry avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar Katrina Wolfe avatar  avatar  avatar Michael Duarte avatar DanielGutierrez avatar  avatar Yanny avatar  avatar Greg Broxterman avatar  avatar  avatar  avatar  avatar Jason Young avatar Daniel Montalvo avatar Mike Chen avatar David Suarez avatar  avatar  avatar  avatar

py-scrypt's Issues

Cannot build on Windows

After fixing my vcvarsall and openssl/aes.h not found errors I was left with this more serious output. Would welcome insights on how to resolve:

Downloading/unpacking scrypt
  Downloading scrypt-0.6.1.tar.gz
  Running setup.py (path:c:\users\samuel\appdata\local\temp\pip_build_Samuel\scrypt\setup.py) egg_info for package scrypt

Installing collected packages: scrypt
  Running setup.py install for scrypt
    building '_scrypt' extension
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DHAVE_CONFIG_H -Iscrypt-1.1.6 -Iscrypt-1.1.6/lib -Iscrypt-1.1.6/lib/scryptenc -Iscrypt-1.1.6/lib/crypto -Iscrypt-1.1.6/lib/util -Ic:\OpenSSL-Win32\include -IC:\usr\Python27\include -IC:\usr\Python27\PC /Tcsrc/scrypt.c /Fobuild\temp.win-amd64-2.7\Release\src/scrypt.obj
    scrypt.c
    c:\usr\python27\include\pymath.h(22) : warning C4273: 'round' : inconsistent dll linkage
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\math.h(516) : see previous definition of 'round'
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DHAVE_CONFIG_H -Iscrypt-1.1.6 -Iscrypt-1.1.6/lib -Iscrypt-1.1.6/lib/scryptenc -Iscrypt-1.1.6/lib/crypto -Iscrypt-1.1.6/lib/util -Ic:\OpenSSL-Win32\include -IC:\usr\Python27\include -IC:\usr\Python27\PC /Tcscrypt-1.1.6/lib/crypto/crypto_aesctr.c /Fobuild\temp.win-amd64-2.7\Release\scrypt-1.1.6/lib/crypto/crypto_aesctr.obj
    crypto_aesctr.c
    C:\usr\OpenSSL\include\openssl/aes.h(1) : error C2059: syntax error : '.'
    scrypt-1.1.6/lib/util\sysendian.h(57) : error C2054: expected '(' to follow 'inline'
    scrypt-1.1.6/lib/util\sysendian.h(58) : error C2085: 'be32enc' : not in formal parameter list
    scrypt-1.1.6/lib/util\sysendian.h(58) : error C2143: syntax error : missing ';' before '{'
    scrypt-1.1.6/lib/util\sysendian.h(68) : error C2054: expected '(' to follow 'inline'
    scrypt-1.1.6/lib/util\sysendian.h(69) : error C2085: 'be64dec' : not in formal parameter list
    scrypt-1.1.6/lib/util\sysendian.h(69) : error C2143: syntax error : missing ';' before '{'
    scrypt-1.1.6/lib/util\sysendian.h(79) : error C2054: expected '(' to follow 'inline'
    scrypt-1.1.6/lib/util\sysendian.h(80) : error C2085: 'be64enc' : not in formal parameter list
    scrypt-1.1.6/lib/util\sysendian.h(80) : error C2143: syntax error : missing ';' before '{'
    scrypt-1.1.6/lib/util\sysendian.h(94) : error C2054: expected '(' to follow 'inline'
    scrypt-1.1.6/lib/util\sysendian.h(95) : error C2085: 'le32dec' : not in formal parameter list
    scrypt-1.1.6/lib/util\sysendian.h(95) : error C2143: syntax error : missing ';' before '{'
    scrypt-1.1.6/lib/util\sysendian.h(103) : error C2054: expected '(' to follow 'inline'
    scrypt-1.1.6/lib/util\sysendian.h(104) : error C2085: 'le32enc' : not in formal parameter list
    scrypt-1.1.6/lib/util\sysendian.h(104) : error C2143: syntax error : missing ';' before '{'
    scrypt-1.1.6/lib/util\sysendian.h(114) : error C2054: expected '(' to follow 'inline'
    scrypt-1.1.6/lib/util\sysendian.h(115) : error C2085: 'le64dec' : not in formal parameter list
    scrypt-1.1.6/lib/util\sysendian.h(115) : error C2143: syntax error : missing ';' before '{'
    scrypt-1.1.6/lib/util\sysendian.h(125) : error C2054: expected '(' to follow 'inline'
    scrypt-1.1.6/lib/util\sysendian.h(126) : error C2085: 'le64enc' : not in formal parameter list
    scrypt-1.1.6/lib/util\sysendian.h(126) : error C2143: syntax error : missing ';' before '{'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(41) : error C2016: C requires that a struct or union has at least one member
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(41) : error C2061: syntax error : identifier 'AES_KEY'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(45) : error C2059: syntax error : '}'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2143: syntax error : missing ')' before '*'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2143: syntax error : missing '{' before '*'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2371: 'uint64_t' : redefinition; different basic types
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\stdint.h(15) : see declaration of 'uint64_t'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2146: syntax error : missing ';' before identifier 'nonce'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2059: syntax error : ')'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(55) : error C2371: 'nonce' : redefinition; different basic types
            scrypt-1.1.6/lib/crypto/crypto_aesctr.c(42) : see declaration of 'nonce'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(55) : error C2054: expected '(' to follow 'nonce'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(91) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(95) : warning C4013: 'be64enc' undefined; assuming extern returning int
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(95) : error C2037: left of 'nonce' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(96) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(97) : warning C4013: 'AES_encrypt' undefined; assuming extern returning int
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(97) : error C2037: left of 'buf' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(97) : error C2037: left of 'key' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(101) : error C2037: left of 'buf' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(104) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(119) : error C2037: left of 'buf' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(120) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'
    scrypt-1.1.6/lib/crypto/crypto_aesctr.c(120) : error C2037: left of 'nonce' specifies undefined struct/union 'crypto_aesctr'
    error: command '"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64\cl.exe"' failed with exit status 2
    Complete output from command C:\usr\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\samuel\\appdata\\local\\temp\\pip_build_Samuel\\scrypt\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\samuel\appdata\local\temp\pip-jbbmj7-record\install-record.txt --single-version-externally-managed --compile:
    running install

running build

running build_py

creating build

creating build\lib.win-amd64-2.7

copying scrypt.py -> build\lib.win-amd64-2.7

running build_ext

building '_scrypt' extension

creating build\temp.win-amd64-2.7

creating build\temp.win-amd64-2.7\Release

creating build\temp.win-amd64-2.7\Release\src

creating build\temp.win-amd64-2.7\Release\scrypt-1.1.6

creating build\temp.win-amd64-2.7\Release\scrypt-1.1.6\lib

creating build\temp.win-amd64-2.7\Release\scrypt-1.1.6\lib\crypto

creating build\temp.win-amd64-2.7\Release\scrypt-1.1.6\lib\scryptenc

creating build\temp.win-amd64-2.7\Release\scrypt-1.1.6\lib\util

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DHAVE_CONFIG_H -Iscrypt-1.1.6 -Iscrypt-1.1.6/lib -Iscrypt-1.1.6/lib/scryptenc -Iscrypt-1.1.6/lib/crypto -Iscrypt-1.1.6/lib/util -Ic:\OpenSSL-Win32\include -IC:\usr\Python27\include -IC:\usr\Python27\PC /Tcsrc/scrypt.c /Fobuild\temp.win-amd64-2.7\Release\src/scrypt.obj

scrypt.c

c:\usr\python27\include\pymath.h(22) : warning C4273: 'round' : inconsistent dll linkage

        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\math.h(516) : see previous definition of 'round'

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DHAVE_CONFIG_H -Iscrypt-1.1.6 -Iscrypt-1.1.6/lib -Iscrypt-1.1.6/lib/scryptenc -Iscrypt-1.1.6/lib/crypto -Iscrypt-1.1.6/lib/util -Ic:\OpenSSL-Win32\include -IC:\usr\Python27\include -IC:\usr\Python27\PC /Tcscrypt-1.1.6/lib/crypto/crypto_aesctr.c /Fobuild\temp.win-amd64-2.7\Release\scrypt-1.1.6/lib/crypto/crypto_aesctr.obj

crypto_aesctr.c

C:\usr\OpenSSL\include\openssl/aes.h(1) : error C2059: syntax error : '.'

scrypt-1.1.6/lib/util\sysendian.h(57) : error C2054: expected '(' to follow 'inline'

scrypt-1.1.6/lib/util\sysendian.h(58) : error C2085: 'be32enc' : not in formal parameter list

scrypt-1.1.6/lib/util\sysendian.h(58) : error C2143: syntax error : missing ';' before '{'

scrypt-1.1.6/lib/util\sysendian.h(68) : error C2054: expected '(' to follow 'inline'

scrypt-1.1.6/lib/util\sysendian.h(69) : error C2085: 'be64dec' : not in formal parameter list

scrypt-1.1.6/lib/util\sysendian.h(69) : error C2143: syntax error : missing ';' before '{'

scrypt-1.1.6/lib/util\sysendian.h(79) : error C2054: expected '(' to follow 'inline'

scrypt-1.1.6/lib/util\sysendian.h(80) : error C2085: 'be64enc' : not in formal parameter list

scrypt-1.1.6/lib/util\sysendian.h(80) : error C2143: syntax error : missing ';' before '{'

scrypt-1.1.6/lib/util\sysendian.h(94) : error C2054: expected '(' to follow 'inline'

scrypt-1.1.6/lib/util\sysendian.h(95) : error C2085: 'le32dec' : not in formal parameter list

scrypt-1.1.6/lib/util\sysendian.h(95) : error C2143: syntax error : missing ';' before '{'

scrypt-1.1.6/lib/util\sysendian.h(103) : error C2054: expected '(' to follow 'inline'

scrypt-1.1.6/lib/util\sysendian.h(104) : error C2085: 'le32enc' : not in formal parameter list

scrypt-1.1.6/lib/util\sysendian.h(104) : error C2143: syntax error : missing ';' before '{'

scrypt-1.1.6/lib/util\sysendian.h(114) : error C2054: expected '(' to follow 'inline'

scrypt-1.1.6/lib/util\sysendian.h(115) : error C2085: 'le64dec' : not in formal parameter list

scrypt-1.1.6/lib/util\sysendian.h(115) : error C2143: syntax error : missing ';' before '{'

scrypt-1.1.6/lib/util\sysendian.h(125) : error C2054: expected '(' to follow 'inline'

scrypt-1.1.6/lib/util\sysendian.h(126) : error C2085: 'le64enc' : not in formal parameter list

scrypt-1.1.6/lib/util\sysendian.h(126) : error C2143: syntax error : missing ';' before '{'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(41) : error C2016: C requires that a struct or union has at least one member

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(41) : error C2061: syntax error : identifier 'AES_KEY'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(45) : error C2059: syntax error : '}'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2143: syntax error : missing ')' before '*'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2143: syntax error : missing '{' before '*'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2371: 'uint64_t' : redefinition; different basic types

        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\stdint.h(15) : see declaration of 'uint64_t'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2146: syntax error : missing ';' before identifier 'nonce'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(54) : error C2059: syntax error : ')'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(55) : error C2371: 'nonce' : redefinition; different basic types

        scrypt-1.1.6/lib/crypto/crypto_aesctr.c(42) : see declaration of 'nonce'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(55) : error C2054: expected '(' to follow 'nonce'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(91) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(95) : warning C4013: 'be64enc' undefined; assuming extern returning int

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(95) : error C2037: left of 'nonce' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(96) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(97) : warning C4013: 'AES_encrypt' undefined; assuming extern returning int

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(97) : error C2037: left of 'buf' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(97) : error C2037: left of 'key' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(101) : error C2037: left of 'buf' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(104) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(119) : error C2037: left of 'buf' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(120) : error C2037: left of 'bytectr' specifies undefined struct/union 'crypto_aesctr'

scrypt-1.1.6/lib/crypto/crypto_aesctr.c(120) : error C2037: left of 'nonce' specifies undefined struct/union 'crypto_aesctr'

error: command '"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64\cl.exe"' failed with exit status 2

----------------------------------------
Cleaning up...
Command "C:\usr\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\samuel\\appdata\\local\\temp\\pip_build_Samuel\\scrypt\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\samuel\appdata\local\temp\pip-jbbmj7-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\samuel\appdata\local\temp\pip_build_Samuel\scrypt
Storing debug log for failure in C:\Users\Samuel\pip\pip.log
``

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.