Search code examples
c#c++wrappersha1

SHA1 different between C# and C++


Hi i'm porting a c++ program to C# but the sha result are different. I'm using openssl library with a wrapper for c#

I have this codes

C++

BigNumber A;
A.SetHexStr("4C9309B98E8A3D7E357688305FECFA1676404D0ABFDE0AB48A98EF318AAE35EA");
SHA1Hash sha;
sha.UpdateBigNumbers(&A);
sha.Finalize();
BigNumber u;
u.SetBinary(sha.GetDigest(), 20);
log("u: %d %s", u.GetNumBytes(), u.AsHexStr());

Result

A: 4C9309B98E8A3D7E357688305FECFA1676404D0ABFDE0AB48A98EF318AAE35EA    
u: 330E234D38088E7C58B40A278A0833B844F06CB5

Here is the link for SHA1Hash.cpp http://pastebin.com/hf5mn1zU

C#

  Sha1Hash SHA = new Sha1Hash();

  byte[] digest = new byte[20];
  BigNumber A = BigNumber.FromHexString("4C9309B98E8A3D7E357688305FECFA1676404D0ABFDE0AB48A98EF318AAE35EA");
  SHA.UpdateData(A.ToBytes(),A.Bytes);
  SHA.Final();
  BigNumber u = BigNumber.FromArray(SHA.getDigest(), 20);
  Logger.Message(LogLevel.Debug, A.ToHexString());
  Logger.Message(LogLevel.Debug, u.ToHexString());

Result

A 4C9309B98E8A3D7E357688305FECFA1676404D0ABFDE0AB48A98EF318AAE35EA
u 1085A72DE27D6D981C1382170B6E39FDED45B92D

C# Files

http://pastebin.com/yCHMkaZS

I tried everything encoding etc but i don't know why they don't match

Thanks in advance

Update: No it still don't work even with NULL


Solution

  • Solved

    I used these classes and now the hashs match

    http://www.codeproject.com/Articles/2728/C-BigInteger-Class

    https://github.com/justMaku/mClient/blob/master/mClient/Crypt/Sha1Hash.cs