简体   繁体   English

如何将带有两个更新语句的 Java MessageDigest 转换为 C#

[英]How to convert Java MessageDigest with two update statements to C#

How can I convert this Java code to C#?如何将此 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();

So far I have:到目前为止,我有:

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

But I don't think this is going in the right direction because ComputeHash actually returns a byte[].但我不认为这是朝着正确的方向发展,因为 ComputeHash 实际上返回了一个字节 []。

So... it looks like update just appends the byte arrays... I wrote a function to do this and it looks like this:所以......看起来更新只是附加字节数组......我写了一个函数来做到这一点,它看起来像这样:

    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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM