-
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 17, 2020
1 parent
bbe3ae5
commit 071ced5
Showing
5 changed files
with
151 additions
and
5 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,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
namespace Kryptos | ||
{ | ||
public class Base64Url | ||
{ | ||
private static readonly char base64PadCharacter = '='; | ||
private static readonly string doubleBase64PadCharacter = "=="; | ||
private static readonly char base64Character62 = '+'; | ||
private static readonly char base64Character63 = '/'; | ||
private static readonly char base64UrlCharacter62 = '-'; | ||
private static readonly char base64UrlCharacter63 = '_'; | ||
public static string Encode(string text) | ||
{ | ||
return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)) | ||
.Split(base64PadCharacter)[0] | ||
.Replace(base64Character62, base64UrlCharacter62) | ||
.Replace(base64Character63, base64UrlCharacter63); | ||
} | ||
public static string Decode(string text) | ||
{ | ||
text = text.Replace(base64UrlCharacter62, base64Character62) | ||
.Replace(base64UrlCharacter63, base64Character63); | ||
|
||
text += (text.Length % 4) switch | ||
{ | ||
2 => doubleBase64PadCharacter, | ||
3 => base64PadCharacter, | ||
_ => throw new FormatException("Base64Url encoded string is not good."), | ||
}; | ||
|
||
return Encoding.UTF8.GetString(Convert.FromBase64String(text)); | ||
} | ||
} | ||
} |
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,110 @@ | ||
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 WireUpBase64UrlExtensions | ||
{ | ||
public static RootCommand WireUpBase64UrlCommands(this RootCommand rootCommand) | ||
{ | ||
var base64UrlCommand = new Command("base64url", "Base64 URL"); | ||
var base64UrlEncCommand = new Command("encode", "Encode"); | ||
base64UrlEncCommand.AddAlias("enc"); | ||
base64UrlEncCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") | ||
{ | ||
Argument = new Argument<string>("text") | ||
}); | ||
base64UrlEncCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
base64UrlEncCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") | ||
{ | ||
Argument = new Argument<FileInfo>("output") | ||
}); | ||
base64UrlEncCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, IConsole>(async (text, input, output, console) => | ||
{ | ||
try | ||
{ | ||
string base64UrlEncodedText = null; | ||
|
||
if (text != null) | ||
{ | ||
base64UrlEncodedText = Base64Url.Encode(text); | ||
} | ||
if (input != null) | ||
{ | ||
base64UrlEncodedText = Base64Url.Encode(File.ReadAllText(input.FullName)); | ||
} | ||
|
||
if (output == null) | ||
{ | ||
console.Out.WriteLine(base64UrlEncodedText); | ||
} | ||
else | ||
{ | ||
File.WriteAllText(output.FullName, base64UrlEncodedText); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
console.Out.WriteLine(ex.Message); | ||
} | ||
}); | ||
var base64UrlDecCommand = new Command("decode", "Decode"); | ||
base64UrlDecCommand.AddAlias("dec"); | ||
base64UrlDecCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") | ||
{ | ||
Argument = new Argument<string>("text") | ||
}); | ||
base64UrlDecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") | ||
{ | ||
Argument = new Argument<FileInfo>("input") | ||
}); | ||
base64UrlDecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") | ||
{ | ||
Argument = new Argument<FileInfo>("output") | ||
}); | ||
base64UrlDecCommand.Handler = CommandHandler.Create<string, FileInfo, FileInfo, IConsole>(async (text, input, output, console) => | ||
{ | ||
try | ||
{ | ||
string base64UrlDecodedText = null; | ||
|
||
if (text != null) | ||
{ | ||
base64UrlDecodedText = Base64Url.Decode(text); | ||
} | ||
if (input != null) | ||
{ | ||
base64UrlDecodedText = Base64Url.Decode(File.ReadAllText(input.FullName)); | ||
} | ||
|
||
if (output == null) | ||
{ | ||
console.Out.WriteLine(base64UrlDecodedText); | ||
} | ||
else | ||
{ | ||
File.WriteAllText(output.FullName, base64UrlDecodedText); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
console.Out.WriteLine(ex.Message); | ||
} | ||
}); | ||
base64UrlCommand.Add(base64UrlEncCommand); | ||
base64UrlCommand.Add(base64UrlDecCommand); | ||
rootCommand.AddCommand(base64UrlCommand); | ||
|
||
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