2023-06-28 17:25:56 +02:00
|
|
|
== How to fix it in pyca
|
|
|
|
|
|
|
|
=== Code examples
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,python,diff-id=1,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
from cryptography.hazmat.primitives.ciphers import (
|
|
|
|
Cipher,
|
|
|
|
algorithms,
|
|
|
|
modes,
|
|
|
|
)
|
|
|
|
|
2024-09-06 15:47:04 +02:00
|
|
|
iv = b"exampleIV1234567"
|
2023-06-28 17:25:56 +02:00
|
|
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
|
|
|
|
|
|
|
|
cipher.encryptor() # Noncompliant
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
:explicit_strong: os.urandom
|
|
|
|
|
|
|
|
include::../../common/fix/explicit-fix.adoc[]
|
|
|
|
|
|
|
|
[source,python,diff-id=1,diff-type=compliant]
|
|
|
|
----
|
|
|
|
from os import urandom
|
|
|
|
|
|
|
|
from cryptography.hazmat.primitives.ciphers import (
|
|
|
|
Cipher,
|
|
|
|
algorithms,
|
|
|
|
modes,
|
|
|
|
)
|
|
|
|
|
|
|
|
iv = urandom(16)
|
|
|
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
|
|
|
|
|
|
|
|
cipher.encryptor()
|
|
|
|
----
|
|
|
|
|
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
include::../../common/fix/fix.adoc[]
|
|
|
|
|