Skip to content

Commit

Permalink
Unify algorithms
Browse files Browse the repository at this point in the history
- Remove type parameter
* Write all types at once
  • Loading branch information
nRafinia committed Mar 11, 2023
1 parent 70ad15e commit 34340b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
47 changes: 23 additions & 24 deletions src/nHash/Features/HashAlgorithmFeature.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System.Security.Cryptography;
using nHash.Features.Models;

namespace nHash.Features;

public class HashAlgorithmFeature : IFeature
{
public Command Command => GetFeatureCommand();
private readonly Argument<string> _textArgument;
private readonly Argument<AlgorithmType> _algorithmType;
private readonly Option<string> _fileName;
private readonly Option<bool> _lowerCase;


public HashAlgorithmFeature()
{
_algorithmType = new Argument<AlgorithmType>("type", "Algorithm type");
_textArgument = new Argument<string>("text", GetDefaultString, "Text for calculate fingerprint");
_fileName = new Option<string>(name: "--file", description: "File name for calculate hash");
_lowerCase = new Option<bool>(name: "--lower", description: "Generate lower case");
Expand All @@ -28,19 +25,18 @@ private Command GetFeatureCommand()
_fileName,
_lowerCase,
};
command.AddArgument(_algorithmType);
command.AddArgument(_textArgument);
command.SetHandler(CalculateText, _textArgument, _algorithmType, _lowerCase, _fileName);
command.SetHandler(CalculateText, _textArgument, _lowerCase, _fileName);

return command;
}

private static void CalculateText(string text, AlgorithmType algorithmType, bool lowerCase, string fileName)
private static void CalculateText(string text, bool lowerCase, string fileName)
{
if (!string.IsNullOrWhiteSpace(text))
{
var inputBytes = System.Text.Encoding.UTF8.GetBytes(text);
CalculateHash(inputBytes, algorithmType, lowerCase);
CalculateHash(inputBytes, lowerCase);
return;
}

Expand All @@ -51,33 +47,36 @@ private static void CalculateText(string text, AlgorithmType algorithmType, bool
Console.WriteLine($"File {fileName} does not exists!");
return;
}

var fileBytes = File.ReadAllBytes(fileName);
CalculateHash(fileBytes, algorithmType, lowerCase);
CalculateHash(fileBytes, lowerCase);
}
}

private static void CalculateHash(byte[] inputBytes, AlgorithmType algorithmType, bool lowerCase)
private static void CalculateHash(byte[] inputBytes, bool lowerCase)
{
HashAlgorithm provider = algorithmType switch
var algorithms = new Dictionary<string, HashAlgorithm>()
{
AlgorithmType.MD5 => MD5.Create(),
AlgorithmType.SHA1 => SHA1.Create(),
AlgorithmType.SHA256 => SHA256.Create(),
AlgorithmType.SHA384 => SHA384.Create(),
AlgorithmType.SHA512 => SHA512.Create(),
_ => throw new ArgumentOutOfRangeException(nameof(algorithmType), algorithmType, null)
{ "MD5", MD5.Create() },
{ "SHA-1", SHA1.Create() },
{ "SHA-256", SHA256.Create() },
{ "SHA-384", SHA384.Create() },
{ "SHA-512", SHA512.Create() }
};

var hashBytes = provider.ComputeHash(inputBytes);
var hashedText = Convert.ToHexString(hashBytes);

if (lowerCase)
foreach (var algorithm in algorithms)
{
hashedText = hashedText.ToLower();
}
var hashBytes = algorithm.Value.ComputeHash(inputBytes);
var hashedText = Convert.ToHexString(hashBytes);

Console.WriteLine(hashedText);
if (lowerCase)
{
hashedText = hashedText.ToLower();
}

Console.WriteLine($"{algorithm.Key}:");
Console.WriteLine(hashedText);
}
}

private static string GetDefaultString() => string.Empty;
Expand Down
10 changes: 0 additions & 10 deletions src/nHash/Features/Models/AlgorithmType.cs

This file was deleted.

0 comments on commit 34340b8

Please sign in to comment.