Created
May 29, 2019 18:01
-
-
Save michel-pi/02fb3b785a37b71f823388be6757a768 to your computer and use it in GitHub Desktop.
Provides static methods to use hash algorithms in a generic way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.Runtime.InteropServices; | |
namespace System.Security.Cryptography.Provider | |
{ | |
/// <summary> | |
/// Provides static methods to use hash algorithms in a generic way. | |
/// </summary> | |
public static class HashProvider | |
{ | |
private static byte[] StringToBytes(string value) | |
{ | |
return Encoding.Unicode.GetBytes(value); | |
} | |
private static byte[] StructToBytes<T>(T value) where T : struct | |
{ | |
GCHandle handle = default; | |
byte[] bytes = new byte[Marshal.SizeOf<T>()]; | |
try | |
{ | |
handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); | |
Marshal.StructureToPtr<T>(value, handle.AddrOfPinnedObject(), false); | |
} | |
finally | |
{ | |
if (handle.IsAllocated) handle.Free(); | |
} | |
return bytes; | |
} | |
private static string ToHexString(byte[] bytes) | |
{ | |
return BitConverter.ToString(bytes).Replace("-", ""); | |
} | |
/// <summary> | |
/// Computes the hash value for the specified byte array using the supplied hash algorithm. | |
/// </summary> | |
/// <typeparam name="THashAlgorithm">A class that implements <see cref="T:System.Security.Cryptography.HashAlgorithm">.</typeparam> | |
/// <param name="value">The input to compute the hash code for.</param> | |
/// <returns>The computed hash code.</returns> | |
public static byte[] ComputeHash<THashAlgorithm>(byte[] value) where THashAlgorithm : HashAlgorithm, new() | |
{ | |
if (value == null) throw new ArgumentNullException(nameof(value)); | |
if (value.Length == 0) throw new ArgumentException(); | |
using (var algo = new THashAlgorithm()) | |
{ | |
return algo.ComputeHash(value); | |
} | |
} | |
/// <summary> | |
/// Computes the hexadecimal hash string for the specified struct using the supplied hash algorithm. | |
/// </summary> | |
/// <typeparam name="THashAlgorithm">A class that implements <see cref="T:System.Security.Cryptography.HashAlgorithm">.</typeparam> | |
/// <typeparam name="TValue">A value type.</typeparam> | |
/// <param name="value">The input to compute the hash code for.</param> | |
/// <returns>The computed hash code.</returns> | |
public static byte[] ComputeHash<THashAlgorithm, TValue>(TValue value) | |
where THashAlgorithm : HashAlgorithm, new() | |
where TValue : struct | |
{ | |
return ComputeHash<THashAlgorithm>(StructToBytes<TValue>(value)); | |
} | |
/// <summary> | |
/// Computes the hash value for the specified string using the supplied hash algorithm. | |
/// </summary> | |
/// <typeparam name="THashAlgorithm">A class that implements <see cref="T:System.Security.Cryptography.HashAlgorithm">.</typeparam> | |
/// <param name="value">The input to compute the hash code for.</param> | |
/// <returns>The computed hash code as a hexadecimal string value.</returns> | |
public static string ComputeHashString<THashAlgorithm>(string value) where THashAlgorithm : HashAlgorithm, new() | |
{ | |
if (value == null) throw new ArgumentNullException(nameof(value)); | |
if (value.Length == 0) throw new ArgumentException(); | |
return ToHexString(ComputeHash<THashAlgorithm>(StringToBytes(value))); | |
} | |
/// <summary> | |
/// Computes the hexadecimal hash string for the specified struct using the supplied hash algorithm. | |
/// </summary> | |
/// <typeparam name="THashAlgorithm">A class that implements <see cref="T:System.Security.Cryptography.HashAlgorithm">.</typeparam> | |
/// <typeparam name="TValue">A value type.</typeparam> | |
/// <param name="value">The input to compute the hash code for.</param> | |
/// <returns>The computed hash code as a hexadecimal string value.</returns> | |
public static string ComputeHashString<THashAlgorithm, TValue>(TValue value) | |
where THashAlgorithm : HashAlgorithm, new() | |
where TValue : struct | |
{ | |
return ToHexString(ComputeHash<THashAlgorithm, TValue>(value)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment