Skip to content

Commit

Permalink
Change parameter calling structure
Browse files Browse the repository at this point in the history
+ Add humanizer for text
  • Loading branch information
nRafinia committed Mar 9, 2023
1 parent 4da451c commit 0068df2
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 160 deletions.
76 changes: 76 additions & 0 deletions src/nHash/Features/HashAlgorithmFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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", "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");
}

private Command GetFeatureCommand()
{
var command = new Command("hash",
"Calculate hash fingerprint (MD5, SHA-1, SHA-256, SHA-384, SHA-512)")
{
_fileName,
_lowerCase,
};
command.AddArgument(_algorithmType);
command.AddArgument(_textArgument);
command.SetHandler(CalculateText, _textArgument, _algorithmType, _lowerCase, _fileName);

return command;
}

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

if (!string.IsNullOrWhiteSpace(fileName))
{
var fileBytes = File.ReadAllBytes(fileName);
CalculateHash(fileBytes, algorithmType, lowerCase);
}
}

private static void CalculateHash(byte[] inputBytes, AlgorithmType algorithmType, bool lowerCase)
{
HashAlgorithm provider = algorithmType switch
{
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)
};

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

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

Console.WriteLine(hashedText);
}
}
70 changes: 0 additions & 70 deletions src/nHash/Features/HashAlgorithms/BaseHashAlgorithm.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/nHash/Features/HashAlgorithms/Md5Feature.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/nHash/Features/HashAlgorithms/Sha1Feature.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/nHash/Features/HashAlgorithms/Sha256Feature.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/nHash/Features/HashAlgorithms/Sha384Feature.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/nHash/Features/HashAlgorithms/Sha512Feature.cs

This file was deleted.

47 changes: 47 additions & 0 deletions src/nHash/Features/HumanizeFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Humanizer;
using nHash.Features.Models;

namespace nHash.Features;

public class HumanizeFeature : IFeature
{
public Command Command => GetFeatureCommand();
private readonly Argument<string> _textArgument;
private readonly Argument<HumanizeType> _humanizeType;

public HumanizeFeature()
{
_humanizeType = new Argument<HumanizeType>("type", "Humanize type");
_textArgument = new Argument<string>("text", "Text for humanize");
}

private Command GetFeatureCommand()
{
var command = new Command("humanize",
"Humanizer text (Pascal-case, Camel-case, Kebab, Underscore, lowercase etc)");
command.AddArgument(_humanizeType);
command.AddArgument(_textArgument);
command.SetHandler(CalculateText, _textArgument, _humanizeType);

return command;
}

private static void CalculateText(string text, HumanizeType humanizeType)
{
var resultText = humanizeType switch
{
HumanizeType.Humanize => text.Humanize(),
HumanizeType.Dehumanize => text.Dehumanize(),
HumanizeType.Pascal => text.Pascalize(),
HumanizeType.Camel => text.Camelize(),
HumanizeType.Kebab => text.Kebaberize(),
HumanizeType.Underscore => text.Underscore(),
HumanizeType.Hyphenate => text.Hyphenate(),
HumanizeType.Lowercase => text.ToLower(),
HumanizeType.Uppercase => text.ToUpper(),
_ => text
};

Console.WriteLine(resultText);
}
}
10 changes: 10 additions & 0 deletions src/nHash/Features/Models/AlgorithmType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace nHash.Features.Models;

public enum AlgorithmType
{
MD5,
SHA1,
SHA256,
SHA384,
SHA512,
}
14 changes: 14 additions & 0 deletions src/nHash/Features/Models/HumanizeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace nHash.Features.Models;

public enum HumanizeType
{
Humanize,
Dehumanize,
Pascal,
Camel,
Kebab,
Underscore,
Hyphenate,
Lowercase,
Uppercase,
}
3 changes: 1 addition & 2 deletions src/nHash/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
global using nHash.Features;
global using nHash.Base;
global using System.CommandLine;
global using nHash.Features.HashAlgorithms;
global using System.CommandLine;
33 changes: 11 additions & 22 deletions src/nHash/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ public static class Startup
{
public static async Task<int> StartAsync(IEnumerable<string> args)
{
var features = GetFeatureClasses();

var rootCommand = new RootCommand("Hash utilities in command-line mode");
var features = new List<IFeature>()
{
new GuidFeature(),
new UrlFeature(),
new HtmlFeature(),
new HashAlgorithmFeature(),
new Base64Feature(),
new HumanizeFeature()
};

var rootCommand = new RootCommand("Hash and Text utilities in command-line mode");
foreach (var command in features)
{
rootCommand.AddCommand(command.Command);
Expand Down Expand Up @@ -38,23 +46,4 @@ private static async Task<string[]> GetParameters(IEnumerable<string> strings)

return list.Where(_ => !string.IsNullOrWhiteSpace(_)).ToArray();
}

private static IEnumerable<IFeature> GetFeatureClasses()
{
var res = new List<IFeature>();
try
{
var featureType = typeof(IFeature);
var types = typeof(Program).Assembly.GetTypes()
.Where(_ => _ is { IsInterface: false, IsAbstract: false } && featureType.IsAssignableFrom(_))
.Select(_ => Activator.CreateInstance(_) as IFeature);
res.AddRange(types!);
}
catch
{
//
}

return res;
}
}
2 changes: 1 addition & 1 deletion src/nHash/nHash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<!-- <PackageReference Include="CppSharp" Version="1.0.45.22293" />-->
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
</Project>

0 comments on commit 0068df2

Please sign in to comment.