33 lines
711 B
Plaintext
33 lines
711 B
Plaintext
![]() |
== How to fix it in Python Standard Library
|
||
|
|
||
|
=== Code examples
|
||
|
|
||
|
include::../../common/fix/code-rationale.adoc[]
|
||
|
|
||
|
==== Noncompliant code example
|
||
|
|
||
|
[source,python,diff-id=1,diff-type=noncompliant]
|
||
|
----
|
||
|
import crypt
|
||
|
|
||
|
hash = crypt.crypt(password) # Noncompliant
|
||
|
----
|
||
|
|
||
|
==== Compliant solution
|
||
|
|
||
|
[source,python,diff-id=1,diff-type=compliant]
|
||
|
----
|
||
|
import crypt
|
||
|
|
||
|
salt = crypt.mksalt(crypt.METHOD_SHA256)
|
||
|
hash = crypt.crypt(password, salt)
|
||
|
----
|
||
|
|
||
|
=== 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 `crypt.mksalt` function. This one internally uses a
|
||
|
cryptographically secure pseudo random number generator.
|