netstandard

时间:2018-06-07 00:27:53

标签: .net base64 urlencode .net-standard

我正在将net472定位到netstandard的项目。我坚持的最后System.Web依赖是HttpServerUtility.UrlTokenEncode(Byte[])

我找到了Microsoft.AspNetCore.WebUtilities,其中包含Base64UrlTextEncoderWebEncoders,但这些内容与UrlTokenEncode / Decode无法互换,因为它附加/期望最后= padding characters的数量,例如:

var data = Encoding.UTF8.GetBytes("SO");
Convert.ToBase64String(data);              // U08=
HttpServerUtility.UrlTokenEncode(data);    // U081 - this is what's expected and 
                                           // the only thing UrlTokenDecode can handle
Base64UrlTextEncoder.Encode(data);         // U08
WebEncoders.Base64UrlEncode(data);         // U08

据我所知,没有其他差异(我使用随机字符串运行测试),但它也引入了一些其他依赖项(Microsoft.Net.Http.Headers& Microsoft.Extensions.Primitives),我不需要那个项目。

是否有任何带有直接替代品的nuget包?我想自己实现这个,如果没有。

3 个答案:

答案 0 :(得分:5)

对于那些正在寻找这种已删除实用工具方法的答案并希望在其间迁移旧版应用程序的人,我从M $源代码中提取了一些信息。

   private static string UrlTokenEncode(byte[] input)
    {
        if (input == null)
            throw new ArgumentNullException("input");
        if (input.Length < 1)
            return String.Empty;
        char[] base64Chars = null;

        ////////////////////////////////////////////////////////
        // Step 1: Do a Base64 encoding
        string base64Str = Convert.ToBase64String(input);
        if (base64Str == null)
            return null;

        int endPos;
        ////////////////////////////////////////////////////////
        // Step 2: Find how many padding chars are present in the end
        for (endPos = base64Str.Length; endPos > 0; endPos--)
        {
            if (base64Str[endPos - 1] != '=') // Found a non-padding char!
            {
                break; // Stop here
            }
        }

        ////////////////////////////////////////////////////////
        // Step 3: Create char array to store all non-padding chars,
        //      plus a char to indicate how many padding chars are needed
        base64Chars = new char[endPos + 1];
        base64Chars[endPos] = (char)((int)'0' + base64Str.Length - endPos); // Store a char at the end, to indicate how many padding chars are needed

        ////////////////////////////////////////////////////////
        // Step 3: Copy in the other chars. Transform the "+" to "-", and "/" to "_"
        for (int iter = 0; iter < endPos; iter++)
        {
            char c = base64Str[iter];

            switch (c)
            {
                case '+':
                    base64Chars[iter] = '-';
                    break;

                case '/':
                    base64Chars[iter] = '_';
                    break;

                case '=':
                    Debug.Assert(false);
                    base64Chars[iter] = c;
                    break;

                default:
                    base64Chars[iter] = c;
                    break;
            }
        }
        return new string(base64Chars);
    }

    private static byte[] UrlTokenDecode(string input)
    {
        if (input == null)
            throw new ArgumentNullException("input");

        int len = input.Length;
        if (len < 1)
            return new byte[0];

        ///////////////////////////////////////////////////////////////////
        // Step 1: Calculate the number of padding chars to append to this string.
        //         The number of padding chars to append is stored in the last char of the string.
        int numPadChars = (int)input[len - 1] - (int)'0';
        if (numPadChars < 0 || numPadChars > 10)
            return null;


        ///////////////////////////////////////////////////////////////////
        // Step 2: Create array to store the chars (not including the last char)
        //          and the padding chars
        char[] base64Chars = new char[len - 1 + numPadChars];


        ////////////////////////////////////////////////////////
        // Step 3: Copy in the chars. Transform the "-" to "+", and "*" to "/"
        for (int iter = 0; iter < len - 1; iter++)
        {
            char c = input[iter];

            switch (c)
            {
                case '-':
                    base64Chars[iter] = '+';
                    break;

                case '_':
                    base64Chars[iter] = '/';
                    break;

                default:
                    base64Chars[iter] = c;
                    break;
            }
        }

        ////////////////////////////////////////////////////////
        // Step 4: Add padding chars
        for (int iter = len - 1; iter < base64Chars.Length; iter++)
        {
            base64Chars[iter] = '=';
        }

        // Do the actual conversion
        return Convert.FromBase64CharArray(base64Chars, 0, base64Chars.Length);
    }

答案 1 :(得分:2)

我的KISS,解决方案,适用于netstandard1.6

public static class Utils
{
    private static readonly Regex InvalidBase64UrlTokens = new Regex(
        @"[^=a-z0-9]", 
        RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

    public static string Base64UrlTokenEncode(byte[] data)
    {
        var padding = 0;
        var base64String = Convert.ToBase64String(data);
        return InvalidBase64UrlTokens.Replace(base64String, m => {
            switch (m.Value)
            {
                case "+": return "-";
                case "=":
                    padding++;
                    return "";
                default: return "_";
            }
        }) + padding;
    }
}

通过以下nunit测试:

[TestFixture]
public class Base64UrlTokenEncodingTests
{        
    private static IEnumerable<TestCaseData> CompareAgainstSystemWebImplementationCases()
    {
        var random = new Random(42);
        for (var i = 0; i < 100; i++)
        {
            var bytes = new byte[i + 1];
            random.NextBytes(bytes);

            var name = Convert.ToBase64String(bytes);
            var systemWeb = System.Web.HttpServerUtility.UrlTokenEncode(bytes);
            yield return new TestCaseData(bytes).SetName(name).Returns(systemWeb);
        }
    }

    [TestCaseSource(nameof(CompareAgainstSystemWebImplementationCases))]
    public string CompareAgainstSystemWebImplementation(byte[] data) =>
        Utils.Base64UrlTokenEncode(data);
}

答案 2 :(得分:0)

在网络核心:Microsoft.AspNetCore.WebUtilities.Base64UrlEndCoder

访问https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webutilities.webencoders?view=aspnetcore-5.0