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

@@ -94,7 +94,7 @@ namespace System.Collections.Generic
/// Determines whether an element is in the <see cref="AsyncQueue{T}"/>.
/// </summary>
/// <param name="item">The object to locate in the <see cref="AsyncQueue{T}"/>. The value can be null for reference types.</param>
/// <returns>true if item is found in the <see cref="AsyncQueue{T}"/>; otherwise, false.</returns>
/// <returns><see langword="true"/> if item is found in the <see cref="AsyncQueue{T}"/>, otherwise <see langword="false"/>.</returns>
[ExcludeFromCodeCoverage]
public bool Contains(T item)
{
@@ -172,7 +172,7 @@ namespace System.Collections.Generic
{
lock (_queue)
{
return _queue.ToArray();
return [.. _queue];
}
}
@@ -304,7 +304,7 @@ namespace System.Collections.Generic
/// Removes the object at the beginning of the <see cref="AsyncQueue{T}"/>, and copies it to the <paramref name="result"/> parameter.
/// </summary>
/// <param name="result">The removed object.</param>
/// <returns>true if the object is successfully removed; false if the <see cref="AsyncQueue{T}"/> is empty.</returns>
/// <returns><see langword="true"/> if the object is successfully removed, <see langword="false"/> if the <see cref="AsyncQueue{T}"/> is empty.</returns>
public bool TryDequeue(out T result)
{
try
@@ -325,7 +325,7 @@ namespace System.Collections.Generic
/// <paramref name="result"/> parameter. The object is not removed from the <see cref="AsyncQueue{T}"/>.
/// </summary>
/// <param name="result">If present, the object at the beginning of the <see cref="AsyncQueue{T}"/>; otherwise, the default value of <typeparamref name="T"/>.</param>
/// <returns>true if there is an object at the beginning of the <see cref="AsyncQueue{T}"/>; false if the <see cref="AsyncQueue{T}"/> is empty.</returns>
/// <returns><see langword="true"/> if there is an object at the beginning of the <see cref="AsyncQueue{T}"/>, <see langword="false"/> if the <see cref="AsyncQueue{T}"/> is empty.</returns>
public bool TryPeek(out T result)
{
try
@@ -344,7 +344,7 @@ namespace System.Collections.Generic
/// Removes the first occurrence of a specific object from the <see cref="AsyncQueue{T}"/>.
/// </summary>
/// <param name="item">The object to remove from the <see cref="AsyncQueue{T}"/>. The value can be null for reference types.</param>
/// <returns>true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the <see cref="AsyncQueue{T}"/>.</returns>
/// <returns><see langword="true"/> if item is successfully removed, otherwise <see langword="false"/>. This method also returns <see langword="false"/> if item was not found in the <see cref="AsyncQueue{T}"/>.</returns>
public bool Remove(T item)
{
lock (_queue)

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

View File

@@ -4,7 +4,12 @@ using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
#if NET8_0_OR_GREATER
using IPNetwork = System.Net.IPNetwork;
#else
using Microsoft.AspNetCore.HttpOverrides;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
#endif
namespace AMWD.Common.Utilities
{
@@ -23,7 +28,7 @@ namespace AMWD.Common.Utilities
public static List<IPAddress> ResolveHost(string hostname, AddressFamily addressFamily = default)
{
if (string.IsNullOrWhiteSpace(hostname))
return new();
return [];
if (addressFamily != AddressFamily.InterNetwork && addressFamily != AddressFamily.InterNetworkV6)
addressFamily = AddressFamily.Unspecified;
@@ -31,7 +36,7 @@ namespace AMWD.Common.Utilities
var ipAddress = ResolveIpAddress(hostname, addressFamily);
// the name was an ip address, should not happen but experience tells other stories
if (ipAddress != null)
return new() { ipAddress };
return [ipAddress];
try
{
@@ -41,7 +46,7 @@ namespace AMWD.Common.Utilities
}
catch
{
return new();
return [];
}
}
@@ -54,7 +59,7 @@ namespace AMWD.Common.Utilities
public static List<IPAddress> ResolveInterface(string interfaceName, AddressFamily addressFamily = default)
{
if (string.IsNullOrWhiteSpace(interfaceName))
return new();
return [];
if (addressFamily != AddressFamily.InterNetwork && addressFamily != AddressFamily.InterNetworkV6)
addressFamily = AddressFamily.Unspecified;
@@ -62,7 +67,7 @@ namespace AMWD.Common.Utilities
var ipAddress = ResolveIpAddress(interfaceName, addressFamily);
// the name was an ip address, should not happen but experience tells other stories
if (ipAddress != null)
return new() { ipAddress };
return [ipAddress];
try
{
@@ -74,45 +79,7 @@ namespace AMWD.Common.Utilities
}
catch
{
return new();
}
}
/// <summary>
/// Parses a CIDR network definition.
/// </summary>
/// <param name="network">The network in CIDR.</param>
/// <returns>The <see cref="IPNetwork"/> or <c>null</c>.</returns>
public static IPNetwork ParseNetwork(string network)
{
TryParseNetwork(network, out var ipNetwork);
return ipNetwork;
}
/// <summary>
/// Tries to parse a CIDR network definition.
/// </summary>
/// <param name="network">The network in CIDR.</param>
/// <param name="ipNetwork">The parsed <see cref="IPNetwork"/>.</param>
/// <returns><c>true</c> on success, otherwise <c>false</c>.</returns>
public static bool TryParseNetwork(string network, out IPNetwork ipNetwork)
{
try
{
string[] parts = network.Split('/');
if (parts.Length != 2)
throw new ArgumentException($"Invalid network type");
var prefix = IPAddress.Parse(parts.First());
int prefixLength = int.Parse(parts.Last());
ipNetwork = new IPNetwork(prefix, prefixLength);
return true;
}
catch
{
ipNetwork = null;
return false;
return [];
}
}
@@ -128,7 +95,11 @@ namespace AMWD.Common.Utilities
{
var list = new List<IPAddress>();
#if NET8_0_OR_GREATER
var ipAddress = network.BaseAddress;
#else
var ipAddress = network.Prefix;
#endif
while (network.Contains(ipAddress))
{
list.Add(ipAddress);