forked from linguanostra/GoogleMapsAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Utils.cs
More file actions
75 lines (60 loc) · 2.46 KB
/
Copy pathBase64Utils.cs
File metadata and controls
75 lines (60 loc) · 2.46 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Text;
namespace GoogleMapsAPI.NET.Utils
{
/// <summary>
/// Base-64 utils
/// </summary>
public class Base64Utils
{
#region Static methods
/// <summary>
/// Encode string s using the URL- and filesystem-safe alphabet, which substitutes - instead of + and _ instead of
/// / in the standard Base64 alphabet. The result can still contain =.
/// See: https://docs.python.org/2/library/base64.html#base64.urlsafe_b64encode
/// </summary>
/// <param name="value">Value to encode</param>
/// <returns>Result</returns>
public static string UrlSafeBase64Encode(string value)
{
return UrlSafeBase64Encode(Encoding.ASCII.GetBytes(value));
}
/// <summary>
/// Encode string s using the URL- and filesystem-safe alphabet, which substitutes - instead of + and _ instead of
/// / in the standard Base64 alphabet. The result can still contain =.
/// See: https://docs.python.org/2/library/base64.html
/// http://stackoverflow.com/questions/26353710/how-to-achieve-base64-url-safe-encoding-in-c
/// </summary>
/// <param name="valueBytes">Value bytes to encode</param>
/// <returns>Result</returns>
public static string UrlSafeBase64Encode(byte[] valueBytes)
{
return Convert.ToBase64String(valueBytes)
.Replace('+', '-').Replace('/', '_');
}
/// <summary>
/// Decode string s using the URL- and filesystem-safe alphabet, which substitutes - instead of + and _ instead
/// of / in the standard Base64 alphabet.
/// See: https://docs.python.org/2/library/base64.html
/// http://stackoverflow.com/questions/26353710/how-to-achieve-base64-url-safe-encoding-in-c
/// </summary>
/// <param name="value">Value to decode</param>
/// <returns>Result</returns>
public static string UrlSafeBase64Decode(string value)
{
var incoming = value.Replace('_', '/').Replace('-', '+');
switch (value.Length%4)
{
case 2:
incoming += "==";
break;
case 3:
incoming += "=";
break;
}
byte[] bytes = Convert.FromBase64String(incoming);
return Encoding.ASCII.GetString(bytes);
}
#endregion
}
}