Different hashes are obtained in C and C #.

On C:

UINT16 data1 = 88; ULONG data2 = 74; PBYTE hash; DWORD len = 0, sz; HCRYPTPROV hProv; HCRYPTHASH hHash; CryptAcquireContextA( reinterpret_cast<HCRYPTPROV*>(&hProv), NULL, (LPSTR)MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET ); CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash); CryptHashData(hHash, (PBYTE)&data1, sizeof(UINT16), 0); CryptHashData(hHash, (PBYTE)&data2, sizeof(ULONG), 0); CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&len, &sz, 0); hash = static_cast<PBYTE>(calloc(1, (size_t)len)); CryptGetHashParam(hHash, HP_HASHVAL, hash, &len, 0); 

(checks removed for short)

In C #:

 ushort data1 = 88; UInt32 data2 = 74; int sz = 0; byte[] data = new byte[sizeof(UInt32) + sizeof(ushort)]; byte[] hash; Array.Copy( BitConverter.GetBytes(data1), 0, data, sz, sizeof(ushort)); sz += sizeof(ushort); Array.Copy( BitConverter.GetBytes(data2), 0, data, sz, sizeof(UInt32)); using (SHA256Managed hs = new SHA256Managed()) { hash = hs.ComputeHash(data); } 

Problems at first glance are not visible, but the result is different.

Upd:

The issue is resolved, as always inattention :)

 DWORD len = 0, sz = sizeof(DWORD); 

C: 3a b1 88 5e eb 0b bf f4 cc 42 12 9c 21 09 25 73 64 45 db 52 55 9d 3a 23 40 be 15 2c 70 4b aa 2d

C #: 3A B1 88 5E EB 0B BF F4 CC 42 12 9C 21 09 25 73 64 45 DB 52 55 9D 3A 23 40 BE 15 2C 70 4B AA 2D

  • The problem is definitely not in the code that you gave. Everything is ok in me. - Zergatul
  • Did you compare the result of executing C vs C #? Hash is the same? - NewView
  • Where is the rest of the code? Where is the beginning in the first version? Where is getting the final hash in the first version? - AnT
  • C part? everything is standard there, but I can add, there is not enough to turn out, taking into account the initialization of CryptHash * - NewView
  • one
    Yes, my hashes are the same, even with your new code. I only pass other parameters to CryptAcquireContextA , because with yours it returns an error: CryptAcquireContextA(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) - Zergatul

0