I have a SHA1 hash and I need to sign it. The CryptSignHash () method requires a HCRYPTHASH handle to write. I create it, and since I have the actual value of the hash code, install it:
CryptCreateHash(cryptoProvider, CALG_SHA1, 0, 0, &hash); CryptSetHashParam(hash, HP_HASHVAL, hashBytes, 0);
hashBytes is an array of 20 bytes.
However, the problem is that the signature created using this HCRYPTHASH manuscript is incorrect. I traced the problem to the point that CAPI does not actually use all 20 bytes from my hashBytes array. For some reason, he believes that SHA1 is only 4 bytes.
To test this, I wrote this little program:
HCRYPTPROV cryptoProvider; CryptAcquireContext(&cryptoProvider, NULL, NULL, PROV_RSA_FULL, 0); HCRYPTHASH hash; HCRYPTKEY keyForHash; CryptCreateHash(cryptoProvider, CALG_SHA1, keyForHash, 0, &hash); DWORD hashLength; CryptGetHashParam(hash, HP_HASHSIZE, NULL, &hashLength, 0); printf("hashLength: %d\n", hashLength);
And that prints hashLength: 4!
Can someone explain what I'm doing wrong or why Microsoft CAPI thinks that SHA1 is 4 bytes (32 bits) instead of 20 bytes (160 bits).