0

我正在尝试使用 python 的密码库来生成与密码相关的密钥:

这两个函数都从文档中复制出来:https://cryptography.io/en/latest/hazmat/primitives/key-derivation-functions/ 对于 Scrypt 和 PBKDF2HMAC 后端都被列为可选参数但是当我运行这些函数时我长大了:

PS D:\code\Fiver\flohar> & C:/Users/mpnlo/AppData/Local/Programs/Python/Python38-32/python.exe d:/code/Fiver/flohar/passwordManager.py
Traceback (most recent call last):
  File "d:/code/Fiver/flohar/passwordManager.py", line 202, in <module>
    main()
  File "d:/code/Fiver/flohar/passwordManager.py", line 152, in main
    key = generateKey('Password')
  File "d:/code/Fiver/flohar/passwordManager.py", line 24, in generateKey
    kdf = Scrypt(
TypeError: __init__() missing 1 required positional argument: 'backend'
PS D:\code\Fiver\flohar>
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt

def generateKey(master):
    salt = b'H\x1d\tMg\xc9\xe3\xec\xbeU\xee\x03\xec\x18\xf1U'
    kdf = Scrypt(
        length=32,
        salt=salt,
        n=2**14,
        r=8,
        p=1,
    )
    return base64.urlsafe_b64encode(kdf.derive(master))
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

def generateKey(master):
    salt = b'H\x1d\tMg\xc9\xe3\xec\xbeU\xee\x03\xec\x18\xf1U'
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
    )
    return base64.urlsafe_b64encode(kdf.derive(master))

模块是否收到了现在需要后端的更新,或者我做错了什么,当导入默认后端并使用这些或仅使用 None 作为后端参数时,我收到后端不支持给定算法的错误。

4

1 回答 1

0

我安装了错误的密码学版本,感谢 Topaco 向我指出这一点。

于 2021-06-22T22:17:03.103 回答