Skip to content

Commit

Permalink
Merge pull request #2 from vijayshinva/vnext
Browse files Browse the repository at this point in the history
Vnext - v0.0.7
  • Loading branch information
vijayshinva authored Dec 5, 2020
2 parents a6f2155 + c6a55ce commit 850bb18
Show file tree
Hide file tree
Showing 28 changed files with 352 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: .NET Core

on:
push:
branches: [ main ]
branches: [ main, vnext ]
pull_request:
branches: [ main ]

Expand Down
2 changes: 0 additions & 2 deletions Kryptos/Kryptos/Base64Url.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Kryptos
Expand Down
17 changes: 17 additions & 0 deletions Kryptos/Kryptos/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.CommandLine.Rendering;

namespace Kryptos
{
public static class ConsoleExtensions
{
static TextSpanFormatter Formatter { get; } = new TextSpanFormatter();

public static TextSpan Underline(this string value) => new ContainerSpan(StyleSpan.UnderlinedOn(), new ContentSpan(value), StyleSpan.UnderlinedOff());
public static TextSpan Rgb(this string value, byte r, byte g, byte b) => new ContainerSpan(ForegroundColorSpan.Rgb(r, g, b), new ContentSpan(value), ForegroundColorSpan.Reset());
public static TextSpan LightGreen(this string value) => new ContainerSpan(ForegroundColorSpan.LightGreen(), new ContentSpan(value), ForegroundColorSpan.Reset());
public static TextSpan White(this string value) => new ContainerSpan(ForegroundColorSpan.White(), new ContentSpan(value), ForegroundColorSpan.Reset());
public static TextSpan ToTextSpan(this FormattableString formattableString) => Formatter.ParseToSpan(formattableString);
public static TextSpan ToTextSpan(this object obj) => Formatter.Format(obj);
}
}
24 changes: 24 additions & 0 deletions Kryptos/Kryptos/ConsoleViewZipInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.CommandLine.Rendering;
using System.CommandLine.Rendering.Views;
using System.IO.Compression;

namespace Kryptos
{
class ConsoleViewZipInfo : StackLayoutView
{
protected TextSpanFormatter Formatter { get; } = new TextSpanFormatter();

public ConsoleViewZipInfo(ZipArchive zipArchive)
{
var tableView = new TableView<ZipArchiveEntry>
{
Items = zipArchive.Entries
};
tableView.AddColumn(cellValue: e => e.Name, header: new ContentView("Name".Underline()));
tableView.AddColumn(cellValue: e => e.Length, header: new ContentView("Length".Underline()));
tableView.AddColumn(cellValue: e => e.CompressedLength, header: new ContentView("CompressedLength".Underline()));
tableView.AddColumn(cellValue: e => e.Crc32, header: new ContentView("Crc32".Underline()));
Add(tableView);
}
}
}
4 changes: 1 addition & 3 deletions Kryptos/Kryptos/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text;

namespace Kryptos
{
Expand Down
7 changes: 1 addition & 6 deletions Kryptos/Kryptos/Jwt.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Kryptos
namespace Kryptos
{
public class Jwt
{
Expand Down
5 changes: 3 additions & 2 deletions Kryptos/Kryptos/Kryptos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<Version>0.0.6</Version>
<Version>0.0.7</Version>
<Authors>Vijayshinva Karnure</Authors>
<Description>A .NET core tool for cryptography.</Description>
<Copyright>2020 Vijayshinva Karnure</Copyright>
Expand All @@ -15,7 +15,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20371.2" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.3.0-alpha.20574.7" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions Kryptos/Kryptos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ static async Task<int> Main(string[] args)
.WireUpHmacSha256Commands()
.WireUpHmacSha384Commands()
.WireUpHmacSha512Commands()
.WireUpSriCommands().
WireUpOidCommands();
.WireUpSriCommands()
.WireUpOidCommands()
.WireUpPfxCommands()
.WireUpRngCommands()
.WireUpZipCommands();

return await rootCommand.InvokeAsync(args);
}
Expand Down
17 changes: 4 additions & 13 deletions Kryptos/Kryptos/WireUpBase64Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
Expand Down Expand Up @@ -74,18 +73,10 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand)
});
var base64DecCommand = new Command("decode", "Decode");
base64DecCommand.AddAlias("dec");
base64DecCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text")
{
Argument = new Argument<string>("text")
});
base64DecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")
{
Argument = new Argument<FileInfo>("input")
});
base64DecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")
{
Argument = new Argument<FileInfo>("output")
});
base64DecCommand.AddOption(new Option<string>(new string[] { "--text", "-t" }, "Input Text"));
base64DecCommand.AddOption(new Option<FileInfo>(new string[] { "--input", "-i" }, "Input file path"));
base64DecCommand.AddOption(new Option<FileInfo>(new string[] { "--output", "-o" }, "Output file path"));

