Skip to content

Commit

Permalink
v1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-techkid committed Nov 30, 2024
1 parent d1c0fb7 commit c756462
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/HashVerifierBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ public abstract class HashVerifierBase<T> : IHashVerifierAsync<T>
/// </summary>
public const StringComparison DefaultComparisonMethod = StringComparison.OrdinalIgnoreCase;

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

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

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

if (algorithm is IHashingAlgorithmAsync<T> asyncAlgorithm)
{
actualHash = await asyncAlgorithm.ComputeHashAsync(data, HashProvider, cancellationToken);
actualHash = await asyncAlgorithm.ComputeHashAsync(data, HashProvider, cancellationToken, encoding);
}
else
{
Expand Down
10 changes: 9 additions & 1 deletion src/HashingAlgorithmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ private async Task<string> AsyncHashComputation(T data, IHashingProvider<T> prov
}

byte[] hashBytes = await ComputeHashAsync(bytes, cancellationToken);
return encoding.ConvertToString(hashBytes);

if (encoding is IStringEncodingAsync asyncEncoding)
{
return await asyncEncoding.ConvertToStringAsync(hashBytes);
}
else
{
return encoding.ConvertToString(hashBytes);
}
}
}
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.6.0</Version>
<Version>1.7.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
3 changes: 2 additions & 1 deletion src/IHashVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IHashVerifier<T>
/// <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="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>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);
public bool VerifyHash(T data, string expectedHash, IHashingAlgorithm<T> algorithm, IStringEncoding? encoding = null);
}
3 changes: 2 additions & 1 deletion src/IHashVerifierAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IHashVerifierAsync<T> : IHashVerifier<T>
/// <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>
/// <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>True, if the hashes match. Otherwise, false.</returns>
public Task<bool> VerifyHashAsync(T data, string expectedHash, IHashingAlgorithm<T> algorithm, CancellationToken cancellationToken = default);
public Task<bool> VerifyHashAsync(T data, string expectedHash, IHashingAlgorithm<T> algorithm, CancellationToken cancellationToken = default, IStringEncoding? encoding = null);
}
3 changes: 3 additions & 0 deletions src/IStringEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace HashingHandler;

/// <summary>
/// Interface for encoding <see cref="byte"/>[] to <see cref="string"/>.
/// </summary>
public interface IStringEncoding
{
/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions src/IStringEncodingAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// HashingHandler by Simon Field

using System.Threading.Tasks;

namespace HashingHandler;

/// <summary>
/// Interface for encoding <see cref="byte"/>[] to <see cref="string"/> asynchronously.
/// </summary>
public interface IStringEncodingAsync : IStringEncoding
{
/// <summary>
/// Converts a <see cref="byte"/>[] to a <see cref="string"/> asynchronously.
/// </summary>
/// <param name="bytes">The <see cref="byte"/>[] to convert to a <see cref="string"/>.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation that returns a <see cref="string"/> converted from the <see cref="byte"/>[].</returns>
public Task<string> ConvertToStringAsync(byte[] bytes);
}
15 changes: 15 additions & 0 deletions src/StringEncodingBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// HashingHandler by Simon Field

using System.Threading.Tasks;

namespace HashingHandler;

/// <summary>
/// Base class for encoding <see cref="byte"/>[] to <see cref="string"/>.
/// </summary>
public abstract class StringEncodingBase : IStringEncodingAsync
{
public abstract string ConvertToString(byte[] bytes);

public virtual Task<string> ConvertToStringAsync(byte[] bytes) => Task.Run(() => ConvertToString(bytes));
}
4 changes: 2 additions & 2 deletions src/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace HashingHandler;
/// <summary>
/// A class containing methods for manipulating <see cref="string"/> objects.
/// </summary>
internal class StringExtensions : IStringEncoding
internal class StringExtensions : StringEncodingBase
{
public string ConvertToString(byte[] data)
public override string ConvertToString(byte[] data)
{
return BitConverter.ToString(data).Replace("-", "");
}
Expand Down

0 comments on commit c756462

Please sign in to comment.