diff --git a/Kryptos/Kryptos/Extensions.cs b/Kryptos/Kryptos/Extensions.cs new file mode 100644 index 0000000..7870ec7 --- /dev/null +++ b/Kryptos/Kryptos/Extensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Kryptos +{ + public static class Extensions + { + public static string ToHexString(this byte[] bytes) + { + var stringBuilder = new StringBuilder(bytes.Length); + foreach (var b in bytes) + { + stringBuilder.Append(b.ToString("X2")); + } + return stringBuilder.ToString(); + } + } +} diff --git a/Kryptos/Kryptos/Kryptos.csproj b/Kryptos/Kryptos/Kryptos.csproj index d9a80d0..be1f543 100644 --- a/Kryptos/Kryptos/Kryptos.csproj +++ b/Kryptos/Kryptos/Kryptos.csproj @@ -3,7 +3,7 @@ Exe netcoreapp3.1 - 0.0.1 + 0.0.2 Vijayshinva Karnure A .NET core tool for cryptography. (c) 2020 Vijayshinva Karnure diff --git a/Kryptos/Kryptos/Program.cs b/Kryptos/Kryptos/Program.cs index 471a33b..c73de69 100644 --- a/Kryptos/Kryptos/Program.cs +++ b/Kryptos/Kryptos/Program.cs @@ -13,7 +13,8 @@ static async Task Main(string[] args) TreatUnmatchedTokensAsErrors = true }; - rootCommand.WireUpBase64Commands(); + rootCommand.WireUpBase64Commands() + .WireUpMd5Commands(); return await rootCommand.InvokeAsync(args); } diff --git a/Kryptos/Kryptos/WireUpBase64Extensions.cs b/Kryptos/Kryptos/WireUpBase64Extensions.cs index 3750a7d..776a3d7 100644 --- a/Kryptos/Kryptos/WireUpBase64Extensions.cs +++ b/Kryptos/Kryptos/WireUpBase64Extensions.cs @@ -30,9 +30,11 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) }); base64EncCommand.Handler = CommandHandler.Create(async (text, input, output, console) => { + Stream outputStream = null; + Stream inputStream = null; try { - Stream outputStream = null; + if (output == null) { outputStream = new MemoryStream(); @@ -41,7 +43,7 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) { outputStream = output.OpenWrite(); } - Stream inputStream = null; + if (text != null) { inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); @@ -55,8 +57,7 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) { await inputStream.CopyToAsync(cryptoStream); } - await inputStream.DisposeAsync(); - await outputStream.DisposeAsync(); + if (output == null) { console.Out.WriteLine(Encoding.UTF8.GetString(((MemoryStream)outputStream).ToArray())); @@ -66,6 +67,17 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) { console.Out.WriteLine(ex.Message); } + finally + { + if (inputStream != null) + { + await inputStream.DisposeAsync(); + } + if (outputStream != null) + { + await outputStream.DisposeAsync(); + } + } }); var base64DecCommand = new Command("decode", "Decode"); base64DecCommand.AddAlias("dec"); @@ -83,9 +95,10 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) }); base64DecCommand.Handler = CommandHandler.Create(async (text, input, output, console) => { + Stream outputStream = null; + Stream inputStream = null; try { - Stream outputStream = null; if (output == null) { outputStream = new MemoryStream(); @@ -94,7 +107,7 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) { outputStream = output.OpenWrite(); } - Stream inputStream = null; + if (text != null) { inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); @@ -108,8 +121,7 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) { await inputStream.CopyToAsync(cryptoStream); } - await inputStream.DisposeAsync(); - await outputStream.DisposeAsync(); + if (output == null) { console.Out.WriteLine(Encoding.UTF8.GetString(((MemoryStream)outputStream).ToArray())); @@ -119,6 +131,17 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) { console.Out.WriteLine(ex.Message); } + finally + { + if (inputStream != null) + { + await inputStream.DisposeAsync(); + } + if (outputStream != null) + { + await outputStream.DisposeAsync(); + } + } }); base64Command.Add(base64EncCommand); base64Command.Add(base64DecCommand); diff --git a/Kryptos/Kryptos/WireUpMd5Extensions.cs b/Kryptos/Kryptos/WireUpMd5Extensions.cs new file mode 100644 index 0000000..bfe2f0d --- /dev/null +++ b/Kryptos/Kryptos/WireUpMd5Extensions.cs @@ -0,0 +1,88 @@ +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 WireUpMd5Extensions + { + public static RootCommand WireUpMd5Commands(this RootCommand rootCommand) + { + var md5Command = new Command("md5", "MD5"); + var md5HashCommand = new Command("hash", "Hash"); + md5HashCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + md5HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + md5HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + md5HashCommand.Handler = CommandHandler.Create(async (text, input, output, console) => + { + Stream outputStream = null; + Stream inputStream = null; + try + { + if (output == null) + { + outputStream = new MemoryStream(); + } + else + { + outputStream = output.OpenWrite(); + } + + if (text != null) + { + inputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)); + } + if (input != null) + { + inputStream = input.OpenRead(); + } + + using var md5 = MD5.Create(); + var hashBytes = md5.ComputeHash(inputStream); + if (output == null) + { + console.Out.WriteLine(hashBytes.ToHexString()); + } + else + { + 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(); + } + } + }); + + md5Command.Add(md5HashCommand); + rootCommand.AddCommand(md5Command); + + return rootCommand; + } + } +}