You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
43 lines
1.2 KiB
using System;
|
|
using System.Text;
|
|
|
|
namespace Infrastructure.Extensions
|
|
{
|
|
public static class Base64Extensions
|
|
{
|
|
public static string Base64UrlEncode(this string input)
|
|
{
|
|
if (input is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(input));
|
|
}
|
|
|
|
var output = Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
|
|
output = output.Split('=')[0];
|
|
output = output.Replace('+', '-');
|
|
output = output.Replace('/', '_');
|
|
return output;
|
|
}
|
|
|
|
public static string Base64UrlDecode(this string input)
|
|
{
|
|
if (input is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(input));
|
|
}
|
|
|
|
var output = input;
|
|
output = output.Replace('-', '+');
|
|
output = output.Replace('_', '/');
|
|
switch (output.Length % 4)
|
|
{
|
|
case 0: break;
|
|
case 2: output += "=="; break;
|
|
case 3: output += "="; break;
|
|
default: break;
|
|
}
|
|
var converted = Convert.FromBase64String(output);
|
|
return Encoding.UTF8.GetString(converted);
|
|
}
|
|
}
|
|
} |