Python:无法从hashlib导入scrypt

3

我需要使用scrypt算法,既然已经在使用hashlib,那么我觉得...为什么不尝试一下呢?我已经检查了这篇文章,指出需要OpenSSL 1.1+。同时,根据官方文档的说明:

hashlib.scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)

...

可用性:需要OpenSSL 1.1+。

版本3.6中新增。

我确保已安装最新版的openssl:

# openssl version
OpenSSL 1.1.1b  26 Feb 2019

我也尝试运行python3.6 和python3 (3.4) ,但两者都无法导入scrypt:

# python3.6
Python 3.6.5 (default, Apr 10 2018, 17:08:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from hashlib import pbkdf2_hmac
>>> from hashlib import scrypt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'scrypt'

如您所见,像 pbkdf2_hmac 这样的其他方法可以使用。可能出了什么问题?

另外,hashlib.scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64) 中的 * 是什么意思?

1个回答

4

我的Mac版本使用的是OpenSSL 1.1.1 11 Sep 2018。 我用Python3.6复现了您的问题, 并发现在Python3.7中,scrypt可以顺利导入。 您可以考虑尝试3.7。

签名中的*是一个相对较新的语法,它标记了位置参数的结尾。 因此,您不能像这样调用:scrypt('secret', 'mySalt')。 您需要指定关键字参数,例如:scrypt('secret', salt='mySalt')。 目的是通过使用错误的参数顺序来使其难以被错误调用。 这对于加密API特别重要, 其中许多参数是不透明且难以验证的。


你说得对。我花了很多时间从源代码编译Python 3.7,但最终成功了。希望文档能更清晰一些。还有感谢你解释位置参数语法,我之前不知道这个。 - Adriano_Pinaffo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接