title: Compute MD5 hash value by c++ WinAPI
body: <p>My C++ app needs to compute an MD5 hash value. Currently, it is done by OpenSSL, and I want to use WinAPI - to avoid dependencies on external libraries.</p>
  
  <p>I wrote this code:</p>
  
  <pre><code>HCRYPTPROV hCryptProv = NULL;
  HCRYPTHASH hHash = NULL;
  HCRYPTHASH hHexHash = NULL;
  HASH HA1;
  HASHHEX HA1HEX;
  DWORD data = HASHLEN;
  
  // Get a handle to a cryptography provider context.
  if(!CryptAcquireContext(&amp;hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
  {
      goto err;
  }
  
  // Acquire a hash object handle.
  if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &amp;hHash))
  {
      goto err;
  }
  
  CryptHashData(hHash, (const BYTE *)str, strlen(str), 0);
  </code></pre>
  
  <p>now, the strange thing is that sometimes it works well, but in other times the CryptAcquireContext returns NTE_BAD_KEYSET error, that according to MSDN
  : </p>
  
  <blockquote>
    <p>The key container could not be opened. A common cause of this error is that the key container does not exist. To create a key container, call CryptAcquireContext using the CRYPT_NEWKEYSET flag. This error code can also indicate that access to an existing key container is denied. Access rights to the container can be granted by the key set creator by using CryptSetProvParam.</p>
    
    <p>Blockquote</p>
  </blockquote>
  
  <p>Now my questions are:</p>
  
  <ol>
  <li>If I call CryptSetProvParam, what exactly does it do? Is it normal that simple application changes the OS settings?</li>
  <li>Is there any less difficult way to compute MD5 in C++ windows?</li>
  </ol>
  
  <p>I'll appreciate if someone gives me good advice on what to do.<br>
  Thanks.</p>