The documentation of hashlib.scrypt
is a bit short:
hashlib.scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)
The function provides scrypt password-based key derivation function as defined in RFC 7914.
password and salt must be bytes-like objects. Applications and libraries should limit password to a sensible length (e.g. 1024). salt should be about 16 or more bytes from a proper source, e.g. os.urandom().
n is the CPU/Memory cost factor, r the block size, p parallelization factor and maxmem limits memory (OpenSSL 1.1.0 defaults to 32 MiB). dklen is the length of the derived key.
I figured out that n
must be a power of 2 and at least 2.
Besides that, I feel a bit left alone. Would hashlib.scrypt(b"foo", salt=b"bar", n=2, r=1, p=1)
be considered safe today? How do I judge which parameters to take?
I was recently using
hashlib.scrypt
and I was also stumped what all these parameters minimum and maximum values were. You have likely answered your question, but I wanted to share my research just in case if you still have some open questions about these parameters.As you previously stated the documentation for
hashlib.scrypt
lacks a solid explanation or these parameters even forPython 3.11
RFC7914 - The scrypt Password-Based Key Derivation Function is also lite on details:
I found another reference, which explained these parameters in greater detail.
The
Scrypt
config parameters are:parameter
N
– iterations count (affects memory and CPU usage), e.g. 16384 (2 ** 14) or 2048 (2 ** 11)parameter
R
- block size (affects memory and CPU usage), e.g. 8parameter
P
– parallelism factor (threads to run in parallel - affects the memory, CPU usage), usually 1parameter
password
– the input password (8-10 chars minimal length is recommended). But you should use long and complex password to avoid password cracking attacks.parameter
salt
– securely-generated random bytes (64 bits minimum, 128 bits recommended)parameter
derived-key-length(dklen)
- how many bytes to generate as output, e.g. 32 bytes (256 bits)The source states: