-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vijayshinva
committed
Nov 15, 2020
1 parent
5d13ac3
commit 5671a29
Showing
5 changed files
with
141 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |