scrypt密钥派生函数库的绑定
scrypt的Python项目详细描述
当加密密码时,scrypt非常有用,因为它可以指定 加密和解密时使用的最短时间。如果, 例如,密码需要0.05秒来验证,用户不会 请注意,登录时稍有延迟,但使用了暴力 搜索几十亿个密码需要相当多的 时间。这与更传统的散列函数(如 MD5或SHA系列,可以以极低的成本快速实现 硬件。
安装
对于debian和ubuntu,请确保安装了以下软件包:
$ sudo apt-get install build-essential libssl-dev python-dev
对于Fedora和RHEL衍生物,请确保安装了以下软件包:
$ sudo yum install gcc openssl-devel python-devel
对于OSX,请执行以下操作:
$ brew install openssl $ export CFLAGS="-I$(brew --prefix openssl)/include $CFLAGS" $ export LDFLAGS="-L$(brew --prefix openssl)/lib $LDFLAGS"
对于OSX,也可以使用预编译的控制盘。它们的安装者:
$ pip install scrypt
对于Windows,请使用预编译的轮子。它们的安装者:
$ pip install scrypt
对于windows,在编译包时,需要https://slproweb.com/products/Win32OpenSSL.html中的开发包。 它需要安装到c:openssl-win64。
如果需要最新的 但可能是非编译版本:
$ 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
或者您可以安装pypi的最新版本:
$ pip install scrypt
Anacondapython发行版的用户可以直接获得预构建的 来自conda forge频道的windows、intel linux或macos/osx二进制文件。 这可以通过:
$ conda install -c conda-forge scrypt
如果您希望python 3环境使用py-scrypt,只需运行 使用python 3解释器执行以上命令。py scrypt同时支持 Python2和3。
从0.6.0版(pypi上尚未提供)开始,py-scrypt支持 皮比也是。
更改日志
0.8.13
- 改进Conda Forge的构建
0.8.12
- 添加scrypt_windows_link_legacy_openssl环境变量,设置后,openssl 1.0.2被链接
0.8.11
- 康达原料的固定生产
0.8.10
- 修复输入错误
0.8.9
- 在Windows和OpenSSL 1.1.1中使用static libcrypto\u static
0.8.8
- Windows的setup.py已改进,可与OpenSSL 1.0.2和1.1.1一起使用
0.8.7
- Windows的setup.py已修复
0.8.6
- setup.py已修复,无法在0.8.5版中导入scrypt
0.8.5
- 固定的manifest.in
- py已使用init.py移动到自己的scrypt目录中
- 已修复OSX控制盘的OpenSSL库路径
0.8.4
- _版本添加到scrypt
- sha256.c中缺少void已修复
0.8.3
- scrypt更新到1.2.1
- 轮子是为Python3.6创建的
用法
在加密/解密之前,库导出两个函数 encrypt和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
通过这些,可以使用以下内容制作一个简单的密码验证器 功能:
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
但是,如果你想要的输出是确定的和大小不变的, 您可以使用hash函数:
>>> 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
致谢
Scrypt由colin percival创建,并被授权为2子句bsd。 由于scrypt通常不构建为共享库,因此我包括 此库中当前最新版本的源 储存库。当新版本到来时,我将更新这些源代码。
Kelvin Wong在bitbucket上提供更改以使库 可在Mac OS X 10.6及更早版本上使用,也可更改为把 库的工作方式更像命令行版本的scrypt by 违约。Kelvin也参与了单元测试 平台测试并处理hash函数。
Burstaholic在bitbucket上提供了必要的更改 这个库是在windows上构建的。
用于设置自动窗口的python-appveyor-demo存储库 为多种python版本构建。
许可证
此库的许可证与scrypt;2条款bsd的许可证相同。