|
| 1 | +// <copyright file="Base64UrlEncoder.cs" company="WebDriver Committers"> |
| 2 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The SFC licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// </copyright> |
| 18 | + |
| 19 | +using System; |
| 20 | + |
| 21 | +namespace OpenQA.Selenium.Internal |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Encodes and Decodes strings as Base64Url encoding. |
| 25 | + /// </summary> |
| 26 | + internal static class Base64UrlEncoder |
| 27 | + { |
| 28 | + private const char base64PadCharacter = '='; |
| 29 | + private const string doubleBase64PadCharacter = "=="; |
| 30 | + private const char base64Character62 = '+'; |
| 31 | + private const char base64Character63 = '/'; |
| 32 | + private const char base64UrlCharacter62 = '-'; |
| 33 | + private const char base64UrlCharacter63 = '_'; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Encoding table |
| 37 | + /// </summary> |
| 38 | + internal static readonly char[] s_base64Table = |
| 39 | + { |
| 40 | + 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', |
| 41 | + 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', |
| 42 | + '0','1','2','3','4','5','6','7','8','9', |
| 43 | + base64UrlCharacter62, |
| 44 | + base64UrlCharacter63 |
| 45 | + }; |
| 46 | + |
| 47 | + // https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.19.0/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs#L85 |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation which is encoded with base-64-url digits. |
| 51 | + /// </summary> |
| 52 | + /// <param name="inArray">An array of 8-bit unsigned integers.</param> |
| 53 | + /// <returns>The string representation in base 64 url encoding of length elements of inArray, starting at position offset.</returns> |
| 54 | + /// <exception cref="ArgumentNullException">'inArray' is null.</exception> |
| 55 | + public static string Encode(byte[] inArray) |
| 56 | + { |
| 57 | + _ = inArray ?? throw new ArgumentNullException(nameof(inArray)); |
| 58 | + |
| 59 | + if (inArray.Length == 0) |
| 60 | + return string.Empty; |
| 61 | + |
| 62 | + var length = inArray.Length; |
| 63 | + |
| 64 | + int lengthmod3 = length % 3; |
| 65 | + int limit = length - lengthmod3; |
| 66 | + char[] output = new char[(length + 2) / 3 * 4]; |
| 67 | + char[] table = s_base64Table; |
| 68 | + int i, j = 0; |
| 69 | + |
| 70 | + // takes 3 bytes from inArray and insert 4 bytes into output |
| 71 | + for (i = 0; i < limit; i += 3) |
| 72 | + { |
| 73 | + byte d0 = inArray[i]; |
| 74 | + byte d1 = inArray[i + 1]; |
| 75 | + byte d2 = inArray[i + 2]; |
| 76 | + |
| 77 | + output[j + 0] = table[d0 >> 2]; |
| 78 | + output[j + 1] = table[((d0 & 0x03) << 4) | (d1 >> 4)]; |
| 79 | + output[j + 2] = table[((d1 & 0x0f) << 2) | (d2 >> 6)]; |
| 80 | + output[j + 3] = table[d2 & 0x3f]; |
| 81 | + j += 4; |
| 82 | + } |
| 83 | + |
| 84 | + //Where we left off before |
| 85 | + i = limit; |
| 86 | + |
| 87 | + switch (lengthmod3) |
| 88 | + { |
| 89 | + case 2: |
| 90 | + { |
| 91 | + byte d0 = inArray[i]; |
| 92 | + byte d1 = inArray[i + 1]; |
| 93 | + |
| 94 | + output[j + 0] = table[d0 >> 2]; |
| 95 | + output[j + 1] = table[((d0 & 0x03) << 4) | (d1 >> 4)]; |
| 96 | + output[j + 2] = table[(d1 & 0x0f) << 2]; |
| 97 | + j += 3; |
| 98 | + } |
| 99 | + break; |
| 100 | + |
| 101 | + case 1: |
| 102 | + { |
| 103 | + byte d0 = inArray[i]; |
| 104 | + |
| 105 | + output[j + 0] = table[d0 >> 2]; |
| 106 | + output[j + 1] = table[(d0 & 0x03) << 4]; |
| 107 | + j += 2; |
| 108 | + } |
| 109 | + break; |
| 110 | + |
| 111 | + //default or case 0: no further operations are needed. |
| 112 | + } |
| 113 | + |
| 114 | + return new string(output, 0, j); |
| 115 | + } |
| 116 | + |
| 117 | + // https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.19.0/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs#L179 |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Converts the specified string, which encodes binary data as base-64-url digits, to an equivalent 8-bit unsigned integer array.</summary> |
| 121 | + /// <param name="str">base64Url encoded string.</param> |
| 122 | + /// <returns>UTF8 bytes.</returns> |
| 123 | + public static byte[] DecodeBytes(string str) |
| 124 | + { |
| 125 | + _ = str ?? throw new ArgumentNullException(nameof(str)); |
| 126 | + |
| 127 | + // 62nd char of encoding |
| 128 | + str = str.Replace(base64UrlCharacter62, base64Character62); |
| 129 | + |
| 130 | + // 63rd char of encoding |
| 131 | + str = str.Replace(base64UrlCharacter63, base64Character63); |
| 132 | + |
| 133 | + // check for padding |
| 134 | + switch (str.Length % 4) |
| 135 | + { |
| 136 | + case 0: |
| 137 | + // No pad chars in this case |
| 138 | + break; |
| 139 | + case 2: |
| 140 | + // Two pad chars |
| 141 | + str += doubleBase64PadCharacter; |
| 142 | + break; |
| 143 | + case 3: |
| 144 | + // One pad char |
| 145 | + str += base64PadCharacter; |
| 146 | + break; |
| 147 | + default: |
| 148 | + throw new FormatException(str); |
| 149 | + } |
| 150 | + |
| 151 | + return Convert.FromBase64String(str); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments