-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebEncoder.cs
More file actions
33 lines (31 loc) · 1.13 KB
/
WebEncoder.cs
File metadata and controls
33 lines (31 loc) · 1.13 KB
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
using System;
namespace LogicSoftware.WebPushEncryption
{
class WebEncoder
{
public static byte[] Base64UrlDecode(string val)
{
string s = val;
s = s.Replace('-', '+'); // 62nd char of encoding
s = s.Replace('_', '/'); // 63rd char of encoding
switch (s.Length % 4) // Pad with trailing '='s
{
case 0: break; // No pad chars in this case
case 2: s += "=="; break; // Two pad chars
case 3: s += "="; break; // One pad char
default:
throw new System.Exception(
"Illegal base64url string!");
}
return Convert.FromBase64String(s); // Standard base64 decoder
}
public static string Base64UrlEncode(byte[] val)
{
string s = Convert.ToBase64String(val); // Regular base64 encoder
s = s.Split('=')[0]; // Remove any trailing '='s
s = s.Replace('+', '-'); // 62nd char of encoding
s = s.Replace('/', '_'); // 63rd char of encoding
return s;
}
}
}