bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
237 stars 122 forks source link

Different results for sha3 between version 3.0.1 and 1.0.2 #93

Closed redDwarf03 closed 3 years ago

redDwarf03 commented 3 years ago

i have different result with sha3digest method since i upgrade to PointyCastle 3.0.1 (I was with 1.0.2) With 1.0.2 the result was ok but now, not

For example, this code return wrong result

final hashed = sha3digest.process(publicKey);

But when I use sha3 plugin directly (https://pub.dev/packages/sha3), I have the good result

 var k = SHA3(256, KECCAK_PADDING, 256);
 k.update(publicKey);
 var hash = k.digest();

Any idea ?

Some plugin use sha3digest and after upgrade for null safety, they don't work anymore correctly

AKushWarrior commented 3 years ago

can you provide specific examples of inputs that were hashed incorrectly?

redDwarf03 commented 3 years ago

I ran this

var k = SHA3(256, KECCAK_PADDING, 256);   
k.update(HEX.decode("f1cd2450be226b99c61ebb251f2e62fd3df98711a59a3346ade9750658e8fee3a48243d15db036474303956515eb4552e0eb4c37253747007386506eb5df4d19"));
var hash = k.digest();
print(HEX.encode(hash));

final hashed = sha3digest.process(HEX.decode("f1cd2450be226b99c61ebb251f2e62fd3df98711a59a3346ade9750658e8fee3a48243d15db036474303956515eb4552e0eb4c37253747007386506eb5df4d19"));
print(HEX.encode(hashed));

I obtained

efa87d6f4a5c6d7bc3f87355d2c7a45cb829738884ef73374b373f31d0651c65
bef67e0c8af62221d0bc44f72e4c270b99071361cf397c04c7130e47ebcf9369

the first hex is ok. not the second (and with pointcastle 1.0.2, the same code gave same results)

redDwarf03 commented 3 years ago

is it because pointycastle's sha3 algorithm (in 1.x) was actually the keccak variant, and they changed that to a consistent naming in pointycastle v2? some dependencies are not ok (like web3dart for example)

AKushWarrior commented 3 years ago

That is correct. In pointycastle v2/3, you use KeccakDigest() instead of SHA3Digest() for the behavior from v1's SHA3Digest. This is more technically correct.

We do run tests to ensure that SHA-3 produces correct hashes, so I think that's the most likely cause.

redDwarf03 commented 3 years ago

i confirm I have the good result with

final hashed = KeccakDigest(256).process(HEX.decode("f1cd2450be226b99c61ebb251f2e62fd3df98711a59a3346ade9750658e8fee3a48243d15db036474303956515eb4552e0eb4c37253747007386506eb5df4d19"));

So, I close this issue but I open issues in the projects which use sha3 with pointycastle

thx