1
0

Updated to C# 12

This commit is contained in:
2024-01-14 13:10:33 +01:00
parent 9cd1344266
commit 27cd54fb30
51 changed files with 637 additions and 379 deletions

View File

@@ -171,6 +171,7 @@ namespace System.Security.Cryptography
#region Static methods
#region Encryption
#pragma warning disable SYSLIB0041
#region AES
@@ -185,7 +186,11 @@ namespace System.Security.Cryptography
byte[] salt = new byte[_saltLength];
Array.Copy(cipher, salt, _saltLength);
using var gen = new Rfc2898DeriveBytes(password, salt);
#if NET8_0_OR_GREATER
using var gen = new Rfc2898DeriveBytes(password, salt, 1000, HashAlgorithmName.SHA1);
#else
using var gen = new Rfc2898DeriveBytes(password, salt, 1000);
#endif
using var aes = Aes.Create();
aes.Mode = CipherMode.CBC;
@@ -225,7 +230,11 @@ namespace System.Security.Cryptography
{
byte[] salt = GetRandomBytes(_saltLength);
using var gen = new Rfc2898DeriveBytes(password, salt);
#if NET8_0_OR_GREATER
using var gen = new Rfc2898DeriveBytes(password, salt, 1000, HashAlgorithmName.SHA1);
#else
using var gen = new Rfc2898DeriveBytes(password, salt, 1000);
#endif
using var aes = Aes.Create();
aes.Mode = CipherMode.CBC;
@@ -271,7 +280,11 @@ namespace System.Security.Cryptography
byte[] salt = new byte[_saltLength];
Array.Copy(cipher, salt, _saltLength);
using var gen = new Rfc2898DeriveBytes(password, salt);
#if NET8_0_OR_GREATER
using var gen = new Rfc2898DeriveBytes(password, salt, 1000, HashAlgorithmName.SHA1);
#else
using var gen = new Rfc2898DeriveBytes(password, salt, 1000);
#endif
using var tdes = TripleDES.Create();
tdes.Mode = CipherMode.CBC;
@@ -298,7 +311,11 @@ namespace System.Security.Cryptography
{
byte[] salt = GetRandomBytes(_saltLength);
using var gen = new Rfc2898DeriveBytes(password, salt);
#if NET8_0_OR_GREATER
using var gen = new Rfc2898DeriveBytes(password, salt, 1000, HashAlgorithmName.SHA1);
#else
using var gen = new Rfc2898DeriveBytes(password, salt, 1000);
#endif
using var tdes = TripleDES.Create();
tdes.Mode = CipherMode.CBC;
@@ -344,6 +361,7 @@ namespace System.Security.Cryptography
#endregion Triple DES
#pragma warning restore SYSLIB0041
#endregion Encryption
#region Hashing
@@ -379,8 +397,12 @@ namespace System.Security.Cryptography
/// <returns>The MD5 hash value, in hexadecimal notation.</returns>
public static string Md5(byte[] bytes)
{
#if NET8_0_OR_GREATER
return MD5.HashData(bytes).BytesToHex();
#else
using var md5 = MD5.Create();
return md5.ComputeHash(bytes).BytesToHex();
#endif
}
#endregion MD5
@@ -416,8 +438,12 @@ namespace System.Security.Cryptography
/// <returns>The SHA-1 hash value, in hexadecimal notation.</returns>
public static string Sha1(byte[] bytes)
{
#if NET8_0_OR_GREATER
return SHA1.HashData(bytes).BytesToHex();
#else
using var sha1 = SHA1.Create();
return sha1.ComputeHash(bytes).BytesToHex();
#endif
}
#endregion SHA-1
@@ -453,8 +479,12 @@ namespace System.Security.Cryptography
/// <returns>The SHA-256 hash value, in hexadecimal notation.</returns>
public static string Sha256(byte[] bytes)
{
#if NET8_0_OR_GREATER
return SHA256.HashData(bytes).BytesToHex();
#else
using var sha256 = SHA256.Create();
return sha256.ComputeHash(bytes).BytesToHex();
#endif
}
#endregion SHA-256
@@ -490,8 +520,12 @@ namespace System.Security.Cryptography
/// <returns>The SHA-512 hash value, in hexadecimal notation.</returns>
public static string Sha512(byte[] bytes)
{
#if NET8_0_OR_GREATER
return SHA512.HashData(bytes).BytesToHex();
#else
using var sha512 = SHA512.Create();
return sha512.ComputeHash(bytes).BytesToHex();
#endif
}
#endregion SHA-512