Skip to content

Commit

Permalink
v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-techkid committed Nov 10, 2024
1 parent 5e99afb commit 1ffe40f
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 34 deletions.
35 changes: 33 additions & 2 deletions src/HashVerifierBase.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
// HashingHandler by Simon Field

using System;
using System.Threading;
using System.Threading.Tasks;

namespace HashingHandler;

/// <summary>
/// Base class for verifying hashes of objects of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of objects being hashed.</typeparam>
public abstract class HashVerifierBase<T> : IHashChecker<T>
public abstract class HashVerifierBase<T> : IHashVerifierAsync<T>
{
/// <summary>
/// The hashing provider used to convert objects of type <typeparamref name="T"/> to <see cref="byte"/>[].
/// </summary>
protected abstract IHashingProvider<T> HashProvider { get; }

/// <summary>
/// The method used for comparing the actual hash with the expected hash.
/// </summary>
protected virtual StringComparison ComparisonMethod => StringComparison.OrdinalIgnoreCase;

public bool VerifyHash(T data, string expectedHash, IHashingAlgorithm<T> algorithm)
{
string actualHash = algorithm.ComputeHash(data, HashProvider);
return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase);
return string.Equals(actualHash, expectedHash, ComparisonMethod);
}

public Task<bool> VerifyHashAsync(T data, string expectedHash, IHashingAlgorithm<T> algorithm, CancellationToken cancellationToken = default)
{
return Task.Run(() => AsyncHashVerification(data, expectedHash, algorithm, cancellationToken), cancellationToken);
}

private async Task<bool> AsyncHashVerification(T data, string expectedHash, IHashingAlgorithm<T> algorithm, CancellationToken cancellationToken)
{
string actualHash;
bool result;

if (algorithm is IHashingAlgorithmAsync<T> asyncAlgorithm)
{
actualHash = await asyncAlgorithm.ComputeHashAsync(data, HashProvider, cancellationToken);
}
else
{
actualHash = algorithm.ComputeHash(data, HashProvider);
}

result = string.Equals(actualHash, expectedHash, ComparisonMethod);

return result;
}
}
15 changes: 12 additions & 3 deletions src/HashingAlgorithmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

namespace HashingHandler;

public abstract class HashingAlgorithmBase<T> : IHashingAlgorithm<T>, IHashingAlgorithmAsync<T>
/// <summary>
/// Base class for hashing algorithms.
/// </summary>
/// <typeparam name="T">The type of objects being hashed.</typeparam>
public abstract class HashingAlgorithmBase<T> : IHashingAlgorithmAsync<T>
{
/// <summary>
/// Compute a hash of type <see cref="byte"/>[] given the data <paramref name="bytes"/> of type <see cref="byte"/>[].
Expand All @@ -29,13 +33,18 @@ public string ComputeHash(T data, IHashingProvider<T> provider)
return StringExtensions.ByteToHex(hashBytes);
}

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

