用法:
hashlib.pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)
該函數提供PKCS#5基於密碼的 key 推導函數2。它使用HMAC作為偽隨機函數。
字符串
hash_name
是 HMAC 哈希摘要算法的所需名稱,例如‘sha1’ 或 ‘sha256’。password
和salt
被解釋為字節緩衝區。應用程序和庫應將password
限製為合理的長度(例如 1024)。salt
應該是來自適當來源的大約 16 個或更多字節,例如os.urandom()
。iterations
的數量應根據哈希算法和計算能力來選擇。截至 2022 年,建議使用數十萬次 SHA-256 迭代。有關為什麽以及如何選擇最適合您的應用程序的理由,請閱讀Appendix A.2.2
of NIST-SP-800-132 。 stackexchange pbkdf2 iterations question上的答案詳細解釋。dklen
是派生 key 的長度。如果dklen
是None
,則使用散列算法hash_name
的摘要大小,例如SHA-512 為 64。>>> from hashlib import pbkdf2_hmac >>> our_app_iters = 500_000 # Application specific, read above. >>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters) >>> dk.hex() '15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
3.4 版中的新函數。
注意
OpenSSL 提供了
pbkdf2_hmac
的快速實現。 Python 實現使用hmac
的內聯版本。它慢了大約三倍,並且不釋放 GIL。自 3.10 版起已棄用:緩慢的Python實現
pbkdf2_hmac
已棄用。將來,該函數將僅在使用 OpenSSL 編譯 Python 時可用。
相關用法
- Python hashlib.sha3_256()用法及代碼示例
- Python hashlib.sha3_512()用法及代碼示例
- Python hashlib.blake2s()用法及代碼示例
- Python hashlib.blake2b()用法及代碼示例
- Python hashlib.sha3_224()用法及代碼示例
- Python hashlib.shake_256()用法及代碼示例
- Python hashlib.shake_128()用法及代碼示例
- Python hashlib.sha3_384()用法及代碼示例
- Python hash()用法及代碼示例
- Python hasattr()用法及代碼示例
- Python statistics harmonic_mean()用法及代碼示例
- Python OpenCV haveImageReader()用法及代碼示例
- Python help()用法及代碼示例
- Python http.HTTPStatus用法及代碼示例
- Python html.escape()用法及代碼示例
- Python html.unescape()用法及代碼示例
- Python math hypot()用法及代碼示例
- Python hex()用法及代碼示例
- Python http.client.HTTPConnection.set_tunnel用法及代碼示例
- Python http.client.HTTPConnection用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 hashlib.pbkdf2_hmac。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。