TransformBlock and TransformFinalBlock are leaving null hash.

Create issue
Issue #5 resolved
claunia created an issue

Using Mono 3.12.0 and SHA3 0.9.2:

using SHA3;

public static byte[] ComputeSHA3(byte[] data) { SHA3Unmanaged _sha3Provider; _sha3Provider = new SHA3Unmanaged(512);

_sha3Provider.TransformBlock(data, 0, (int)len, data, 0);
_sha3Provider.TransformFinalBlock(new byte[0], 0, 0);
return _sha3Provider.Hash;

}

Will return null.

Comments (8)

  1. Joe Dluzen repo owner

    I just tried to repro using the code you provided. It does seem like it's a Mono issue. It works here on .NET 4.

  2. Joe Dluzen repo owner

    Yes, the issue has to due with Mono's implementation. Specifically, they call Initialize() just before returning, Microsoft's does not. My Initialize() clears the hash value because my CanReuseTransform = true. I'll file a bug.

    In the meantime, you can create a new class which inherits from SHA3, and add a new method such as MyTransformFinalBlock, and use Mono's implementation, but remove the Initialize() call.

  3. claunia reporter

    Ok, I've found the problem.

    On SHA3.cs, line 197, HashValue = null.

    For some reason on .NET Framework HashValue is returned before Initialize() nullifies it, so it gets to its destination. On the contrary on Mono, it is nullified before being returned, so null is arriving.

    Checking Mono sources, seems that they don't nullify HashValue on any of their Initialize() implementations, dunno what does Microsoft.

    Solution is up to you on how to do that, but just removing the offending line seems to not affect the tests.

    My suggestion is to add a private bool that says if HashValue is correct or not depending on TransformFinalBlock() being called so Hash returns HashValue only if it is correct, that's being set on a combination of Initialize and first call to TransformBlock, not just on Initialize. imho.

  4. Log in to comment