private async Task<string> AsyncHashComputation(T data, IHashingProvider<T> provider, CancellationToken cancellationToken)
{
byte[] bytes;

if (provider is IHashingProviderAsync<T> asyncProvider)
{
bytes = await asyncProvider.ConvertToBytes(data, cancellationToken);
bytes = await asyncProvider.ConvertToBytesAsync(data, cancellationToken);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/HashingHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<AssemblyVersion Condition="'$(AssemblyVersion)' == ''">$(Version)</AssemblyVersion>
<AssemblyFileVersion Condition="'$(AssemblyFileVersion)' == ''">$(Version)</AssemblyFileVersion>
<PackageVersion Condition="'$(PackageVersion)' == ''">$(Version)</PackageVersion>
<Version>1.4.0</Version>
<Version>1.5.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
19 changes: 0 additions & 19 deletions src/IHashChecker.cs

This file was deleted.

16 changes: 10 additions & 6 deletions src/IHashVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// HashingHandler by Simon Field
// HashingHandler by Simon Field

namespace HashingHandler;

/// <summary>
/// Interface for classes supporting the verification of data using hashes.
/// Unifies classes supporting the verifying of a hash of an object of type <typeparamref name="T"/> with another hash.
/// </summary>
public interface IHashVerifier
/// <typeparam name="T">The type of the object being hashed.</typeparam>
public interface IHashVerifier<T>
{
/// <summary>
/// Verifies whether checksum included in the data matches the dats's contents.
/// Verify that the hash of the given <paramref name="data"/> of type <typeparamref name="T"/> matches the <paramref name="expectedHash"/>.
/// </summary>
/// <returns>True, if the checksum matches. Otherwise, false.</returns>
bool VerifyHash();
/// <param name="data">Data to be hashed, in format <typeparamref name="T"/>.</param>
/// <param name="expectedHash">A <see cref="string"/> representing the expected hash for the given <paramref name="data"/> of type <typeparamref name="T"/>.</param>
/// <param name="algorithm">The algorithm used to hash the given data of type <typeparamref name="T"/>.</param>
/// <returns>True, if the calculated hash of the data of type <typeparamref name="T"/> matches the <paramref name="expectedHash"/>. Otherwise, false.</returns>
public bool VerifyHash(T data, string expectedHash, IHashingAlgorithm<T> algorithm);
}
23 changes: 23 additions & 0 deletions src/IHashVerifierAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// HashingHandler by Simon Field

using System.Threading;
using System.Threading.Tasks;

namespace HashingHandler;

/// <summary>
/// Unifies classes supporting the asynchronous verification of a hash of an object of type <typeparamref name="T"/> with another hash.
/// </summary>
/// <typeparam name="T">The type of the object being hashed.</typeparam>
public interface IHashVerifierAsync<T> : IHashVerifier<T>
{
/// <summary>
/// Asynchronously verify that the hash of the given <paramref name="data"/> of type <typeparamref name="T"/> matches the <paramref name="expectedHash"/>.
/// </summary>
/// <param name="data">Data to be hashed, in format <typeparamref name="T"/>.</param>
/// <param name="expectedHash">A <see cref="string"/> representing the expected hash for the given <paramref name="data"/> of type <typeparamref name="T"/>.</param>
/// <param name="algorithm">The algorithm used to hash the given data of type <typeparamref name="T"/>.</param>
/// <param name="cancellationToken">A cancellation token allowing the canceling of asynchronous jobs.</param>
/// <returns></returns>
public Task<bool> VerifyHashAsync(T data, string expectedHash, IHashingAlgorithm<T> algorithm, CancellationToken cancellationToken = default);
}
2 changes: 1 addition & 1 deletion src/IHashingAlgorithmAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public interface IHashingAlgorithmAsync<T> : IHashingAlgorithm<T>
/// <param name="provider">The data interpreter.</param>
/// <param name="cancellationToken">A cancellation token allowing the canceling of asynchronous jobs.</param>
/// <returns>A <see cref="Task"/> including the hash computation job.</returns>
public Task<string> ComputeHashAsync(T data, IHashingProvider<T> provider, CancellationToken cancellationToken);
public Task<string> ComputeHashAsync(T data, IHashingProvider<T> provider, CancellationToken cancellationToken = default);
}
2 changes: 1 addition & 1 deletion src/IHashingProviderAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public interface IHashingProviderAsync<T> : IHashingProvider<T>
/// <param name="data">An object of type <typeparamref name="T"/> to be converted to <see cref="byte"/>[].</param>
/// <param name="cancellationToken">A cancellation token allowing the canceling of asynchronous jobs.</param>
/// <returns>A <see cref="Task"/> representing the conversion of this document of type <typeparamref name="T"/> in the form <see cref="byte"/>[].</returns>
public Task<byte[]> ConvertToBytes(T data, CancellationToken cancellationToken);
public Task<byte[]> ConvertToBytesAsync(T data, CancellationToken cancellationToken = default);
}
2 changes: 1 addition & 1 deletion src/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace HashingHandler;
/// <summary>
/// A class containing methods for manipulating <see cref="string"/> objects.
/// </summary>
public static class StringExtensions
internal static class StringExtensions
{
/// <summary>
/// Convert a <see cref="byte"/>[] to a <see cref="string"/> of hexadecimal characters.
Expand Down

0 comments on commit 1ffe40f

Please sign in to comment.