-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e99afb
commit 1ffe40f
Showing
9 changed files
with
82 additions
and
34 deletions.
There are no files selected for viewing
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
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; | ||
} | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
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
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); | ||
} |
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
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); | ||
} |
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
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
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