1

How can I convert this Java code to 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[].

Brian Rice
  • 3,107
  • 1
  • 35
  • 53

1 Answers1

2

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;
    }
Brian Rice
  • 3,107
  • 1
  • 35
  • 53