为什么python键派生函数“需要”一个可选参数:backend?

2024-05-15 21:06:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python的加密库生成一个依赖于密码的密钥:

这两个函数都从文档中删去:https://cryptography.io/en/latest/hazmat/primitives/key-derivation-functions/ 对于Scrypt和PBKDF2HMAC,backend都列为可选参数,但是当我运行这些函数时,会出现以下问题:

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时,我会得到一个错误,即后端不支持给定的算法


Tags: frompyimportmastercodesaltcryptographyscrypt