what kind of key to use for CALG_SHA_256 hash

Greg Wilson-Lindberg 126 Reputation points
2022-11-10T23:31:11.133+00:00

I am trying to generate a SHA_256 hash across some data. I have started by using the example provided here: example-c-program--creating-an-hmac.

This example uses CALG_HMAC to create the hash, I want to use CALG_SHA_256. I found that I need to change the initial 'provider type' in the CryptAcquireContext() call to PROV_RSA_AES (line 50 in the example) to allow changing the hash algorithm (in line 117) to CALG_SHA_256. But when I do that, I get an NTE_BAD_KEY error.

So, my question is, what kind of key do I need to create an SHA_256 hash. I haven't been able to find any other examples of creating a hash, or actually using the MS crypto functions at all. And none of this is documented that I can find.

Regards,
Greg

Windows development | Windows API - Win32
Developer technologies | C++
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2022-11-11T08:58:28.623+00:00

    Hi

    @Greg Wilson-Lindberg

    Welcome to Microsoft Q&A!

    I suggest you could try replacing the provider's name with "Microsoft Enhanced RSA and AES Cryptographic Provider".

    CryptAcquireContext(&hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0);  
    

    For more details I suggest you could refer to the thread: https://stackoverflow.com/questions/4191312/windows-cryptoapi-cryptsignhash-with-calg-sha-256-and-private-key-from-my-keyst

    Thank you.

    Jeanine


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.