From 0068df2effb4d0ad26f67b66518326cee30aecb4 Mon Sep 17 00:00:00 2001 From: Naser Date: Thu, 9 Mar 2023 19:07:21 +0330 Subject: [PATCH] Change parameter calling structure + Add humanizer for text --- src/nHash/Features/HashAlgorithmFeature.cs | 76 +++++++++++++++++++ .../HashAlgorithms/BaseHashAlgorithm.cs | 70 ----------------- .../Features/HashAlgorithms/Md5Feature.cs | 13 ---- .../Features/HashAlgorithms/Sha1Feature.cs | 13 ---- .../Features/HashAlgorithms/Sha256Feature.cs | 13 ---- .../Features/HashAlgorithms/Sha384Feature.cs | 13 ---- .../Features/HashAlgorithms/Sha512Feature.cs | 13 ---- src/nHash/Features/HumanizeFeature.cs | 47 ++++++++++++ src/nHash/Features/Models/AlgorithmType.cs | 10 +++ src/nHash/Features/Models/HumanizeType.cs | 14 ++++ src/nHash/GlobalUsings.cs | 3 +- src/nHash/Startup.cs | 33 +++----- src/nHash/nHash.csproj | 2 +- 13 files changed, 160 insertions(+), 160 deletions(-) create mode 100644 src/nHash/Features/HashAlgorithmFeature.cs delete mode 100644 src/nHash/Features/HashAlgorithms/BaseHashAlgorithm.cs delete mode 100644 src/nHash/Features/HashAlgorithms/Md5Feature.cs delete mode 100644 src/nHash/Features/HashAlgorithms/Sha1Feature.cs delete mode 100644 src/nHash/Features/HashAlgorithms/Sha256Feature.cs delete mode 100644 src/nHash/Features/HashAlgorithms/Sha384Feature.cs delete mode 100644 src/nHash/Features/HashAlgorithms/Sha512Feature.cs create mode 100644 src/nHash/Features/HumanizeFeature.cs create mode 100644 src/nHash/Features/Models/AlgorithmType.cs create mode 100644 src/nHash/Features/Models/HumanizeType.cs diff --git a/src/nHash/Features/HashAlgorithmFeature.cs b/src/nHash/Features/HashAlgorithmFeature.cs new file mode 100644 index 0000000..8c9f80f --- /dev/null +++ b/src/nHash/Features/HashAlgorithmFeature.cs @@ -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 _textArgument; + private readonly Argument _algorithmType; + private readonly Option _fileName; + private readonly Option _lowerCase; + + + public HashAlgorithmFeature() + { + _algorithmType = new Argument("type", "Algorithm type"); + _textArgument = new Argument("text", "Text for calculate fingerprint"); + _fileName = new Option(name: "--file", description: "File name for calculate hash"); + _lowerCase = new Option(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); + } +} \ No newline at end of file diff --git a/src/nHash/Features/HashAlgorithms/BaseHashAlgorithm.cs b/src/nHash/Features/HashAlgorithms/BaseHashAlgorithm.cs deleted file mode 100644 index e62b13d..0000000 --- a/src/nHash/Features/HashAlgorithms/BaseHashAlgorithm.cs +++ /dev/null @@ -1,70 +0,0 @@ -namespace nHash.Features.HashAlgorithms; - -public abstract class BaseHashAlgorithm : IFeature -{ - private string HashName { get; } - private string CommandName { get; } - - public Command Command => GetFeatureCommand(); - private readonly Option _lowerCase; - private readonly Option _fileName; - private readonly Argument _textArgument; - - protected abstract byte[] CalculateHash(byte[] input); - - protected BaseHashAlgorithm(string commandName, string hashName) - { - HashName = hashName; - CommandName = commandName.ToLower(); - _lowerCase = new Option(name: "--lower", description: "Generate lower case"); - _fileName = new Option(name: "--file", description: $"File name for calculate {hashName}"); - _textArgument = new Argument("text", GetDefaultValue, $"text for calculate {hashName} fingerprint"); - } - - private Command GetFeatureCommand() - { - var command = new Command(CommandName, $"Calculate {HashName} fingerprint") - { - _lowerCase, - _fileName - }; - command.AddArgument(_textArgument); - command.SetHandler(CalculateTextHash, _textArgument, _lowerCase, _fileName); - - return command; - } - - private static string GetDefaultValue() - { - return string.Empty; - } - - private void CalculateTextHash(string text, bool lowerCase, string fileName) - { - if (!string.IsNullOrWhiteSpace(text)) - { - var inputBytes = System.Text.Encoding.UTF8.GetBytes(text); - CalculateHash(inputBytes, lowerCase); - return; - } - - if (!string.IsNullOrWhiteSpace(fileName)) - { - var fileBytes = File.ReadAllBytes(fileName); - CalculateHash(fileBytes, lowerCase); - } - } - - private void CalculateHash(byte[] inputBytes, bool lowerCase) - { - var hashBytes = CalculateHash(inputBytes); - var hashedText = Convert.ToHexString(hashBytes); - - if (lowerCase) - { - hashedText = hashedText.ToLower(); - } - - Console.WriteLine(hashedText); - } -} \ No newline at end of file diff --git a/src/nHash/Features/HashAlgorithms/Md5Feature.cs b/src/nHash/Features/HashAlgorithms/Md5Feature.cs deleted file mode 100644 index 1dc87f5..0000000 --- a/src/nHash/Features/HashAlgorithms/Md5Feature.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Security.Cryptography; - -namespace nHash.Features.HashAlgorithms; - -public class Md5Feature : BaseHashAlgorithm -{ - public Md5Feature() : base("md5", "MD5") - { - } - - protected override byte[] CalculateHash(byte[] input) - => MD5.HashData(input); -} \ No newline at end of file diff --git a/src/nHash/Features/HashAlgorithms/Sha1Feature.cs b/src/nHash/Features/HashAlgorithms/Sha1Feature.cs deleted file mode 100644 index 565a8c6..0000000 --- a/src/nHash/Features/HashAlgorithms/Sha1Feature.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Security.Cryptography; - -namespace nHash.Features.HashAlgorithms; - -public class Sha1Feature : BaseHashAlgorithm -{ - public Sha1Feature() : base("sha1", "SHA1") - { - } - - protected override byte[] CalculateHash(byte[] input) - => SHA1.HashData(input); -} \ No newline at end of file diff --git a/src/nHash/Features/HashAlgorithms/Sha256Feature.cs b/src/nHash/Features/HashAlgorithms/Sha256Feature.cs deleted file mode 100644 index c3606ff..0000000 --- a/src/nHash/Features/HashAlgorithms/Sha256Feature.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Security.Cryptography; - -namespace nHash.Features.HashAlgorithms; - -public class Sha256Feature : BaseHashAlgorithm -{ - public Sha256Feature() : base("sha256", "SHA256") - { - } - - protected override byte[] CalculateHash(byte[] input) - => SHA256.HashData(input); -} \ No newline at end of file diff --git a/src/nHash/Features/HashAlgorithms/Sha384Feature.cs b/src/nHash/Features/HashAlgorithms/Sha384Feature.cs deleted file mode 100644 index 5581aa6..0000000 --- a/src/nHash/Features/HashAlgorithms/Sha384Feature.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Security.Cryptography; - -namespace nHash.Features.HashAlgorithms; - -public class Sha384Feature : BaseHashAlgorithm -{ - public Sha384Feature() : base("sha384", "SHA384") - { - } - - protected override byte[] CalculateHash(byte[] input) - => SHA384.HashData(input); -} \ No newline at end of file diff --git a/src/nHash/Features/HashAlgorithms/Sha512Feature.cs b/src/nHash/Features/HashAlgorithms/Sha512Feature.cs deleted file mode 100644 index e6c57dd..0000000 --- a/src/nHash/Features/HashAlgorithms/Sha512Feature.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Security.Cryptography; - -namespace nHash.Features.HashAlgorithms; - -public class Sha512Feature : BaseHashAlgorithm -{ - public Sha512Feature() : base("sha512", "SHA512") - { - } - - protected override byte[] CalculateHash(byte[] input) - => SHA512.HashData(input); -} \ No newline at end of file diff --git a/src/nHash/Features/HumanizeFeature.cs b/src/nHash/Features/HumanizeFeature.cs new file mode 100644 index 0000000..357e9fc --- /dev/null +++ b/src/nHash/Features/HumanizeFeature.cs @@ -0,0 +1,47 @@ +using Humanizer; +using nHash.Features.Models; + +namespace nHash.Features; + +public class HumanizeFeature : IFeature +{ + public Command Command => GetFeatureCommand(); + private readonly Argument _textArgument; + private readonly Argument _humanizeType; + + public HumanizeFeature() + { + _humanizeType = new Argument("type", "Humanize type"); + _textArgument = new Argument("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); + } +} \ No newline at end of file diff --git a/src/nHash/Features/Models/AlgorithmType.cs b/src/nHash/Features/Models/AlgorithmType.cs new file mode 100644 index 0000000..b16d32b --- /dev/null +++ b/src/nHash/Features/Models/AlgorithmType.cs @@ -0,0 +1,10 @@ +namespace nHash.Features.Models; + +public enum AlgorithmType +{ + MD5, + SHA1, + SHA256, + SHA384, + SHA512, +} \ No newline at end of file diff --git a/src/nHash/Features/Models/HumanizeType.cs b/src/nHash/Features/Models/HumanizeType.cs new file mode 100644 index 0000000..a327813 --- /dev/null +++ b/src/nHash/Features/Models/HumanizeType.cs @@ -0,0 +1,14 @@ +namespace nHash.Features.Models; + +public enum HumanizeType +{ + Humanize, + Dehumanize, + Pascal, + Camel, + Kebab, + Underscore, + Hyphenate, + Lowercase, + Uppercase, +} \ No newline at end of file diff --git a/src/nHash/GlobalUsings.cs b/src/nHash/GlobalUsings.cs index 2c4dc3a..4b1256e 100644 --- a/src/nHash/GlobalUsings.cs +++ b/src/nHash/GlobalUsings.cs @@ -1,4 +1,3 @@ global using nHash.Features; global using nHash.Base; -global using System.CommandLine; -global using nHash.Features.HashAlgorithms; \ No newline at end of file +global using System.CommandLine; \ No newline at end of file diff --git a/src/nHash/Startup.cs b/src/nHash/Startup.cs index 3d161b5..0db1513 100644 --- a/src/nHash/Startup.cs +++ b/src/nHash/Startup.cs @@ -4,9 +4,17 @@ public static class Startup { public static async Task StartAsync(IEnumerable args) { - var features = GetFeatureClasses(); - - var rootCommand = new RootCommand("Hash utilities in command-line mode"); + var features = new List() + { + 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); @@ -38,23 +46,4 @@ private static async Task GetParameters(IEnumerable strings) return list.Where(_ => !string.IsNullOrWhiteSpace(_)).ToArray(); } - - private static IEnumerable GetFeatureClasses() - { - var res = new List(); - 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; - } } \ No newline at end of file diff --git a/src/nHash/nHash.csproj b/src/nHash/nHash.csproj index 6371c19..f6ec287 100644 --- a/src/nHash/nHash.csproj +++ b/src/nHash/nHash.csproj @@ -13,7 +13,7 @@ - +