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
}