126 lines
5.8 KiB
C#
126 lines
5.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Swan.Cryptography {
|
|
/// <summary>
|
|
/// Use this class to compute a hash in MD4, SHA1, SHA256 or SHA512.
|
|
/// </summary>
|
|
public static class Hasher {
|
|
private static readonly Lazy<MD5> Md5Hasher = new Lazy<MD5>(MD5.Create, true);
|
|
private static readonly Lazy<SHA1> SHA1Hasher = new Lazy<SHA1>(SHA1.Create, true);
|
|
private static readonly Lazy<SHA256> SHA256Hasher = new Lazy<SHA256>(SHA256.Create, true);
|
|
private static readonly Lazy<SHA512> SHA512Hasher = new Lazy<SHA512>(SHA512.Create, true);
|
|
|
|
/// <summary>
|
|
/// Computes the MD5 hash of the given stream.
|
|
/// Do not use for large streams as this reads ALL bytes at once.
|
|
/// </summary>
|
|
/// <param name="this">The stream.</param>
|
|
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
|
|
/// <returns>
|
|
/// The computed hash code.
|
|
/// </returns>
|
|
/// <exception cref="ArgumentNullException">stream.</exception>
|
|
[Obsolete("Use a better hasher.")]
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Stil", "IDE0045:In bedingten Ausdruck konvertieren", Justification = "<Ausstehend>")]
|
|
public static Byte[] ComputeMD5(Stream @this, Boolean createHasher = false) {
|
|
if(@this == null) {
|
|
throw new ArgumentNullException(nameof(@this));
|
|
}
|
|
|
|
MD5 md5 = MD5.Create();
|
|
const Int32 bufferSize = 4096;
|
|
|
|
Byte[] readAheadBuffer = new Byte[bufferSize];
|
|
Int32 readAheadBytesRead = @this.Read(readAheadBuffer, 0, readAheadBuffer.Length);
|
|
|
|
do {
|
|
Int32 bytesRead = readAheadBytesRead;
|
|
Byte[] buffer = readAheadBuffer;
|
|
|
|
readAheadBuffer = new Byte[bufferSize];
|
|
readAheadBytesRead = @this.Read(readAheadBuffer, 0, readAheadBuffer.Length);
|
|
|
|
if(readAheadBytesRead == 0) {
|
|
_ = md5.TransformFinalBlock(buffer, 0, bytesRead);
|
|
} else {
|
|
_ = md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);
|
|
}
|
|
}
|
|
while(readAheadBytesRead != 0);
|
|
|
|
return md5.Hash;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes the MD5 hash of the given string using UTF8 byte encoding.
|
|
/// </summary>
|
|
/// <param name="value">The input string.</param>
|
|
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
|
|
/// <returns>The computed hash code.</returns>
|
|
[Obsolete("Use a better hasher.")]
|
|
public static Byte[] ComputeMD5(String value, Boolean createHasher = false) => ComputeMD5(Encoding.UTF8.GetBytes(value), createHasher);
|
|
|
|
/// <summary>
|
|
/// Computes the MD5 hash of the given byte array.
|
|
/// </summary>
|
|
/// <param name="data">The data.</param>
|
|
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
|
|
/// <returns>The computed hash code.</returns>
|
|
[Obsolete("Use a better hasher.")]
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
|
|
public static Byte[] ComputeMD5(Byte[] data, Boolean createHasher = false) => (createHasher ? MD5.Create() : Md5Hasher.Value).ComputeHash(data);
|
|
|
|
/// <summary>
|
|
/// Computes the SHA-1 hash of the given string using UTF8 byte encoding.
|
|
/// </summary>
|
|
/// <param name="this">The input string.</param>
|
|
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
|
|
/// <returns>
|
|
/// The computes a Hash-based Message Authentication Code (HMAC)
|
|
/// using the SHA1 hash function.
|
|
/// </returns>
|
|
[Obsolete("Use a better hasher.")]
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
|
|
public static Byte[] ComputeSha1(String @this, Boolean createHasher = false) {
|
|
Byte[] inputBytes = Encoding.UTF8.GetBytes(@this);
|
|
return (createHasher ? SHA1.Create() : SHA1Hasher.Value).ComputeHash(inputBytes);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Computes the SHA-256 hash of the given string using UTF8 byte encoding.
|
|
/// </summary>
|
|
/// <param name="value">The input string.</param>
|
|
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
|
|
/// <returns>
|
|
/// The computes a Hash-based Message Authentication Code (HMAC)
|
|
/// by using the SHA256 hash function.
|
|
/// </returns>
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
|
|
public static Byte[] ComputeSha256(String value, Boolean createHasher = false) {
|
|
Byte[] inputBytes = Encoding.UTF8.GetBytes(value);
|
|
return (createHasher ? SHA256.Create() : SHA256Hasher.Value).ComputeHash(inputBytes);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Computes the SHA-512 hash of the given string using UTF8 byte encoding.
|
|
/// </summary>
|
|
/// <param name="value">The input string.</param>
|
|
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
|
|
/// <returns>
|
|
/// The computes a Hash-based Message Authentication Code (HMAC)
|
|
/// using the SHA512 hash function.
|
|
/// </returns>
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
|
|
public static Byte[] ComputeSha512(String value, Boolean createHasher = false) {
|
|
Byte[] inputBytes = Encoding.UTF8.GetBytes(value);
|
|
return (createHasher ? SHA512.Create() : SHA512Hasher.Value).ComputeHash(inputBytes);
|
|
}
|
|
}
|
|
}
|