Skip to content

Commit

Permalink
v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-techkid committed Nov 30, 2024
1 parent 1350fde commit e336d62
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/HashVerifierBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ public abstract class HashVerifierBase<T> : IHashVerifierAsync<T>

/// <summary>
/// The method used for comparing the actual hash with the expected hash.
/// The default is <see cref="DefaultComparisonMethod"/>.
/// </summary>
protected virtual StringComparison ComparisonMethod => StringComparison.OrdinalIgnoreCase;
protected virtual StringComparison ComparisonMethod => DefaultComparisonMethod;

/// <summary>
/// The default string comparison method used for comparing the actual hash with the expected hash.
/// </summary>
public const StringComparison DefaultComparisonMethod = StringComparison.OrdinalIgnoreCase;

public bool VerifyHash(T data, string expectedHash, IHashingAlgorithm<T> algorithm)
{
Expand Down
29 changes: 29 additions & 0 deletions src/HashVerifierGeneric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// HashingHandler by Simon Field

using System;

namespace HashingHandler;

/// <summary>
/// Verifies hashes of objects of type <typeparamref name="T"/> using a given hashing provider.
/// </summary>
/// <typeparam name="T">The type of objects to verify the hashes of.</typeparam>
public class HashVerifierGeneric<T> : HashVerifierBase<T>
{
private StringComparison UserComparisonMethod { get; }

/// <summary>
/// Creates a new instance of <see cref="HashVerifierGeneric{T}"/> with the given hashing provider.
/// The default string comparison method will be used, <see cref="StringComparison.OrdinalIgnoreCase"/>.
/// </summary>
/// <param name="provider">The provider for converting this data type to byte array.</param>
public HashVerifierGeneric(IHashingProvider<T> provider, StringComparison method = DefaultComparisonMethod)
{
HashProvider = provider;
UserComparisonMethod = method;
}

protected override IHashingProvider<T> HashProvider { get; }

protected override StringComparison ComparisonMethod => UserComparisonMethod;
}
16 changes: 10 additions & 6 deletions src/HashingAlgorithmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ public abstract class HashingAlgorithmBase<T> : IHashingAlgorithmAsync<T>
/// <returns>A <see cref="Task"/> representing the hash computation that returns a hash of type <see cref="byte"/>[] given the data <paramref name="bytes"/>.</returns>
protected virtual Task<byte[]> ComputeHashAsync(byte[] bytes, CancellationToken cancellationToken = default) => Task.Run(() => ComputeHash(bytes), cancellationToken);

public string ComputeHash(T data, IHashingProvider<T> provider)
public string ComputeHash(T data, IHashingProvider<T> provider, IStringEncoding? encoding = null)
{
encoding ??= new StringExtensions();

byte[] bytes = provider.ConvertToBytes(data);
byte[] hashBytes = ComputeHash(bytes);
return StringExtensions.ByteToHex(hashBytes);
return encoding.ConvertToString(hashBytes);
}

public Task<string> ComputeHashAsync(T data, IHashingProvider<T> provider, CancellationToken cancellationToken = default)
public Task<string> ComputeHashAsync(T data, IHashingProvider<T> provider, CancellationToken cancellationToken = default, IStringEncoding? encoding = null)
{
return Task.Run(() => AsyncHashComputation(data, provider, cancellationToken), cancellationToken);
return Task.Run(() => AsyncHashComputation(data, provider, cancellationToken, encoding), cancellationToken);
}

private async Task<string> AsyncHashComputation(T data, IHashingProvider<T> provider, CancellationToken cancellationToken)
private async Task<string> AsyncHashComputation(T data, IHashingProvider<T> provider, CancellationToken cancellationToken, IStringEncoding? encoding)
{
encoding ??= new StringExtensions();

byte[] bytes;

if (provider is IHashingProviderAsync<T> asyncProvider)
Expand All @@ -52,6 +56,6 @@ private async Task<string> AsyncHashComputation(T data, IHashingProvider<T> prov
}

byte[] hashBytes = await ComputeHashAsync(bytes, cancellationToken);
return StringExtensions.ByteToHex(hashBytes);
return encoding.ConvertToString(hashBytes);
}
}
4 changes: 3 additions & 1 deletion src/IHashingAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public interface IHashingAlgorithm<T>
{
/// <summary>
/// Compute a checksum (<see cref="string"/>) hash for the given data of type <typeparamref name="T"/>.
/// If the provided <paramref name="encoding"/> is null, the hash will be given in hexadecimal format.
/// </summary>
/// <param name="data">An object of type <typeparamref name="T"/> to be hashed.</param>
/// <param name="provider">An <see cref="IHashingProvider{T}"/> that provides access to data of type <typeparamref name="T"/> as a <see cref="byte"/>[].</param>
/// <param name="encoding">An <see cref="IStringEncoding"/> representing the conversion method of the hash from <see cref="byte"/>[] to <see cref="string"/>. Default: Hexadecimal string.</param>
/// <returns>A <see cref="string"/> representation of <paramref name="data"/>.</returns>
public abstract string ComputeHash(T data, IHashingProvider<T> provider);
public string ComputeHash(T data, IHashingProvider<T> provider, IStringEncoding? encoding = null);
}
4 changes: 3 additions & 1 deletion src/IHashingAlgorithmAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public interface IHashingAlgorithmAsync<T> : IHashingAlgorithm<T>
{
/// <summary>
/// Get a <see cref="Task"/> representing the hash computation of data <paramref name="data"/> of type <typeparamref name="T"/>.
/// If the provided <paramref name="encoding"/> is null, the hash will be given in hexadecimal format.
/// </summary>
/// <param name="data">The data to hash.</param>
/// <param name="provider">The data interpreter.</param>
/// <param name="cancellationToken">A cancellation token allowing the canceling of asynchronous jobs.</param>
/// <param name="encoding">An <see cref="IStringEncoding"/> representing the conversion method of the hash from <see cref="byte"/>[] to <see cref="string"/>. Default: Hexadecimal string.</param>
/// <returns>A <see cref="Task"/> including the hash computation job.</returns>
public Task<string> ComputeHashAsync(T data, IHashingProvider<T> provider, CancellationToken cancellationToken = default);
public Task<string> ComputeHashAsync(T data, IHashingProvider<T> provider, CancellationToken cancellationToken = default, IStringEncoding? encoding = null);
}
13 changes: 13 additions & 0 deletions src/IStringEncoding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// HashingHandler by Simon Field

namespace HashingHandler;

public interface IStringEncoding
{
/// <summary>
/// Converts a <see cref="byte"/>[] to a <see cref="string"/>.
/// </summary>
/// <param name="bytes">The <see cref="byte"/>[] to convert.</param>
/// <returns>A <see cref="string"/> representing the <see cref="byte"/>[] <paramref name="bytes"/>.</returns>
public string ConvertToString(byte[] bytes);
}
9 changes: 2 additions & 7 deletions src/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ namespace HashingHandler;
/// <summary>
/// A class containing methods for manipulating <see cref="string"/> objects.
/// </summary>
internal static class StringExtensions
internal class StringExtensions : IStringEncoding
{
/// <summary>
/// Convert a <see cref="byte"/>[] to a <see cref="string"/> of hexadecimal characters.
/// </summary>
/// <param name="data">The data to be converted.</param>
/// <returns>A <see cref="string"/> of hexadecimals representing the given <paramref name="data"/>.</returns>
public static string ByteToHex(byte[] data)
public string ConvertToString(byte[] data)
{
return BitConverter.ToString(data).Replace("-", "");
}
Expand Down

0 comments on commit e336d62

Please sign in to comment.