base64DecCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, IConsole>(async (text, input, output, console) =>
{
Stream outputStream = null;
Expand Down
3 changes: 0 additions & 3 deletions Kryptos/Kryptos/WireUpBase64UrlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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
{
Expand Down
1 change: 0 additions & 1 deletion Kryptos/Kryptos/WireUpHmacMd5Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
Expand Down
1 change: 0 additions & 1 deletion Kryptos/Kryptos/WireUpHmacSha1Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
Expand Down
1 change: 0 additions & 1 deletion Kryptos/Kryptos/WireUpHmacSha256Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
Expand Down
1 change: 0 additions & 1 deletion Kryptos/Kryptos/WireUpHmacSha384Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
Expand Down
1 change: 0 additions & 1 deletion Kryptos/Kryptos/WireUpHmacSha512Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
Expand Down
3 changes: 0 additions & 3 deletions Kryptos/Kryptos/WireUpJwtExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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
{
Expand Down
1 change: 0 additions & 1 deletion Kryptos/Kryptos/WireUpMd5Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
Expand Down
3 changes: 0 additions & 3 deletions Kryptos/Kryptos/WireUpOidExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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
{
Expand Down
174 changes: 174 additions & 0 deletions Kryptos/Kryptos/WireUpPfxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Kryptos
{
public static class WireUpPfxExtensions
{
public static RootCommand WireUpPfxCommands(this RootCommand rootCommand)
{
var pfxCommand = new Command("pfx", "PFX, PKCS #12");
var pfxEncCommand = new Command("encrypt", "Encrypt");
pfxEncCommand.AddAlias("enc");
pfxEncCommand.AddOption(new Option<string>(new string[] { "--text", "-t" }, "Input Text"));
pfxEncCommand.AddOption(new Option<FileInfo>(new string[] { "--input", "-i" }, "Input file path"));
pfxEncCommand.AddOption(new Option<FileInfo>(new string[] { "--output", "-o" }, "Output file path"));
pfxEncCommand.AddOption(new Option<FileInfo>(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)"));
pfxEncCommand.AddOption(new Option<string>(new string[] { "--keytext", "-kt" }, "Key Text"));

pfxEncCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, FileInfo, string, IConsole>(async (text, input, output, certInput, keyText, console) =>
{
try
{
if (input != null)
{
text = await File.ReadAllTextAsync(input.FullName).ConfigureAwait(false);
}

using var certificate = new X509Certificate2(certInput.FullName, keyText);
using var rsa = certificate.GetRSAPublicKey();
var encryptedText = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.Pkcs1);

if (output == null)
{
console.Out.WriteLine(Convert.ToBase64String(encryptedText));
}
else
{
var outputStream = output.OpenWrite();
await outputStream.WriteAsync(encryptedText).ConfigureAwait(false);
}
}
catch (Exception ex)
{
console.Out.WriteLine(ex.Message);
return 22;
}
return 0;
});

var pfxDecCommand = new Command("decrypt", "Decrypt");
pfxDecCommand.AddAlias("dec");
pfxDecCommand.AddOption(new Option<string>(new string[] { "--text", "-t" }, "Input Text"));
pfxDecCommand.AddOption(new Option<FileInfo>(new string[] { "--input", "-i" }, "Input file path"));
pfxDecCommand.AddOption(new Option<FileInfo>(new string[] { "--output", "-o" }, "Output file path"));
pfxDecCommand.AddOption(new Option<FileInfo>(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)"));
pfxDecCommand.AddOption(new Option<string>(new string[] { "--keytext", "-kt" }, "Key Text"));

pfxDecCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, FileInfo, string, IConsole>(async (text, input, output, certInput, keyText, console) =>
{
try
{
using var certificate = new X509Certificate2(certInput.FullName, keyText, X509KeyStorageFlags.EphemeralKeySet);
using var rsa = certificate.GetRSAPrivateKey();

var textBytes = Convert.FromBase64String(text);
if (input != null)
{
textBytes = await File.ReadAllBytesAsync(input.FullName).ConfigureAwait(false);
}
var decryptedText = rsa.Decrypt(textBytes, RSAEncryptionPadding.Pkcs1);
if (output == null)
{
console.Out.WriteLine(Encoding.UTF8.GetString(decryptedText));
}
else
{
using var outputStream = output.OpenWrite();
await outputStream.WriteAsync(decryptedText).ConfigureAwait(false);
}
}
catch (Exception ex)
{
console.Out.WriteLine(ex.Message);
return 22;
}
return 0;
});

var pfxInfoCommand = new Command("info", "Information");
pfxInfoCommand.AddAlias("info");
pfxInfoCommand.AddOption(new Option<FileInfo>(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)"));
pfxInfoCommand.AddOption(new Option<string>(new string[] { "--keytext", "-kt" }, "Key Text"));

pfxInfoCommand.Handler = CommandHandler.Create<FileInfo, string, IConsole>((certInput, keyText, console) =>
{
try
{
using var certificate = new X509Certificate2(certInput.FullName, keyText);
console.Out.WriteLine($"Thumbprint\t: {certificate.Thumbprint}");
console.Out.WriteLine($"Subject\t\t: {certificate.Subject}");
console.Out.WriteLine($"FriendlyName\t: {certificate.FriendlyName}");
console.Out.WriteLine($"Issuer\t\t: {certificate.Issuer}");
console.Out.WriteLine($"NotAfter\t: {certificate.NotAfter:dd-MMM-yyyy}");
console.Out.WriteLine($"NotBefore\t: {certificate.NotBefore:dd-MMM-yyyy}");
console.Out.WriteLine($"HasPrivateKey\t: {certificate.HasPrivateKey}");
console.Out.WriteLine($"PublicKey\t: {certificate.PublicKey.Oid.FriendlyName} ({certificate.PublicKey.Oid.Value})");
console.Out.WriteLine($"Signature\t: {certificate.SignatureAlgorithm.FriendlyName} ({certificate.SignatureAlgorithm.Value})");
console.Out.WriteLine($"SerialNumber\t: {certificate.SerialNumber}");
console.Out.WriteLine($"Version\t\t: V{certificate.Version}");
}
catch (Exception ex)
{
console.Out.WriteLine(ex.Message);
return 22;
}
return 0;
});

var pfxCreateCommand = new Command("create", "Create Self Signed Certificate");
pfxCreateCommand.AddOption(new Option<string>(new string[] { "--subjecttext", "-st" }, "Subject Text"));
pfxCreateCommand.AddOption(new Option<string>(new string[] { "--keytext", "-kt" }, "Key Text"));
pfxCreateCommand.AddOption(new Option<FileInfo>(new string[] { "--output", "-o" }, "Output file path"));

pfxCreateCommand.Handler = CommandHandler.Create<string, string, FileInfo, IConsole>((subjectText, keyText, output, console) =>
{
try
{
using var rsa = RSA.Create(3072);
var certificateRequest = new CertificateRequest($"CN={subjectText}", rsa, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
var subjectAlternativeNameBuilder = new SubjectAlternativeNameBuilder();
subjectAlternativeNameBuilder.AddIpAddress(IPAddress.Loopback);
subjectAlternativeNameBuilder.AddIpAddress(IPAddress.IPv6Loopback);
subjectAlternativeNameBuilder.AddDnsName("localhost");
certificateRequest.CertificateExtensions.Add(subjectAlternativeNameBuilder.Build());

var certificate = certificateRequest.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(3));
certificate.FriendlyName = subjectText;

if (output == null)
{
console.Out.WriteLine("-----BEGIN CERTIFICATE-----\r\n");
console.Out.WriteLine(Convert.ToBase64String(certificate.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
console.Out.WriteLine("\r\n-----END CERTIFICATE-----");
}
else
{
File.WriteAllBytesAsync(output.FullName, certificate.Export(X509ContentType.Pfx, keyText));
}
}
catch (Exception ex)
{
console.Out.WriteLine(ex.Message);
return 22;
}
return 0;
});

pfxCommand.Add(pfxEncCommand);
pfxCommand.Add(pfxDecCommand);
pfxCommand.Add(pfxInfoCommand);
pfxCommand.Add(pfxCreateCommand);
rootCommand.AddCommand(pfxCommand);

return rootCommand;
}
}
}
Loading

0 comments on commit 850bb18

Please sign in to comment.