title: sha1 function gives weird output
body: <p>I have this C++ program developed in visual studio, which should print out the SHA1 and MD5 hashes of a given string:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;ctime&gt;
  #include "windows.h"
  #include "conio.h"
  #include "wincrypt.h"
  
  char Buf [256];
  DWORD BufSize;
  
  LPSTR CreateHash(LPSTR tohash, ALG_ID alg)
  {
      HCRYPTPROV hProv;
      HCRYPTHASH hash;
  
      if (CryptAcquireContext(&amp;hProv, NULL, NULL, PROV_RSA_FULL,
          CRYPT_VERIFYCONTEXT))
      {   
          if (CryptCreateHash(hProv, alg, 0, 0, &amp;hash))
          {
              if (CryptHashData(hash, (BYTE *)&amp;tohash, sizeof(tohash), 0))
              {
                  ZeroMemory(&amp;Buf, sizeof(Buf));
                  BufSize = sizeof(Buf);
  
                  if (!CryptGetHashParam(hash, HP_HASHVAL,
                      (BYTE *) &amp;Buf, &amp;BufSize, 0))
                      printf("Call to CryptGetHashParam failed. Error: %d\n",
                      GetLastError());
  
              } else printf("Call to CryptHashData failed. Error: %d\n",
                  GetLastError());
  
              if (!CryptDestroyHash(hash))
                  printf("Call to CryptDestroyHash failed. Error: %d\n",
                  GetLastError());
  
          } else printf("Call to CryptCreateHash failed. Error: %d\n", 
              GetLastError());
  
          if (!CryptReleaseContext(hProv, 0))
              printf("Call to CryptReleaseContext failed. Error: %d\n", 
              GetLastError());
  
      } else printf("Call to CryptAcquireContext failed. Error: %d\n", 
          GetLastError());
  
      return Buf;
  }
  
  using namespace std;
  int _tmain(int argc, _TCHAR* argv[])
  {
      cout &lt;&lt; "SHA hash: " &lt;&lt; CreateHash("Visual C++", CALG_SHA)&lt;&lt;endl;
      cout &lt;&lt; "MD5 hash: " &lt;&lt; CreateHash("Visual C++", CALG_MD5)&lt;&lt;endl;
  
      system("pause");
  }
  </code></pre>
  
  <p>It compiles but when i run it, it shows weird output.</p>
  
  <p>The output should be</p>
  
  <pre><code>SHA hash: 0e4abc18f9fb9d8d6d39f047dc20fbebd4a975bd
  MD5 hash: f3be2f04531a477a4bb755f9f42c6f2e
  </code></pre>
  
  <p>But it prints</p>
  
  <pre><code>SHA hash: ü©á*ÀªvÍŒe²5Ê¥Å?•Â
  MD5 hash: òù%•þîOú°b&gt;«’
  </code></pre>
  
  <p>Why?</p>
  
  <p>How can i solve this?</p>