簡體   English   中英

Python:無法從 hashlib 導入 scrypt

[英]Python: cannot import scrypt from hashlib

我需要使用 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)

我的 Mac 運行的是OpenSSL 1.1.1 11 Sep 2018 我用 python3.6 重現了你的導入症狀,發現scrypt用 python3.7 導入得很好。 您可能會考慮嘗試 3.7。

簽名中的*是一種相對較新的語法,它標志着位置參數的結束。 所以你不能調用scrypt('secret', 'mySalt') 您需要指定關鍵字參數,例如scrypt('secret', salt='mySalt') 目的是通過使用錯誤的 arg 順序使錯誤調用變得更加困難。 這對於加密 API 尤其重要,因為其中許多參數不透明且難以驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM