2

我需要使用 scrypt 算法,因为我已经在使用 hashlib,我想......为什么不呢?我已经检查过,它指出 OpenSSL 1.1+ 是必要的。另外,根据官方文档

hashlib.scrypt(密码,*,盐,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)

4

1 回答 1

3

我的 Mac 正在运行OpenSSL 1.1.1 11 Sep 2018. 我用python3.6重现了你的导入症状,发现scrypt用python3.7导入就好了。您可以考虑尝试 3.7。

签名中的*是一种相对较新的语法,它标志着位置参数的结束。所以你不能调用 as scrypt('secret', 'mySalt')。您需要指定关键字参数,例如scrypt('secret', salt='mySalt'). 目的是通过使用错误的 arg 顺序来增加错误调用的难度。这对于加密 API 尤其重要,其中许多参数是不透明的且难以验证。

于 2019-03-27T01:56:21.417 回答