rspec/rules/S3329/kotlin/rule.adoc

54 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,kotlin]
----
val bytesIV = "7cVgr5cbdCZVw5WY".toByteArray(charset("UTF-8")) // Predictable / hardcoded IV
val iv = IvParameterSpec(bytesIV)
val skeySpec = SecretKeySpec(secretKey.toByteArray(), "AES")
val cipher: Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv) // Noncompliant (s3329)
val encryptedBytes: ByteArray = cipher.doFinal("foo".toByteArray())
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[source,kotlin]
----
val random: SecureRandom = SecureRandom()
val bytesIV: ByteArray = ByteArray(16)
random.nextBytes(bytesIV); // Unpredictable / random IV
val iv = IvParameterSpec(bytesIV)
val skeySpec = SecretKeySpec(secretKey.toByteArray(), "AES")
val cipher: Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv) //Compliant (s3329)
val encryptedBytes: ByteArray = cipher.doFinal("foo".toByteArray())
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]