|
3 | 3 |
|
4 | 4 | namespace OpenSourceToolkit.Converters |
5 | 5 | { |
| 6 | + /// <summary> |
| 7 | + /// Provides helpers for Base64 and URL-safe Base64 text conversion. |
| 8 | + /// </summary> |
6 | 9 | public static class Base64Converter |
7 | 10 | { |
| 11 | + /// <summary> |
| 12 | + /// Encodes text as a Base64 string. |
| 13 | + /// </summary> |
| 14 | + /// <param name="text">The text to encode.</param> |
| 15 | + /// <param name="encoding">The text encoding to use, or <c>null</c> to use UTF-8.</param> |
| 16 | + /// <returns>The Base64-encoded text, or <c>null</c> when <paramref name="text"/> is <c>null</c>.</returns> |
8 | 17 | public static string Encode(string text, Encoding encoding = null) |
9 | 18 | { |
10 | 19 | if (text == null) return null; |
11 | 20 | var bytes = (encoding ?? Encoding.UTF8).GetBytes(text); |
12 | 21 | return Convert.ToBase64String(bytes); |
13 | 22 | } |
14 | 23 |
|
| 24 | + /// <summary> |
| 25 | + /// Decodes a Base64 string to text. |
| 26 | + /// </summary> |
| 27 | + /// <param name="base64">The Base64 string to decode.</param> |
| 28 | + /// <param name="encoding">The text encoding to use, or <c>null</c> to use UTF-8.</param> |
| 29 | + /// <returns>The decoded text, or <c>null</c> when <paramref name="base64"/> is <c>null</c>.</returns> |
15 | 30 | public static string Decode(string base64, Encoding encoding = null) |
16 | 31 | { |
17 | 32 | if (base64 == null) return null; |
18 | 33 | var bytes = Convert.FromBase64String(base64); |
19 | 34 | return (encoding ?? Encoding.UTF8).GetString(bytes); |
20 | 35 | } |
21 | 36 |
|
| 37 | + /// <summary> |
| 38 | + /// Encodes text as a URL-safe Base64 string without padding. |
| 39 | + /// </summary> |
| 40 | + /// <param name="text">The text to encode.</param> |
| 41 | + /// <param name="encoding">The text encoding to use, or <c>null</c> to use UTF-8.</param> |
| 42 | + /// <returns>The URL-safe Base64-encoded text.</returns> |
22 | 43 | public static string EncodeUrlSafe(string text, Encoding encoding = null) |
23 | 44 | { |
24 | 45 | var base64 = Encode(text, encoding); |
25 | 46 | return base64.Replace("+", "-").Replace("/", "_").TrimEnd('='); |
26 | 47 | } |
27 | 48 |
|
| 49 | + /// <summary> |
| 50 | + /// Decodes a URL-safe Base64 string to text. |
| 51 | + /// </summary> |
| 52 | + /// <param name="base64Url">The URL-safe Base64 string to decode.</param> |
| 53 | + /// <param name="encoding">The text encoding to use, or <c>null</c> to use UTF-8.</param> |
| 54 | + /// <returns>The decoded text.</returns> |
28 | 55 | public static string DecodeUrlSafe(string base64Url, Encoding encoding = null) |
29 | 56 | { |
30 | 57 | var base64 = base64Url.Replace("-", "+").Replace("_", "/"); |
|
0 commit comments