Skip to content

Commit

Permalink
Started adding some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-matt committed May 30, 2022
1 parent 3e99016 commit 3b5cd34
Show file tree
Hide file tree
Showing 16 changed files with 1,130 additions and 515 deletions.
129 changes: 70 additions & 59 deletions Extenso.Core/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,67 +46,78 @@ public static T BinaryDeserialize<T>(this byte[] data)
}
}

/// <summary>
/// Encrypts data with the System.Security.Cryptography.RSA algorithm.
/// </summary>
/// <param name="source">The data to be encrypted.</param>
/// <param name="parameters">The parameters for System.Security.Cryptography.RSA.</param>
/// <param name="fOAEP">
/// true to perform direct System.Security.Cryptography.RSA encryption using OAEP
/// padding (only available on a computer running Windows XP or later); otherwise,
/// false to use PKCS#1 v1.5 padding.
/// </param>
/// <returns>The encrypted data.</returns>
public static byte[] RSAEncrypt(this byte[] source, RSAParameters parameters, bool fOAEP)
{
using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider())
{
rsaCryptoServiceProvider.ImportParameters(parameters);
return rsaCryptoServiceProvider.Encrypt(source, fOAEP);
}
}
//public static string Decrypt(this byte[] source, Encoding encoding, ICryptoTransform cryptoTransform)
//{
// using (var memoryStream = new MemoryStream(source))
// using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Read))
// {
// byte[] bytes = new byte[source.Length];
// cryptoStream.Read(bytes, 0, bytes.Length);
// return encoding.GetString(bytes);
// }
//}

/// <summary>
/// Decrypts data with the System.Security.Cryptography.RSA algorithm.
/// </summary>
/// <param name="source">The data to be decrypted.</param>
/// <param name="parameters">The parameters for System.Security.Cryptography.RSA.</param>
/// <param name="fOAEP">
/// true to perform direct System.Security.Cryptography.RSA decryption using OAEP
/// padding (only available on a computer running Microsoft Windows XP or later);
/// otherwise, false to use PKCS#1 v1.5 padding.
/// </param>
/// <returns>The decrypted data, which is the original plain text before encryption.</returns>
public static byte[] RSADecrypt(this byte[] source, RSAParameters parameters, bool fOAEP)
{
using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider())
{
rsaCryptoServiceProvider.ImportParameters(parameters);
return rsaCryptoServiceProvider.Decrypt(source, fOAEP);
}
}
///// <summary>
///// Encrypts data with the System.Security.Cryptography.RSA algorithm.
///// </summary>
///// <param name="source">The data to be encrypted.</param>
///// <param name="parameters">The parameters for System.Security.Cryptography.RSA.</param>
///// <param name="fOAEP">
///// true to perform direct System.Security.Cryptography.RSA encryption using OAEP
///// padding (only available on a computer running Windows XP or later); otherwise,
///// false to use PKCS#1 v1.5 padding.
///// </param>
///// <returns>The encrypted data.</returns>
//public static byte[] RSAEncrypt(this byte[] source, RSAParameters parameters, bool fOAEP)
//{
// using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider())
// {
// rsaCryptoServiceProvider.ImportParameters(parameters);
// return rsaCryptoServiceProvider.Encrypt(source, fOAEP);
// }
//}

