1

如何将此 Java 代码转换为 C#?

    byte[] a = ...some byte array...;
    byte[] b = ...some byte array...;

    MessageDigest m_Hash = MessageDigest.getInstance("SHA-1");
    m_Hash.update(a);
    m_Hash.update(b);
    byte[] ub = m_Hash.digest();

到目前为止,我有:

        var hash = HashAlgorithm.Create("SHA-1");
        hash.ComputeHash(a);
        hash.ComputeHash(b);

但我不认为这是朝着正确的方向发展,因为 ComputeHash 实际上返回了一个字节 []。

4

1 回答 1

2

所以......看起来更新只是附加字节数组......我写了一个函数来做到这一点,它看起来像这样:

    var hash = HashAlgorithm.Create("SHA-1");
    byte[] ub = hash.ComputeHash(AppendArrays(a, b));

    public byte[] AppendArrays(byte[] b1, params byte[][] others)
    {
        int n = b1.Length;
        foreach (var other in others)
            n += other.Length;

        var result = new byte[n];

        n = 0;
        Array.Copy(b1, 0, result, n, b1.Length);
        n += b1.Length;
        foreach (var other in others)
        {
            Array.Copy(other, 0, result, n, other.Length);
            n += other.Length;
        }

        return result;
    }
于 2017-10-03T04:26:37.297 回答