rspec/rules/S3329/csharp/rule.adoc

51 lines
1.2 KiB
Plaintext
Raw Normal View History

include::../description.adoc[]
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,csharp]
----
public void Encrypt(byte[] key, byte[] data, MemoryStream target)
{
byte[] initializationVector = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
using var aes = new AesCryptoServiceProvider();
var encryptor = aes.CreateEncryptor(key, initializationVector); // Noncompliant, hardcoded value is used
using var cryptoStream = new CryptoStream(target, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(data);
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,csharp]
----
public byte[] Encrypt(byte[] key, byte[] data, MemoryStream target)
{
using var aes = new AesCryptoServiceProvider();
var encryptor = aes.CreateEncryptor(key, aes.IV); // aes.IV is automatically generated to random secure value
using var cryptoStream = new CryptoStream(target, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(data);
return aes.IV;
}
----
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[]