/// <summary>
/// Decrypts the specified byte array using the TripleDES symmetric algorithm and returns the original string.
/// </summary>
/// <param name="source">The data to be decrypted.</param>
/// <param name="encoding">The System.Text.Encoding to use.</param>
/// <param name="key">The secret key to use for the symmetric algorithm.</param>
/// <param name="initializationVector">The initialization vector to use for the symmetric algorithm.</param>
/// <returns>The decrypted data, which is the original plain text before encryption.</returns>
public static string TripleDESDecrypt(this byte[] source, Encoding encoding, byte[] key, byte[] initializationVector)
{
using (var memoryStream = new MemoryStream(source))
using (var cryptoStream = new CryptoStream(
memoryStream,
new TripleDESCryptoServiceProvider().CreateDecryptor(key, initializationVector),
CryptoStreamMode.Read))
{
byte[] bytes = new byte[source.Length];
cryptoStream.Read(bytes, 0, bytes.Length);
return encoding.GetString(bytes);
}
}
///// <summary>
///// Decrypts data with the System.Security.Cryptography.RSA algorithm.
///// </summary>
///// <param name="source">The data to be decrypted.</param>
///// <param name="parameters">The parameters for System.Security.Cryptography.RSA.</param>
///// <param name="fOAEP">
///// true to perform direct System.Security.Cryptography.RSA decryption using OAEP
///// padding (only available on a computer running Microsoft Windows XP or later);
///// otherwise, false to use PKCS#1 v1.5 padding.
///// </param>
///// <returns>The decrypted data, which is the original plain text before encryption.</returns>
//public static byte[] RSADecrypt(this byte[] source, RSAParameters parameters, bool fOAEP)
//{
// using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider())
// {
// rsaCryptoServiceProvider.ImportParameters(parameters);
// return rsaCryptoServiceProvider.Decrypt(source, fOAEP);
// }
//}

///// <summary>
///// Decrypts the specified byte array using the TripleDES symmetric algorithm and returns the original string.
///// </summary>
///// <param name="source">The data to be decrypted.</param>
///// <param name="encoding">The System.Text.Encoding to use.</param>
///// <param name="key">The secret key to use for the symmetric algorithm.</param>
///// <param name="initializationVector">The initialization vector to use for the symmetric algorithm.</param>
///// <returns>The decrypted data, which is the original plain text before encryption.</returns>
//public static string TripleDESDecrypt(this byte[] source, Encoding encoding, byte[] key, byte[] initializationVector)
//{
// using (var memoryStream = new MemoryStream(source))
// using (var cryptoStream = new CryptoStream(
// memoryStream,
// TripleDES.Create().CreateDecryptor(key, initializationVector),
// CryptoStreamMode.Read))
// {
// byte[] bytes = new byte[source.Length];
// cryptoStream.Read(bytes, 0, bytes.Length);
// return encoding.GetString(bytes);
// }
//}

/// <summary>
/// Creates a new non-resizable instance of the System.IO.MemoryStream class based on the specified byte array.
Expand Down
10 changes: 0 additions & 10 deletions Extenso.Core/CharExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
/// </summary>
public static class CharExtensions
{
/// <summary>
/// Gets a value indicating whether the specified Unicode character is a letter (uppercase or lowercase).
/// </summary>
/// <param name="c">The Unicode character to examine.</param>
/// <returns>true if the character is a letter; otherwise false.</returns>
public static bool IsLetter(this char c)
{
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}

/// <summary>
/// Initializes a new instance of the System.String class to the value indicated by a specified Unicode character repeated a specified number of times.
/// </summary>
Expand Down
12 changes: 7 additions & 5 deletions Extenso.Core/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public static bool IsNullOrDefault(this DateTime? source)
/// <returns>A System.DateTime equivalent for unixTimestamp</returns>
public static DateTime ParseUnixTimestamp(int unixTimestamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0, 0)
.AddSeconds(unixTimestamp)
.ToLocalTime();
return DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).DateTime;

//return new DateTime(1970, 1, 1, 0, 0, 0, 0)
// .AddSeconds(unixTimestamp)
// .ToLocalTime();
}

/// <summary>
Expand Down Expand Up @@ -96,9 +98,9 @@ public static string ToISO8601DateString(this DateTime source)
/// </summary>
/// <param name="source">The System.DateTime to convert to its equivalent Unix Timestamp.</param>
/// <returns>A System.Int32 whose value represents the Unix Timestamp equivalent of the given System.DateTime.</returns>
public static int ToUnixTimestamp(this DateTime source)
public static long ToUnixTimestamp(this DateTime source)
{
return (int)(source - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds * 1000;
return new DateTimeOffset(source).ToUnixTimeSeconds();
}
}
}
Loading

0 comments on commit 3b5cd34

Please sign in to comment.