1
0

Weitere UnitTests

This commit is contained in:
2021-11-19 23:10:35 +01:00
parent 0c3825587f
commit 0736d54c87
7 changed files with 691 additions and 51 deletions

View File

@@ -9,7 +9,7 @@ namespace System.Security.Cryptography
/// </summary>
public class CryptographyHelper
{
private static readonly int saltLength = 8;
private static int saltLength = 8;
private readonly string masterKeyFile;
@@ -206,6 +206,19 @@ namespace System.Security.Cryptography
return ms.ToArray();
}
/// <summary>
/// Decrypts a Base64 string using the AES algorithm and a password to an UTF-8 string.
/// </summary>
/// <param name="cipherStr">The encrypted Base64 encoded string.</param>
/// <param name="password">The password to use for decryption.</param>
/// <returns>The decrypted UTF-8 string string.</returns>
public static string AesDecrypt(string cipherStr, string password)
{
byte[] cipher = Convert.FromBase64String(cipherStr);
byte[] plain = AesDecrypt(cipher, password);
return Encoding.UTF8.GetString(plain);
}
/// <summary>
/// Encrypts data using the AES algorithm and a password.
/// </summary>
@@ -234,19 +247,6 @@ namespace System.Security.Cryptography
return ms.ToArray();
}
/// <summary>
/// Decrypts a Base64 string using the AES algorithm and a password to an UTF-8 string.
/// </summary>
/// <param name="cipherStr">The encrypted Base64 encoded string.</param>
/// <param name="password">The password to use for decryption.</param>
/// <returns>The decrypted UTF-8 string string.</returns>
public static string AesDecrypt(string cipherStr, string password)
{
byte[] cipher = Convert.FromBase64String(cipherStr);
byte[] plain = AesDecrypt(cipher, password);
return Encoding.UTF8.GetString(plain);
}
/// <summary>
/// Encrypts an UTF-8 string using the AES algorithm and a password to a Base64 string.
/// </summary>
@@ -543,29 +543,7 @@ namespace System.Security.Cryptography
#endregion Random
#region Probing security
/// <summary>
/// Determines whether two strings are equal in constant time. This method does not stop
/// early if a difference was detected, unless the length differs.
/// </summary>
/// <param name="a">The first string.</param>
/// <param name="b">The second string.</param>
/// <returns>true, if both strings are equal; otherwise, false.</returns>
public static bool SecureEquals(string a, string b)
{
if ((a == null) != (b == null))
return false;
if (a.Length != b.Length)
return false;
int differentBits = 0;
for (int i = 0; i < a.Length; i++)
{
differentBits |= a[i] ^ b[i];
}
return differentBits == 0;
}
#region Secure probing
/// <summary>
/// Determines whether two byte arrays are equal in constant time. This method does not stop
@@ -589,7 +567,29 @@ namespace System.Security.Cryptography
return differentBits == 0;
}
#endregion Probing security
/// <summary>
/// Determines whether two strings are equal in constant time. This method does not stop
/// early if a difference was detected, unless the length differs.
/// </summary>
/// <param name="a">The first string.</param>
/// <param name="b">The second string.</param>
/// <returns>true, if both strings are equal; otherwise, false.</returns>
public static bool SecureEquals(string a, string b)
{
if ((a == null) != (b == null))
return false;
if (a.Length != b.Length)
return false;
int differentBits = 0;
for (int i = 0; i < a.Length; i++)
{
differentBits |= a[i] ^ b[i];
}
return differentBits == 0;
}
#endregion Secure probing
#endregion Static methods
}

View File

@@ -22,7 +22,7 @@ namespace AMWD.Common.Utilities
/// <summary>
/// The synchronisation object.
/// </summary>
protected readonly object syncObj = new();
protected readonly object syncLock = new();
/// <summary>
/// The exception handler.
@@ -122,7 +122,7 @@ namespace AMWD.Common.Utilities
/// </summary>
public void Reset()
{
lock (syncObj)
lock (syncLock)
{
if (!IsWaitingToRun && !IsRunning)
{
@@ -149,7 +149,7 @@ namespace AMWD.Common.Utilities
public void Cancel()
{
TaskCompletionSourceWrapper localTcs = null;
lock (syncObj)
lock (syncLock)
{
IsWaitingToRun = false;
nextRunPending = false;
@@ -172,7 +172,7 @@ namespace AMWD.Common.Utilities
/// <returns><c>true</c>, if an execution was started; otherwise, <c>false</c>.</returns>
public bool ExecutePending()
{
lock (syncObj)
lock (syncLock)
{
if (!IsWaitingToRun && !IsRunning)
{
@@ -197,7 +197,7 @@ namespace AMWD.Common.Utilities
/// <returns>An awaiter instance.</returns>
public TaskAwaiter GetAwaiter()
{
lock (syncObj)
lock (syncLock)
{
return tcs.Task.GetAwaiter();
}
@@ -211,7 +211,7 @@ namespace AMWD.Common.Utilities
{
get
{
lock (syncObj)
lock (syncLock)
{
return tcs.Task;
}
@@ -273,7 +273,7 @@ namespace AMWD.Common.Utilities
/// <param name="state">Unused.</param>
protected void OnTimerCallback(object state)
{
lock (syncObj)
lock (syncLock)
{
if (!IsWaitingToRun)
{
@@ -304,7 +304,7 @@ namespace AMWD.Common.Utilities
catch (Exception ex)
{
exception = ex;
lock (syncObj)
lock (syncLock)
{
runAgain = false;
IsRunning = false;
@@ -320,7 +320,7 @@ namespace AMWD.Common.Utilities
}
finally
{
lock (syncObj)
lock (syncLock)
{
runAgain = nextRunPending;
IsRunning = runAgain;
@@ -484,7 +484,7 @@ namespace AMWD.Common.Utilities
/// <returns>An awaiter instance.</returns>
public new TaskAwaiter<TResult> GetAwaiter()
{
lock (syncObj)
lock (syncLock)
{
var myTcs = (TaskCompletionSourceWrapper<TResult>)tcs;
var myTask = (Task<TResult>)myTcs.Task;
@@ -499,7 +499,7 @@ namespace AMWD.Common.Utilities
{
get
{
lock (syncObj)
lock (syncLock)
{
var myTcs = (TaskCompletionSourceWrapper<TResult>)tcs;
var myTask = (Task<TResult>)myTcs.Task;

View File

@@ -10,6 +10,7 @@ namespace AMWD.Common.Utilities
/// <summary>
/// Provides some network utils.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static class NetworkHelper
{
/// <summary>
@@ -49,7 +50,6 @@ namespace AMWD.Common.Utilities
/// <param name="interfaceName">The interface name to resolve.</param>
/// <param name="addressFamily">An address family to use (available: <see cref="AddressFamily.InterNetwork"/> and <see cref="AddressFamily.InterNetworkV6"/>).</param>
/// <returns>The resolved <see cref="IPAddress"/>es or an empty list.</returns>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] // not possible to define interfaces on unit tests
public static List<IPAddress> ResolveInterface(string interfaceName, AddressFamily addressFamily = default)
{
if (string.IsNullOrWhiteSpace(interfaceName))