Skip to content

Commit

Permalink
MD5
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayshinva committed Nov 15, 2020
1 parent 5d13ac3 commit 5671a29
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 10 deletions.
19 changes: 19 additions & 0 deletions Kryptos/Kryptos/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
2 changes: 1 addition & 1 deletion Kryptos/Kryptos/Kryptos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>0.0.1</Version>
<Version>0.0.2</Version>
<Authors>Vijayshinva Karnure</Authors>
<Description>A .NET core tool for cryptography.</Description>
<Copyright>(c) 2020 Vijayshinva Karnure</Copyright>
Expand Down
3 changes: 2 additions & 1 deletion Kryptos/Kryptos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ static async Task<int> Main(string[] args)
TreatUnmatchedTokensAsErrors = true
};

rootCommand.WireUpBase64Commands();
rootCommand.WireUpBase64Commands()
.WireUpMd5Commands();

return await rootCommand.InvokeAsync(args);
}
Expand Down
39 changes: 31 additions & 8 deletions Kryptos/Kryptos/WireUpBase64Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand)
});
base64EncCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, IConsole>(async (text, input, output, console) =>
{
Stream outputStream = null;
Stream inputStream = null;
try
{
Stream outputStream = null;

if (output == null)
{
outputStream = new MemoryStream();
Expand All @@ -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));
Expand All @@ -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()));
Expand All @@ -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");
Expand All @@ -83,9 +95,10 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand)
});
base64DecCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, IConsole>(async (text, input, output, console) =>
{
Stream outputStream = null;
Stream inputStream = null;
try
{
Stream outputStream = null;
if (output == null)
{
outputStream = new MemoryStream();
Expand All @@ -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));
Expand All @@ -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()));
Expand All @@ -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);
Expand Down
88 changes: 88 additions & 0 deletions Kryptos/Kryptos/WireUpMd5Extensions.cs
Original file line number Diff line number Diff line change
@@ -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<string>("text")
});
md5HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")
{
Argument = new Argument<FileInfo>("input")
});
md5HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")
{
Argument = new Argument<FileInfo>("output")
});
md5HashCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, IConsole>(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;
}
}
}

0 comments on commit 5671a29

Please sign in to comment.