Skip to content

Commit

Permalink
Feature/check sum generator (#234)
Browse files Browse the repository at this point in the history
* 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
Joaolfelicio authored Jan 27, 2022
1 parent 1a2c03d commit 3c1bac2
Show file tree
Hide file tree
Showing 25 changed files with 2,311 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Many tools are available.
- Hash (MD5, SHA1, SHA256, SHA512)
- UUID 1 and 4
- Lorem Ipsum
- Check Sum
- Text
- Inspector & Case Converter
- Regex Tester
Expand Down Expand Up @@ -97,6 +98,7 @@ Here is the list of tool name you can use:
- `hash` - Hash Generator
- `uuid` - UUID Generator
- `loremipsum` - Lorem Ipsum Generator
- `checksum` - CheckSum File
- `jsonformat` Json Formatter
- `jsonyaml` - Json <> Yaml
- `jwt` - JWT Decoder
Expand Down
13 changes: 13 additions & 0 deletions src/dev/impl/DevToys/DevToys.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
<Compile Include="Helpers\StorageFileHelper.cs" />
<Compile Include="Helpers\ImageHelper.cs" />
<Compile Include="Helpers\ColorBlindnessSimulatorHelper.cs" />
<Compile Include="Helpers\HashingHelper.cs" />
<Compile Include="Helpers\NumberBaseHelper.cs" />
<Compile Include="Messages\PinToolToStartMessage.cs" />
<Compile Include="Messages\OpenToolInNewWindowMessage.cs" />
<Compile Include="Messages\ChangeSelectedMenuItemMessage.cs" />
<Compile Include="Models\HashingAlgorithm.cs" />
<Compile Include="Models\HashingAlgorithmDisplayPair.cs" />
<Compile Include="Models\HashingProgress.cs" />
<Compile Include="Models\NoResultFoundMockToolProvider.cs" />
<Compile Include="Models\Radix.cs" />
<Compile Include="Models\NumberBaseFormat.cs" />
Expand All @@ -53,6 +57,8 @@
<Compile Include="ViewModels\Tools\Converters\JsonYaml\DecimalJsonConverter.cs" />
<Compile Include="ViewModels\Tools\Converters\JsonYaml\DecimalYamlTypeResolver.cs" />
<Compile Include="ViewModels\Tools\EncodersDecoders\EncodersDecodersGroupToolProvider.cs" />
<Compile Include="ViewModels\Tools\Generators\CheckSumGenerator\CheckSumGeneratorToolProvider.cs" />
<Compile Include="ViewModels\Tools\Generators\CheckSumGenerator\CheckSumGeneratorToolViewModel.cs" />
<Compile Include="ViewModels\Tools\Generators\GeneratorsGroupToolProvider.cs" />
<Compile Include="ViewModels\Tools\Generators\LoremIpsumGenerator\LoremIpsumGeneratorToolProvider.cs" />
<Compile Include="ViewModels\Tools\Generators\LoremIpsumGenerator\LoremIpsumGeneratorToolViewModel.cs" />
Expand All @@ -79,6 +85,9 @@
<Compile Include="Views\Tools\EncodersDecoders\GZipEncoderDecoder\GZipEncoderDecoderToolPage.xaml.cs">
<DependentUpon>GZipEncoderDecoderToolPage.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Tools\Generators\CheckSumGenerator\CheckSumGeneratorToolPage.xaml.cs">
<DependentUpon>CheckSumGeneratorToolPage.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Tools\Graphic\ColorBlindnessSimulator\ColorBlindnessSimulatorToolPage.xaml.cs">
<DependentUpon>ColorBlindnessSimulatorToolPage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -525,6 +534,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Tools\Generators\CheckSumGenerator\CheckSumGeneratorToolPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Tools\Graphic\ColorBlindnessSimulator\ColorBlindnessSimulatorToolPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
72 changes: 72 additions & 0 deletions src/dev/impl/DevToys/Helpers/HashingHelper.cs
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);
}
}
}
96 changes: 96 additions & 0 deletions src/dev/impl/DevToys/LanguageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class LanguageManager : ObservableObject

