-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add CheckSumGenerator boilerplate files * Add progress loading for the hashing calculation * Refactor compute hash helper * Refactor incorrect variable name * Add HashingHelper unit tests * Update rews files * Add compute hash iterations logic * Short circuit CheckSum if file is null * Disable hashing dropdown while processing Apply code review suggestions * Revert assembly info version * Apply code review changes * Remove ControlsToolKit xaml import
- Loading branch information
1 parent
1a2c03d
commit 3c1bac2
Showing
25 changed files
with
2,311 additions
and
16 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using DevToys.Models; | ||
using DevToys.Shared.Core; | ||
|
||
namespace DevToys.Helpers | ||
{ | ||
internal static class HashingHelper | ||
{ | ||
internal static async Task<byte[]> ComputeHashAsync( | ||
HashAlgorithm hashAlgorithm, | ||
Stream stream, | ||
IProgress<HashingProgress> progress, | ||
CancellationToken cancellationToken, | ||
int bufferSize = 1024 * 1024) | ||
{ | ||
Arguments.NotNull(stream, nameof(stream)); | ||
Arguments.NotNull(hashAlgorithm, nameof(hashAlgorithm)); | ||
|
||
byte[] readAheadBuffer = new byte[bufferSize]; | ||
byte[] buffer; | ||
|
||
int readAheadBytes = await stream.ReadAsync(readAheadBuffer, 0, bufferSize, cancellationToken); | ||
int bytesRead; | ||
long totalBytesRead = readAheadBytes; | ||
|
||
while (readAheadBytes != 0) | ||
{ | ||
bytesRead = readAheadBytes; | ||
buffer = readAheadBuffer; | ||
|
||
readAheadBytes = await stream.ReadAsync(readAheadBuffer, 0, bufferSize, cancellationToken); | ||
totalBytesRead += readAheadBytes; | ||
|
||
if (readAheadBytes == 0) | ||
{ | ||
hashAlgorithm.TransformFinalBlock(buffer, 0, bytesRead); | ||
} | ||
else | ||
{ | ||
hashAlgorithm.TransformBlock(buffer, 0, bytesRead, buffer, 0); | ||
} | ||
|
||
progress.Report(new HashingProgress(stream.Length, totalBytesRead)); | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
} | ||
return hashAlgorithm.Hash ?? Array.Empty<byte>(); | ||
} | ||
|
||
internal static int ComputeHashIterations(Stream stream, int bufferSize = 1024 * 1024) | ||
{ | ||
Arguments.NotNull(stream, nameof(stream)); | ||
Arguments.NotZeroOrBelow(bufferSize, nameof(bufferSize)); | ||
|
||
if(stream.Length == 0) | ||
{ | ||
return 0; | ||
} | ||
else if(bufferSize >= stream.Length) | ||
{ | ||
return 1; | ||
} | ||
|
||
return (int)(stream.Length / bufferSize); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DevToys.Models | ||
{ | ||
public enum HashingAlgorithm | ||
{ | ||
MD5, | ||
SHA1, | ||
SHA256, | ||
SHA384, | ||
SHA512 | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/dev/impl/DevToys/Models/HashingAlgorithmDisplayPair.cs
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,25 @@ | ||
using System; | ||
|
||
namespace DevToys.Models | ||
{ | ||
public sealed class HashingAlgorithmDisplayPair : IEquatable<HashingAlgorithmDisplayPair> | ||
{ | ||
public static readonly HashingAlgorithmDisplayPair MD5 = new(nameof(MD5), HashingAlgorithm.MD5); | ||
public static readonly HashingAlgorithmDisplayPair SHA1 = new(nameof(SHA1), HashingAlgorithm.SHA1); | ||
public static readonly HashingAlgorithmDisplayPair SHA256 = new(nameof(SHA256), HashingAlgorithm.SHA256); | ||
public static readonly HashingAlgorithmDisplayPair SHA384 = new(nameof(SHA384), HashingAlgorithm.SHA384); | ||
public static readonly HashingAlgorithmDisplayPair SHA512 = new(nameof(SHA512), HashingAlgorithm.SHA512); | ||
|
||
public string DisplayName { get; } | ||
|
||
public HashingAlgorithm Value { get; } | ||
|
||
private HashingAlgorithmDisplayPair(string displayName, HashingAlgorithm value) | ||
{ | ||
DisplayName = displayName; | ||
Value = value; | ||
} | ||
|
||
public bool Equals(HashingAlgorithmDisplayPair other) => other.Value == Value; | ||
} | ||
} |
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,18 @@ | ||
using System; | ||
|
||
namespace DevToys.Models | ||
{ | ||
public class HashingProgress | ||
{ | ||
public long TotalBytes { get; } | ||
public long CompletedBytes { get; } | ||
|
||
public int GetPercentage() => (int)Math.Floor(100f * CompletedBytes / TotalBytes); | ||
|
||
public HashingProgress(long totalBytes, long completedBytes = 0) | ||
{ | ||
TotalBytes = totalBytes; | ||
CompletedBytes = completedBytes; | ||
} | ||
} | ||
} |
Oops, something went wrong.