-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64UrlHelper.cs
More file actions
34 lines (30 loc) · 935 Bytes
/
Copy pathBase64UrlHelper.cs
File metadata and controls
34 lines (30 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Text;
namespace WebAuthn_Client_.NET
{
public static class Base64UrlHelper
{
public static string Encode(byte[] input)
{
var base64 = Convert.ToBase64String(input);
return base64.TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
public static byte[] Decode(string input)
{
var base64 = input.Replace('-', '+').Replace('_', '/');
switch (base64.Length % 4)
{
case 2: base64 += "=="; break;
case 3: base64 += "="; break;
}
return Convert.FromBase64String(base64);
}
public static string EncodeString(string input)
{
return Encode(Encoding.UTF8.GetBytes(input));
}
public static string DecodeString(string input)
{
return Encoding.UTF8.GetString(Decode(input));
}
}
}