private readonly AllToolsStrings _alltools = new AllToolsStrings();
private readonly Base64EncoderDecoderStrings _base64encoderdecoder = new Base64EncoderDecoderStrings();
private readonly CheckSumGeneratorStrings _checksumgenerator = new CheckSumGeneratorStrings();
private readonly ColorBlindnessSimulatorStrings _colorblindnesssimulator = new ColorBlindnessSimulatorStrings();
private readonly CommonStrings _common = new CommonStrings();
private readonly GuidGeneratorStrings _guidgenerator = new GuidGeneratorStrings();
Expand Down Expand Up @@ -69,6 +70,11 @@ public partial class LanguageManager : ObservableObject
/// </summary>
public Base64EncoderDecoderStrings Base64EncoderDecoder => _base64encoderdecoder;

/// <summary>
/// Gets the <see cref="CheckSumGeneratorStrings"/>.
/// </summary>
public CheckSumGeneratorStrings CheckSumGenerator => _checksumgenerator;

/// <summary>
/// Gets the <see cref="ColorBlindnessSimulatorStrings"/>.
/// </summary>
Expand Down Expand Up @@ -321,6 +327,96 @@ public class Base64EncoderDecoderStrings : ObservableObject
public string SearchDisplayName => _resources.GetString("SearchDisplayName");
}

public class CheckSumGeneratorStrings : ObservableObject
{
private readonly ResourceLoader _resources = ResourceLoader.GetForViewIndependentUse("CheckSumGenerator");

/// <summary>
/// Gets the resource AccessibleName.
/// </summary>
public string AccessibleName => _resources.GetString("AccessibleName");

/// <summary>
/// Gets the resource Cancel.
/// </summary>
public string Cancel => _resources.GetString("Cancel");

/// <summary>
/// Gets the resource Configuration.
/// </summary>
public string Configuration => _resources.GetString("Configuration");

/// <summary>
/// Gets the resource Description.
/// </summary>
public string Description => _resources.GetString("Description");

/// <summary>
/// Gets the resource HashingAlgorithmDescription.
/// </summary>
public string HashingAlgorithmDescription => _resources.GetString("HashingAlgorithmDescription");

/// <summary>
/// Gets the resource HashingAlgorithmTitle.
/// </summary>
public string HashingAlgorithmTitle => _resources.GetString("HashingAlgorithmTitle");

/// <summary>
/// Gets the resource Input.
/// </summary>
public string Input => _resources.GetString("Input");

/// <summary>
/// Gets the resource MenuDisplayName.
/// </summary>
public string MenuDisplayName => _resources.GetString("MenuDisplayName");

/// <summary>
/// Gets the resource Output.
/// </summary>
public string Output => _resources.GetString("Output");

/// <summary>
/// Gets the resource OutputComparer.
/// </summary>
public string OutputComparer => _resources.GetString("OutputComparer");

/// <summary>
/// Gets the resource SaveAs.
/// </summary>
public string SaveAs => _resources.GetString("SaveAs");

/// <summary>
/// Gets the resource SearchDisplayName.
/// </summary>
public string SearchDisplayName => _resources.GetString("SearchDisplayName");

/// <summary>
/// Gets the resource SelectFilesInstruction1.
/// </summary>
public string SelectFilesInstruction1 => _resources.GetString("SelectFilesInstruction1");

/// <summary>
/// Gets the resource SelectFilesInstruction2.
/// </summary>
public string SelectFilesInstruction2 => _resources.GetString("SelectFilesInstruction2");

/// <summary>
/// Gets the resource SelectFilesInstruction3.
/// </summary>
public string SelectFilesInstruction3 => _resources.GetString("SelectFilesInstruction3");

/// <summary>
/// Gets the resource SelectFilesInstruction4.
/// </summary>
public string SelectFilesInstruction4 => _resources.GetString("SelectFilesInstruction4");

/// <summary>
/// Gets the resource Uppercase.
/// </summary>
public string Uppercase => _resources.GetString("Uppercase");
}

public class ColorBlindnessSimulatorStrings : ObservableObject
{
private readonly ResourceLoader _resources = ResourceLoader.GetForViewIndependentUse("ColorBlindnessSimulator");
Expand Down
17 changes: 17 additions & 0 deletions src/dev/impl/DevToys/Models/HashingAlgorithm.cs
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 src/dev/impl/DevToys/Models/HashingAlgorithmDisplayPair.cs
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;
}
}
18 changes: 18 additions & 0 deletions src/dev/impl/DevToys/Models/HashingProgress.cs
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;
}
}
}
Loading

0 comments on commit 3c1bac2

Please sign in to comment.