Skip to content

Commit

Permalink
HMAC
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayshinva committed Nov 18, 2020
1 parent b8d018e commit 13717ac
Show file tree
Hide file tree
Showing 8 changed files with 522 additions and 3 deletions.
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.4</Version>
<Version>0.0.5</Version>
<Authors>Vijayshinva Karnure</Authors>
<Description>A .NET core tool for cryptography.</Description>
<Copyright>2020 Vijayshinva Karnure</Copyright>
Expand Down
7 changes: 6 additions & 1 deletion Kryptos/Kryptos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ static async Task<int> Main(string[] args)
.WireUpSha256Commands()
.WireUpSha384Commands()
.WireUpSha512Commands()
.WireUpJwtCommands();
.WireUpJwtCommands()
.WireUpHmacMd5Commands()
.WireUpHmacSha1Commands()
.WireUpHmacSha256Commands()
.WireUpHmacSha384Commands()
.WireUpHmacSha512Commands();

return await rootCommand.InvokeAsync(args);
}
Expand Down
101 changes: 101 additions & 0 deletions Kryptos/Kryptos/WireUpHmacMd5Extensions.cs
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;
}
}
}
101 changes: 101 additions & 0 deletions Kryptos/Kryptos/WireUpHmacSha1Extensions.cs
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;
}
}
}
101 changes: 101 additions & 0 deletions Kryptos/Kryptos/WireUpHmacSha256Extensions.cs
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;
}
}
}
Loading

0 comments on commit 13717ac

Please sign in to comment.