Skip to content

Commit

Permalink
Base64Url
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayshinva committed Nov 17, 2020
1 parent bbe3ae5 commit 071ced5
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 5 deletions.
38 changes: 38 additions & 0 deletions Kryptos/Kryptos/Base64Url.cs
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));
}
}
}
4 changes: 2 additions & 2 deletions Kryptos/Kryptos/Kryptos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>0.0.2</Version>
<Version>0.0.3</Version>
<Authors>Vijayshinva Karnure</Authors>
<Description>A .NET core tool for cryptography.</Description>
<Copyright>(c) 2020 Vijayshinva Karnure</Copyright>
<Copyright>2020 Vijayshinva Karnure</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/vijayshinva/kryptos</PackageProjectUrl>
<RepositoryUrl>https://github.com/vijayshinva/kryptos</RepositoryUrl>
Expand Down
1 change: 1 addition & 0 deletions Kryptos/Kryptos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static async Task<int> Main(string[] args)

rootCommand.WireUpUuidCommands()
.WireUpBase64Commands()
.WireUpBase64UrlCommands()
.WireUpMd5Commands()
.WireUpSha1Commands()
.WireUpSha256Commands()
Expand Down
110 changes: 110 additions & 0 deletions Kryptos/Kryptos/WireUpBase64UrlExtensions.cs
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;
}
}
}
3 changes: 0 additions & 3 deletions Kryptos/Kryptos/WireUpUuidExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public static RootCommand WireUpUuidCommands(this RootCommand rootCommand)
{
console.Out.WriteLine(ex.Message);
}
finally
{
}
});

rootCommand.AddCommand(guidCommand);
Expand Down

0 comments on commit 071ced5

Please sign in to comment.