diff --git a/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionCommonExtensions.cs b/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionCommonExtensions.cs index 97b1377abe..f4fd8801ae 100644 --- a/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionCommonExtensions.cs +++ b/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionCommonExtensions.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using Microsoft.AspNetCore.DataProtection.Abstractions; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.DataProtection { diff --git a/src/Microsoft.AspNetCore.DataProtection.Abstractions/WebEncoders.cs b/src/Microsoft.AspNetCore.DataProtection.Abstractions/WebEncoders.cs deleted file mode 100644 index 607fc7e35e..0000000000 --- a/src/Microsoft.AspNetCore.DataProtection.Abstractions/WebEncoders.cs +++ /dev/null @@ -1,133 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Diagnostics; - -namespace Microsoft.AspNetCore.DataProtection -{ - // Internal copy of HttpAbstractions functionality. - internal static class WebEncoders - { - /// - /// Decodes a base64url-encoded string. - /// - /// The base64url-encoded input to decode. - /// The base64url-decoded form of the input. - /// - /// The input must not contain any whitespace or padding characters. - /// Throws FormatException if the input is malformed. - /// - public static byte[] Base64UrlDecode(string input) - { - // Assumption: input is base64url encoded without padding and contains no whitespace. - - // First, we need to add the padding characters back. - int numPaddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(input.Length); - char[] completeBase64Array = new char[checked(input.Length + numPaddingCharsToAdd)]; - Debug.Assert(completeBase64Array.Length % 4 == 0, "Invariant: Array length must be a multiple of 4."); - input.CopyTo(0, completeBase64Array, 0, input.Length); - for (int i = 1; i <= numPaddingCharsToAdd; i++) - { - completeBase64Array[completeBase64Array.Length - i] = '='; - } - - // Next, fix up '-' -> '+' and '_' -> '/' - for (int i = 0; i < completeBase64Array.Length; i++) - { - char c = completeBase64Array[i]; - if (c == '-') - { - completeBase64Array[i] = '+'; - } - else if (c == '_') - { - completeBase64Array[i] = '/'; - } - } - - // Finally, decode. - // If the caller provided invalid base64 chars, they'll be caught here. - return Convert.FromBase64CharArray(completeBase64Array, 0, completeBase64Array.Length); - } - - /// - /// Encodes an input using base64url encoding. - /// - /// The binary input to encode. - /// The base64url-encoded form of the input. - public static string Base64UrlEncode(byte[] input) - { - // Special-case empty input - if (input.Length == 0) - { - return String.Empty; - } - - // We're going to use base64url encoding with no padding characters. - // See RFC 4648, Sec. 5. - char[] buffer = new char[GetNumBase64CharsRequiredForInput(input.Length)]; - int numBase64Chars = Convert.ToBase64CharArray(input, 0, input.Length, buffer, 0); - - // Fix up '+' -> '-' and '/' -> '_' - for (int i = 0; i < numBase64Chars; i++) - { - char ch = buffer[i]; - if (ch == '+') - { - buffer[i] = '-'; - } - else if (ch == '/') - { - buffer[i] = '_'; - } - else if (ch == '=') - { - // We've reached a padding character: truncate the string from this point - return new String(buffer, 0, i); - } - } - - // If we got this far, the buffer didn't contain any padding chars, so turn - // it directly into a string. - return new String(buffer, 0, numBase64Chars); - } - - private static int GetNumBase64CharsRequiredForInput(int inputLength) - { - int numWholeOrPartialInputBlocks = checked(inputLength + 2) / 3; - return checked(numWholeOrPartialInputBlocks * 4); - } - - private static int GetNumBase64PaddingCharsInString(string str) - { - // Assumption: input contains a well-formed base64 string with no whitespace. - - // base64 guaranteed have 0 - 2 padding characters. - if (str[str.Length - 1] == '=') - { - if (str[str.Length - 2] == '=') - { - return 2; - } - return 1; - } - return 0; - } - - private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength) - { - switch (inputLength % 4) - { - case 0: - return 0; - case 2: - return 2; - case 3: - return 1; - default: - throw Error.CryptCommon_PayloadInvalid(); // not valid base64 - } - } - } -} diff --git a/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json b/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json index b5e4591fd8..8a46ae5517 100644 --- a/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json +++ b/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json @@ -15,6 +15,10 @@ "Microsoft.AspNetCore.DataProtection.Sources": { "type": "build", "version": "1.1.0-*" + }, + "Microsoft.Extensions.WebEncoders.Sources": { + "type": "build", + "version": "1.1.0-*" } }, "frameworks": { diff --git a/test/Microsoft.AspNetCore.Cryptography.Internal.Test/Microsoft.AspNetCore.Cryptography.Internal.Test.xproj b/test/Microsoft.AspNetCore.Cryptography.Internal.Test/Microsoft.AspNetCore.Cryptography.Internal.Test.xproj index b2ff65481f..2cef9ca48f 100644 --- a/test/Microsoft.AspNetCore.Cryptography.Internal.Test/Microsoft.AspNetCore.Cryptography.Internal.Test.xproj +++ b/test/Microsoft.AspNetCore.Cryptography.Internal.Test/Microsoft.AspNetCore.Cryptography.Internal.Test.xproj @@ -13,5 +13,8 @@ 2.0 + + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test.xproj b/test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test.xproj index b4f9d422bc..4dc21f6e52 100644 --- a/test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test.xproj +++ b/test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test/Microsoft.AspNetCore.Cryptography.KeyDerivation.Test.xproj @@ -13,5 +13,8 @@ 2.0 + + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/Microsoft.AspNetCore.DataProtection.Abstractions.Test.xproj b/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/Microsoft.AspNetCore.DataProtection.Abstractions.Test.xproj index 9c681869bd..d3ab9d6ae6 100644 --- a/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/Microsoft.AspNetCore.DataProtection.Abstractions.Test.xproj +++ b/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/Microsoft.AspNetCore.DataProtection.Abstractions.Test.xproj @@ -13,5 +13,8 @@ 2.0 + + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.DataProtection.Extensions.Test/Microsoft.AspNetCore.DataProtection.Extensions.Test.xproj b/test/Microsoft.AspNetCore.DataProtection.Extensions.Test/Microsoft.AspNetCore.DataProtection.Extensions.Test.xproj index aca61d6a38..e3f8006626 100644 --- a/test/Microsoft.AspNetCore.DataProtection.Extensions.Test/Microsoft.AspNetCore.DataProtection.Extensions.Test.xproj +++ b/test/Microsoft.AspNetCore.DataProtection.Extensions.Test/Microsoft.AspNetCore.DataProtection.Extensions.Test.xproj @@ -13,5 +13,8 @@ 2.0 + + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.DataProtection.Test/Microsoft.AspNetCore.DataProtection.Test.xproj b/test/Microsoft.AspNetCore.DataProtection.Test/Microsoft.AspNetCore.DataProtection.Test.xproj index 6fc457862f..4673904cc3 100644 --- a/test/Microsoft.AspNetCore.DataProtection.Test/Microsoft.AspNetCore.DataProtection.Test.xproj +++ b/test/Microsoft.AspNetCore.DataProtection.Test/Microsoft.AspNetCore.DataProtection.Test.xproj @@ -13,5 +13,8 @@ 2.0 + + + \ No newline at end of file