34 lines
860 B
Plaintext

== How to fix it in Cryptodome
=== Code examples
include::../../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,python,diff-id=3,diff-type=noncompliant]
----
from Crypto.Protocol.KDF import scrypt
digest = scrypt(password, salt=b"F3MdWpeHeeSjlUxvKBnzzA", key_len=32, N=2**17, r=8, p=1) # Noncompliant
----
==== Compliant solution
[source,python,diff-id=3,diff-type=compliant]
----
import secrets
from Crypto.Protocol.KDF import scrypt
salt = secrets.token_bytes(32)
digest = scrypt(password, salt=salt, key_len=32, N=2**17, r=8, p=1)
----
=== How does this work?
include::../../common/fix/salt.adoc[]
Here, the compliant code example ensures the salt is random and has a sufficient
length by calling the `secrets.token_bytes` function. This function internally
uses a cryptographically secure pseudo-random number generator.