Skip to content

Instantly share code, notes, and snippets.

@johnallers
Created December 12, 2018 21:49
Show Gist options
  • Save johnallers/a3155b808ff495505bcb712fe4fda0f5 to your computer and use it in GitHub Desktop.
Save johnallers/a3155b808ff495505bcb712fe4fda0f5 to your computer and use it in GitHub Desktop.
// https://tools.ietf.org/html/rfc4648#section-5
public class Base64Url
{
public static string Encode(byte[] inArray)
{
var base64 = Convert.ToBase64String(inArray);
return base64
.Replace("+", "-")
.Replace("/", "_")
.Replace("=", "");
}
public static byte[] Decode(string s)
{
var sb = new StringBuilder(s);
sb.Replace("-", "+");
sb.Replace("_", "/");
while (sb.Length * 6 % 8 != 0)
{
sb.Append("=");
}
return Convert.FromBase64String(sb.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment