-
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 18, 2020
1 parent
b8d018e
commit 13717ac
Showing
8 changed files
with
522 additions
and
3 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
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,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<string>("text") | ||
}); | ||
hmacmd5HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
hmacmd5HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") | ||
{ | ||
Argument = new Argument<string>("text") | ||
}); | ||
hmacmd5HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
hmacmd5HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") | ||
{ | ||
Argument = new Argument<FileInfo>("output") | ||
}); | ||
hmacmd5HashCommand.Handler = CommandHandler.Create<string, FileInfo, string, FileInfo, FileInfo, IConsole>(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; | ||
} | ||
} | ||
} |
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,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<string>("text") | ||
}); | ||
hmacsha1HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
hmacsha1HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") | ||
{ | ||
Argument = new Argument<string>("text") | ||
}); | ||
hmacsha1HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
hmacsha1HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") | ||
{ | ||
Argument = new Argument<FileInfo>("output") | ||
}); | ||
hmacsha1HashCommand.Handler = CommandHandler.Create<string, FileInfo, string, FileInfo, FileInfo, IConsole>(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; | ||
} | ||
} | ||
} |
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,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<string>("text") | ||
}); | ||
hmacsha256HashCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
hmacsha256HashCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text") | ||
{ | ||
Argument = new Argument<string>("text") | ||
}); | ||
hmacsha256HashCommand.AddOption(new Option(new string[] { "--keyinput", "-ki" }, "Key file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
hmacsha256HashCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") | ||
{ | ||
Argument = new Argument<FileInfo>("output") | ||
}); | ||
hmacsha256HashCommand.Handler = CommandHandler.Create<string, FileInfo, string, FileInfo, FileInfo, IConsole>(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; | ||
} | ||
} | ||
} |
Oops, something went wrong.