Python Glossary Русский

Python 3.6+hashlibfunction

hashlib.scrypt()

password hashing

Derives a key from a password with the scrypt algorithm (RFC 7914): slowly and with heavy memory use, which makes password cracking on GPUs harder.

Syntax

hashlib.scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)

Passwords are stored neither in plain text nor as sha256(password): a fast hash can be brute-forced at billions of guesses per second. scrypt is deliberately expensive: n is the cost factor (a power of two such as 2**14), r the block size, p the parallelism; memory grows as 128 * r * n bytes. The salt is 16+ random bytes from os.urandom, unique per password, stored next to the result. All arguments except the password are keyword-only. To verify a password, call it again with the same salt and compare with hmac.compare_digest. The function exists when Python is built with OpenSSL 1.1+.

Examples

import hashlib, hmac, os
salt = os.urandom(16)
key = hashlib.scrypt(b'secret', salt=salt, n=2**14, r=8, p=1)
print(len(key))  # → 64
again = hashlib.scrypt(b'secret', salt=salt, n=2**14, r=8, p=1)
print(hmac.compare_digest(key, again))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy