diff --git a/.github/workflows/dotnet-core.yml b/.github/workflows/dotnet-core.yml index 2d6e413..9440306 100644 --- a/.github/workflows/dotnet-core.yml +++ b/.github/workflows/dotnet-core.yml @@ -2,7 +2,7 @@ name: .NET Core on: push: - branches: [ main ] + branches: [ main, vnext ] pull_request: branches: [ main ] diff --git a/Kryptos/Kryptos/Base64Url.cs b/Kryptos/Kryptos/Base64Url.cs index a206115..20ea114 100644 --- a/Kryptos/Kryptos/Base64Url.cs +++ b/Kryptos/Kryptos/Base64Url.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Diagnostics; using System.Text; namespace Kryptos diff --git a/Kryptos/Kryptos/ConsoleExtensions.cs b/Kryptos/Kryptos/ConsoleExtensions.cs new file mode 100644 index 0000000..2aa7d91 --- /dev/null +++ b/Kryptos/Kryptos/ConsoleExtensions.cs @@ -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); + } +} diff --git a/Kryptos/Kryptos/ConsoleViewZipInfo.cs b/Kryptos/Kryptos/ConsoleViewZipInfo.cs new file mode 100644 index 0000000..cd410b0 --- /dev/null +++ b/Kryptos/Kryptos/ConsoleViewZipInfo.cs @@ -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 + { + 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); + } + } +} diff --git a/Kryptos/Kryptos/Extensions.cs b/Kryptos/Kryptos/Extensions.cs index 7870ec7..b8a7bf7 100644 --- a/Kryptos/Kryptos/Extensions.cs +++ b/Kryptos/Kryptos/Extensions.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Text; namespace Kryptos { diff --git a/Kryptos/Kryptos/Jwt.cs b/Kryptos/Kryptos/Jwt.cs index 2f4a509..8a523e1 100644 --- a/Kryptos/Kryptos/Jwt.cs +++ b/Kryptos/Kryptos/Jwt.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Text; - -namespace Kryptos +namespace Kryptos { public class Jwt { diff --git a/Kryptos/Kryptos/Kryptos.csproj b/Kryptos/Kryptos/Kryptos.csproj index c5e0fb4..3b47e9f 100644 --- a/Kryptos/Kryptos/Kryptos.csproj +++ b/Kryptos/Kryptos/Kryptos.csproj @@ -3,7 +3,7 @@ Exe netcoreapp3.1;net5.0 - 0.0.6 + 0.0.7 Vijayshinva Karnure A .NET core tool for cryptography. 2020 Vijayshinva Karnure @@ -15,7 +15,8 @@ - + + diff --git a/Kryptos/Kryptos/Program.cs b/Kryptos/Kryptos/Program.cs index eec838a..d6ac5c6 100644 --- a/Kryptos/Kryptos/Program.cs +++ b/Kryptos/Kryptos/Program.cs @@ -27,8 +27,11 @@ static async Task Main(string[] args) .WireUpHmacSha256Commands() .WireUpHmacSha384Commands() .WireUpHmacSha512Commands() - .WireUpSriCommands(). - WireUpOidCommands(); + .WireUpSriCommands() + .WireUpOidCommands() + .WireUpPfxCommands() + .WireUpRngCommands() + .WireUpZipCommands(); return await rootCommand.InvokeAsync(args); } diff --git a/Kryptos/Kryptos/WireUpBase64Extensions.cs b/Kryptos/Kryptos/WireUpBase64Extensions.cs index 6310746..6245ac1 100644 --- a/Kryptos/Kryptos/WireUpBase64Extensions.cs +++ b/Kryptos/Kryptos/WireUpBase64Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; @@ -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("text") - }); - base64DecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") - { - Argument = new Argument("input") - }); - base64DecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") - { - Argument = new Argument("output") - }); + base64DecCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text")); + base64DecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")); + base64DecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + base64DecCommand.Handler = CommandHandler.Create(async (text, input, output, console) => { Stream outputStream = null; diff --git a/Kryptos/Kryptos/WireUpBase64UrlExtensions.cs b/Kryptos/Kryptos/WireUpBase64UrlExtensions.cs index f2dabeb..155a617 100644 --- a/Kryptos/Kryptos/WireUpBase64UrlExtensions.cs +++ b/Kryptos/Kryptos/WireUpBase64UrlExtensions.cs @@ -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 { diff --git a/Kryptos/Kryptos/WireUpHmacMd5Extensions.cs b/Kryptos/Kryptos/WireUpHmacMd5Extensions.cs index 2d95c57..67ac280 100644 --- a/Kryptos/Kryptos/WireUpHmacMd5Extensions.cs +++ b/Kryptos/Kryptos/WireUpHmacMd5Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpHmacSha1Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha1Extensions.cs index dc8bef1..6d00c71 100644 --- a/Kryptos/Kryptos/WireUpHmacSha1Extensions.cs +++ b/Kryptos/Kryptos/WireUpHmacSha1Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpHmacSha256Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha256Extensions.cs index b4a7f91..eaadf27 100644 --- a/Kryptos/Kryptos/WireUpHmacSha256Extensions.cs +++ b/Kryptos/Kryptos/WireUpHmacSha256Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpHmacSha384Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha384Extensions.cs index aa3b5a5..895c0ad 100644 --- a/Kryptos/Kryptos/WireUpHmacSha384Extensions.cs +++ b/Kryptos/Kryptos/WireUpHmacSha384Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpHmacSha512Extensions.cs b/Kryptos/Kryptos/WireUpHmacSha512Extensions.cs index 47b0085..8742eba 100644 --- a/Kryptos/Kryptos/WireUpHmacSha512Extensions.cs +++ b/Kryptos/Kryptos/WireUpHmacSha512Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpJwtExtensions.cs b/Kryptos/Kryptos/WireUpJwtExtensions.cs index c649b7a..a871848 100644 --- a/Kryptos/Kryptos/WireUpJwtExtensions.cs +++ b/Kryptos/Kryptos/WireUpJwtExtensions.cs @@ -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 { diff --git a/Kryptos/Kryptos/WireUpMd5Extensions.cs b/Kryptos/Kryptos/WireUpMd5Extensions.cs index f9823e3..6afd950 100644 --- a/Kryptos/Kryptos/WireUpMd5Extensions.cs +++ b/Kryptos/Kryptos/WireUpMd5Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpOidExtensions.cs b/Kryptos/Kryptos/WireUpOidExtensions.cs index 740d778..980e406 100644 --- a/Kryptos/Kryptos/WireUpOidExtensions.cs +++ b/Kryptos/Kryptos/WireUpOidExtensions.cs @@ -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 { diff --git a/Kryptos/Kryptos/WireUpPfxExtensions.cs b/Kryptos/Kryptos/WireUpPfxExtensions.cs new file mode 100644 index 0000000..24e25ec --- /dev/null +++ b/Kryptos/Kryptos/WireUpPfxExtensions.cs @@ -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(new string[] { "--text", "-t" }, "Input Text")); + pfxEncCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")); + pfxEncCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + pfxEncCommand.AddOption(new Option(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)")); + pfxEncCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text")); + + pfxEncCommand.Handler = CommandHandler.Create(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(new string[] { "--text", "-t" }, "Input Text")); + pfxDecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")); + pfxDecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + pfxDecCommand.AddOption(new Option(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)")); + pfxDecCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text")); + + pfxDecCommand.Handler = CommandHandler.Create(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(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)")); + pfxInfoCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text")); + + pfxInfoCommand.Handler = CommandHandler.Create((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(new string[] { "--subjecttext", "-st" }, "Subject Text")); + pfxCreateCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text")); + pfxCreateCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + + pfxCreateCommand.Handler = CommandHandler.Create((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; + } + } +} diff --git a/Kryptos/Kryptos/WireUpRngExtensions.cs b/Kryptos/Kryptos/WireUpRngExtensions.cs new file mode 100644 index 0000000..048ea4a --- /dev/null +++ b/Kryptos/Kryptos/WireUpRngExtensions.cs @@ -0,0 +1,50 @@ +using System; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.Security.Cryptography; + +namespace Kryptos +{ + public static class WireUpRngExtensions + { + public static RootCommand WireUpRngCommands(this RootCommand rootCommand) + { + var rngCommand = new Command("rng", "Random Number Generator"); + rngCommand.AddOption(new Option(new string[] { "--max", "-m" }, "Maximum (exclusive)")); + rngCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + + rngCommand.Handler = CommandHandler.Create(async (max, output, console) => + { + try + { + if (max <= 0) + { + max = int.MaxValue; + } + var random = RandomNumberGenerator.GetInt32(max); + + if (output == null) + { + console.Out.WriteLine(random.ToString()); + } + else + { + await File.WriteAllTextAsync(output.FullName, random.ToString()).ConfigureAwait(false); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + return 22; + } + return 0; + }); + + rootCommand.AddCommand(rngCommand); + + return rootCommand; + } + } +} diff --git a/Kryptos/Kryptos/WireUpSha1Extensions.cs b/Kryptos/Kryptos/WireUpSha1Extensions.cs index 9ddc2d4..c806ac1 100644 --- a/Kryptos/Kryptos/WireUpSha1Extensions.cs +++ b/Kryptos/Kryptos/WireUpSha1Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpSha256Extensions.cs b/Kryptos/Kryptos/WireUpSha256Extensions.cs index 369a2d9..d3646e5 100644 --- a/Kryptos/Kryptos/WireUpSha256Extensions.cs +++ b/Kryptos/Kryptos/WireUpSha256Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpSha384Extensions.cs b/Kryptos/Kryptos/WireUpSha384Extensions.cs index 977a5ed..301ffd8 100644 --- a/Kryptos/Kryptos/WireUpSha384Extensions.cs +++ b/Kryptos/Kryptos/WireUpSha384Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpSha512Extensions.cs b/Kryptos/Kryptos/WireUpSha512Extensions.cs index d5653a1..ab48f65 100644 --- a/Kryptos/Kryptos/WireUpSha512Extensions.cs +++ b/Kryptos/Kryptos/WireUpSha512Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpSriExtensions.cs b/Kryptos/Kryptos/WireUpSriExtensions.cs index 54f84da..41b26f6 100644 --- a/Kryptos/Kryptos/WireUpSriExtensions.cs +++ b/Kryptos/Kryptos/WireUpSriExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; diff --git a/Kryptos/Kryptos/WireUpUuidExtensions.cs b/Kryptos/Kryptos/WireUpUuidExtensions.cs index 4c015c6..9a5f1e6 100644 --- a/Kryptos/Kryptos/WireUpUuidExtensions.cs +++ b/Kryptos/Kryptos/WireUpUuidExtensions.cs @@ -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 { diff --git a/Kryptos/Kryptos/WireUpZipExtensions.cs b/Kryptos/Kryptos/WireUpZipExtensions.cs new file mode 100644 index 0000000..073dd48 --- /dev/null +++ b/Kryptos/Kryptos/WireUpZipExtensions.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.IO.Compression; +using System.CommandLine.Rendering; + +namespace Kryptos +{ + public static class WireUpZipExtensions + { + public static RootCommand WireUpZipCommands(this RootCommand rootCommand) + { + var zipCommand = new Command("zip", "Zip Archive"); + + var infoCommand = new Command("info", "Zip File Information"); + infoCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")); + infoCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + + infoCommand.Handler = CommandHandler.Create(async (input, output, console) => + { + try + { + using var stream = input.OpenRead(); + using var zip = new ZipArchive(stream); + + if (output == null) + { + console.Append(new ConsoleViewZipInfo(zip)); + } + else + { + var response = new List(new string[] { "Name,Length,CompressedLength,Crc32" }); + foreach (var item in zip.Entries) + { + response.Add($"{item.Name},{item.Length},{item.CompressedLength},{item.Crc32}"); + } + await File.WriteAllLinesAsync(output.FullName, response).ConfigureAwait(false); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + return 22; + } + return 0; + }); + + zipCommand.Add(infoCommand); + rootCommand.AddCommand(zipCommand); + + return rootCommand; + } + } +} diff --git a/README.md b/README.md index a022778..ef4c74d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,11 @@ A .NET core tool for cryptography. - JWT decoding - HMAC-SHA1, HMAC-SHA256, HMAC-SHA384, HMAC-SHA512, HMAC-MD5 - Subresource Integrity +- Cryptographic Object Identifier lookup +- Create self-signed certificates (PFX) +- Encryption and decryption using PFX +- Random Number Generator +- Zip file content information ## Installation @@ -70,11 +75,19 @@ kryptos --help ``` kryptos oid -t 1.3.6.1.5.5.7.3.1 ``` - +6. Generate a self-signed PFX certificate + ``` + kryptos pfx create -st www.contoso.com -kt password -o contoso.pfx + ``` +7. Encrypt text using a PFX certificate + ``` + kryptos pfx enc -ci contoso.pfx -kt password -t "The quick brown fox jumps over the lazy dog." + ``` + ## Contributing - Fork the repo on [GitHub][git-repo] - Clone the project to your own machine -- Commit changes to your own branch +- Commit changes to ```vnext``` branch - Push your work back up to your fork - Be sure to pull the latest from "upstream" before making a pull request! - Submit a Pull Request so that changes can be reviewed and merged