diff --git a/Kryptos/Kryptos/Kryptos.csproj b/Kryptos/Kryptos/Kryptos.csproj index ae2ad94..a8fbb4a 100644 --- a/Kryptos/Kryptos/Kryptos.csproj +++ b/Kryptos/Kryptos/Kryptos.csproj @@ -3,7 +3,7 @@ Exe netcoreapp3.1 - 0.0.4 + 0.0.5 Vijayshinva Karnure A .NET core tool for cryptography. 2020 Vijayshinva Karnure diff --git a/Kryptos/Kryptos/Program.cs b/Kryptos/Kryptos/Program.cs index f06ac14..6a66137 100644 --- a/Kryptos/Kryptos/Program.cs +++ b/Kryptos/Kryptos/Program.cs @@ -21,7 +21,12 @@ static async Task Main(string[] args) .WireUpSha256Commands() .WireUpSha384Commands() .WireUpSha512Commands() - .WireUpJwtCommands(); + .WireUpJwtCommands() + .WireUpHmacMd5Commands() + .WireUpHmacSha1Commands() + .WireUpHmacSha256Commands() + .WireUpHmacSha384Commands() + .WireUpHmacSha512Commands(); return await rootCommand.InvokeAsync(args); } diff --git a/Kryptos/Kryptos/WireUpHmacMd5Extensions.cs b/Kryptos/Kryptos/WireUpHmacMd5Extensions.cs new file mode 100644 index 0000000..f1d8907 --- /dev/null +++ b/Kryptos/Kryptos/WireUpHmacMd5Extensions.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Kryptos +{ + public static class WireUpHmacMd5Extensions + { + public static RootCommand WireUpHmacMd5Commands(this RootCommand rootCommand) + { + var hmacmd5Command = new Command("hmacmd5", "Hash based Message Authentication Code - MD5"); + var hmacmd5HashCommand = new Command("hash", "Hash"); + hmacmd5HashCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + hmacmd5HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + hmacmd5HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") + { + Argument = new Argument("text") + }); + hmacmd5HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") + { + Argument = new Argument("input") + }); + hmacmd5HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + hmacmd5HashCommand.Handler = CommandHandler.Create(async (text, input, keytext, keyinput, output, console) => + { + Stream outputStream = null; + Stream inputStream = null; + try + { + + if (text != null) + { + inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); + } + if (input != null) + { + inputStream = input.OpenRead(); + } + + byte[] key = null; + + if (keytext != null) + { + key = Encoding.UTF8.GetBytes(keytext); + } + + if (keyinput != null) + { + key = await File.ReadAllBytesAsync(keyinput.FullName); + } + + using var hmacMd5 = new HMACMD5(key); + var hashBytes = hmacMd5.ComputeHash(inputStream); + if (output == null) + { + console.Out.WriteLine(hashBytes.ToHexString()); + } + else + { + outputStream = output.OpenWrite(); + await outputStream.WriteAsync(hashBytes); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + } + finally + { + if (inputStream != null) + { + await inputStream.DisposeAsync(); + } + if (outputStream != null) + { + await outputStream.DisposeAsync(); + } + } + }); + + hmacmd5Command.Add(hmacmd5HashCommand); + rootCommand.AddCommand(hmacmd5Command); + + return rootCommand; + } + } +} diff --git a/Kryptos/Kryptos/WireUpHmacSha1Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha1Extensions.cs new file mode 100644 index 0000000..d673b62 --- /dev/null +++ b/Kryptos/Kryptos/WireUpHmacSha1Extensions.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Kryptos +{ + public static class WireUpHmacSha1Extensions + { + public static RootCommand WireUpHmacSha1Commands(this RootCommand rootCommand) + { + var hmacsha1Command = new Command("hmacsha1", "Hash based Message Authentication Code - SHA1"); + var hmacsha1HashCommand = new Command("hash", "Hash"); + hmacsha1HashCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + hmacsha1HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + hmacsha1HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") + { + Argument = new Argument("text") + }); + hmacsha1HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") + { + Argument = new Argument("input") + }); + hmacsha1HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + hmacsha1HashCommand.Handler = CommandHandler.Create(async (text, input, keytext, keyinput, output, console) => + { + Stream outputStream = null; + Stream inputStream = null; + try + { + + if (text != null) + { + inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); + } + if (input != null) + { + inputStream = input.OpenRead(); + } + + byte[] key = null; + + if (keytext != null) + { + key = Encoding.UTF8.GetBytes(keytext); + } + + if (keyinput != null) + { + key = await File.ReadAllBytesAsync(keyinput.FullName); + } + + using var hmacSha1 = new HMACSHA1(key); + var hashBytes = hmacSha1.ComputeHash(inputStream); + if (output == null) + { + console.Out.WriteLine(hashBytes.ToHexString()); + } + else + { + outputStream = output.OpenWrite(); + await outputStream.WriteAsync(hashBytes); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + } + finally + { + if (inputStream != null) + { + await inputStream.DisposeAsync(); + } + if (outputStream != null) + { + await outputStream.DisposeAsync(); + } + } + }); + + hmacsha1Command.Add(hmacsha1HashCommand); + rootCommand.AddCommand(hmacsha1Command); + + return rootCommand; + } + } +} diff --git a/Kryptos/Kryptos/WireUpHmacSha256Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha256Extensions.cs new file mode 100644 index 0000000..017cfd0 --- /dev/null +++ b/Kryptos/Kryptos/WireUpHmacSha256Extensions.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Kryptos +{ + public static class WireUpHmacSha256Extensions + { + public static RootCommand WireUpHmacSha256Commands(this RootCommand rootCommand) + { + var hmacsha256Command = new Command("hmacsha256", "Hash based Message Authentication Code - SHA256"); + var hmacsha256HashCommand = new Command("hash", "Hash"); + hmacsha256HashCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + hmacsha256HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + hmacsha256HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") + { + Argument = new Argument("text") + }); + hmacsha256HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") + { + Argument = new Argument("input") + }); + hmacsha256HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + hmacsha256HashCommand.Handler = CommandHandler.Create(async (text, input, keytext, keyinput, output, console) => + { + Stream outputStream = null; + Stream inputStream = null; + try + { + + if (text != null) + { + inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); + } + if (input != null) + { + inputStream = input.OpenRead(); + } + + byte[] key = null; + + if (keytext != null) + { + key = Encoding.UTF8.GetBytes(keytext); + } + + if (keyinput != null) + { + key = await File.ReadAllBytesAsync(keyinput.FullName); + } + + using var hmacSha256 = new HMACSHA256(key); + var hashBytes = hmacSha256.ComputeHash(inputStream); + if (output == null) + { + console.Out.WriteLine(hashBytes.ToHexString()); + } + else + { + outputStream = output.OpenWrite(); + await outputStream.WriteAsync(hashBytes); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + } + finally + { + if (inputStream != null) + { + await inputStream.DisposeAsync(); + } + if (outputStream != null) + { + await outputStream.DisposeAsync(); + } + } + }); + + hmacsha256Command.Add(hmacsha256HashCommand); + rootCommand.AddCommand(hmacsha256Command); + + return rootCommand; + } + } +} diff --git a/Kryptos/Kryptos/WireUpHmacSha384Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha384Extensions.cs new file mode 100644 index 0000000..e5d3a52 --- /dev/null +++ b/Kryptos/Kryptos/WireUpHmacSha384Extensions.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Kryptos +{ + public static class WireUpHmacSha384Extensions + { + public static RootCommand WireUpHmacSha384Commands(this RootCommand rootCommand) + { + var hmacsha384Command = new Command("hmacsha384", "Hash based Message Authentication Code - SHA384"); + var hmacsha384HashCommand = new Command("hash", "Hash"); + hmacsha384HashCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + hmacsha384HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + hmacsha384HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") + { + Argument = new Argument("text") + }); + hmacsha384HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") + { + Argument = new Argument("input") + }); + hmacsha384HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + hmacsha384HashCommand.Handler = CommandHandler.Create(async (text, input, keytext, keyinput, output, console) => + { + Stream outputStream = null; + Stream inputStream = null; + try + { + + if (text != null) + { + inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); + } + if (input != null) + { + inputStream = input.OpenRead(); + } + + byte[] key = null; + + if (keytext != null) + { + key = Encoding.UTF8.GetBytes(keytext); + } + + if (keyinput != null) + { + key = await File.ReadAllBytesAsync(keyinput.FullName); + } + + using var hmacSha384 = new HMACSHA384(key); + var hashBytes = hmacSha384.ComputeHash(inputStream); + if (output == null) + { + console.Out.WriteLine(hashBytes.ToHexString()); + } + else + { + outputStream = output.OpenWrite(); + await outputStream.WriteAsync(hashBytes); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + } + finally + { + if (inputStream != null) + { + await inputStream.DisposeAsync(); + } + if (outputStream != null) + { + await outputStream.DisposeAsync(); + } + } + }); + + hmacsha384Command.Add(hmacsha384HashCommand); + rootCommand.AddCommand(hmacsha384Command); + + return rootCommand; + } + } +} diff --git a/Kryptos/Kryptos/WireUpHmacSha512Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha512Extensions.cs new file mode 100644 index 0000000..3cabbc4 --- /dev/null +++ b/Kryptos/Kryptos/WireUpHmacSha512Extensions.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Kryptos +{ + public static class WireUpHmacSha512Extensions + { + public static RootCommand WireUpHmacSha512Commands(this RootCommand rootCommand) + { + var hmacsha512Command = new Command("hmacsha512", "Hash based Message Authentication Code - SHA512"); + var hmacsha512HashCommand = new Command("hash", "Hash"); + hmacsha512HashCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + hmacsha512HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + hmacsha512HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") + { + Argument = new Argument("text") + }); + hmacsha512HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") + { + Argument = new Argument("input") + }); + hmacsha512HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + hmacsha512HashCommand.Handler = CommandHandler.Create(async (text, input, keytext, keyinput, output, console) => + { + Stream outputStream = null; + Stream inputStream = null; + try + { + + if (text != null) + { + inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); + } + if (input != null) + { + inputStream = input.OpenRead(); + } + + byte[] key = null; + + if (keytext != null) + { + key = Encoding.UTF8.GetBytes(keytext); + } + + if (keyinput != null) + { + key = await File.ReadAllBytesAsync(keyinput.FullName); + } + + using var hmacSha512 = new HMACSHA512(key); + var hashBytes = hmacSha512.ComputeHash(inputStream); + if (output == null) + { + console.Out.WriteLine(hashBytes.ToHexString()); + } + else + { + outputStream = output.OpenWrite(); + await outputStream.WriteAsync(hashBytes); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + } + finally + { + if (inputStream != null) + { + await inputStream.DisposeAsync(); + } + if (outputStream != null) + { + await outputStream.DisposeAsync(); + } + } + }); + + hmacsha512Command.Add(hmacsha512HashCommand); + rootCommand.AddCommand(hmacsha512Command); + + return rootCommand; + } + } +} diff --git a/README.md b/README.md index 4275b7b..caa3ed5 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ A .NET core tool for cryptography. - MD5 Hash - SHA-1, SHA-256, SHA-384, SHA-512 Hash - JWT decoding +- HMAC-SHA1, HMAC-SHA256, HMAC-SHA384, HMAC-SHA512, HMAC-MD5 ## Installation @@ -29,12 +30,19 @@ A .NET core tool for cryptography. dotnet tool install --global Kryptos ``` +For offline installation, download the [nuget package][nuget-package] into a folder and run the following command. + +``` +dotnet tool install --global Kryptos --add-source FolderWithKryptosNuget\ +``` + ## Update **Kryptos** is under active development and new features are being added. Update to the latest version using the command below. ``` dotnet tool update --global Kryptos ``` + ## Usage ``` @@ -77,4 +85,5 @@ NOTE: By raising a Pull Request you grant the guardians of this project the nece -[git-repo]: https://github.com/vijayshinva/kryptos \ No newline at end of file +[git-repo]: https://github.com/vijayshinva/kryptos +[nuget-package]: https://www.nuget.org/packages/Kryptos \ No newline at end of file