使用.netstandard 2.0转换大端字节数组BigInteger

问题描述 投票:0回答:1

由于this constructor类的BigInteger,我有一个可与.netstandard2.1 / netcore3.0一起使用的代码。此ctor不适用于netstandard2.0,我希望能够在不强制使用netstandard2.1的情况下实现相同的功能。

这里是问题:

  • 将字符串转换为UTF8,并使用Sha256对其进行哈希处理
  • 使用big endian字节顺序转换BigInteger中的字节数组

这是我的可与.netstandard2.1配合使用的解决方案

    static SHA256 sha256 = SHA256.Create();
    internal static string GetEncoded(string value)
    {
        var data = sha256.ComputeHash(value.GetUTF8Bytes());

        return new BigInteger(
            value: data,
            isUnsigned: true,
            isBigEndian: true).ToString();
    }

这是netstandard2.0中的解决方案,很遗憾,它不会产生相同的结果。

    internal static string GetEncoded(string value)
    {
        var data = sha256.ComputeHash(value.GetUTF8Bytes());

        return new BigInteger(value: data).ToString();
    }

这里有一些样本值及其预期的编码输出。

"encoded": "68086943237164982734333428280784300550565381723532936263016368251445461241953",
"raw": "101 Wilson Lane"

"encoded": "101327353979588246869873249766058188995681113722618593621043638294296500696424",
"raw": "SLC"

据我了解,BigInteger(byte[]) ctor期望一个小的字节序字节数组。我尝试的几种解决方案均未取得预期的结果,因此我将向SO寻求答案。任何帮助将不胜感激。

Related update issue to net core

c# biginteger .net-standard
1个回答
0
投票

这是您使用的构造函数的代码:https://github.com/dotnet/corefx/blob/191ad0b5d52172366436322bf9d553dc770d23b1/src/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs#L256

您可以对其进行修改,以便用字节[]代替ReadOnlySpan。

© www.soinside.com 2019 - 2024. All rights reserved.