| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace Azure.Identity |
| | | 8 | | { |
| | | 9 | | internal static class Base64Url |
| | | 10 | | { |
| | | 11 | | public static byte[] Decode(string str) |
| | | 12 | | { |
| | 0 | 13 | | str = new StringBuilder(str).Replace('-', '+').Replace('_', '/').Append('=', (str.Length % 4 == 0) ? 0 : 4 - |
| | | 14 | | |
| | 0 | 15 | | return Convert.FromBase64String(str); |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public static string Encode(byte[] bytes) |
| | | 19 | | { |
| | 0 | 20 | | return new StringBuilder(Convert.ToBase64String(bytes)).Replace('+', '-').Replace('/', '_').Replace("=", "") |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | |
| | | 24 | | public static string HexToBase64Url(string hex) |
| | | 25 | | { |
| | 0 | 26 | | byte[] bytes = new byte[hex.Length / 2]; |
| | | 27 | | |
| | 0 | 28 | | for (int i = 0; i < hex.Length; i += 2) |
| | 0 | 29 | | bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); |
| | | 30 | | |
| | 0 | 31 | | return Base64Url.Encode(bytes); |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | } |