title: OpenSSL SHA256 Wrong result
body: <p>I have following piece of code that is supposed to calculate the <code>SHA256</code> of a file. I am reading the file chunk by chunk and using <code>EVP_DigestUpdate</code> for the chunk. When I test the code with the file that has content</p>
  
  <p><code>Test Message
   Hello World
  </code></p>
  
  <p>in Windows, it gives me SHA256 value of <code>97b2bc0cd1c3849436c6532d9c8de85456e1ce926d1e872a1e9b76a33183655f</code> but the value is supposed to be <code>318b20b83a6730b928c46163a2a1cefee4466132731c95c39613acb547ccb715</code>, which can be verified <a href="http://www.xorbin.com/tools/sha256-hash-calculator" rel="nofollow">here too</a>.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;openssl\evp.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdio&gt;
  const int MAX_BUFFER_SIZE = 1024;
  std::string FileChecksum(std::string, std::string);
  int main()
  {
      std::string checksum = FileChecksum("C:\\Users\\Dell\\Downloads\\somefile.txt","sha256");
      std::cout &lt;&lt; checksum &lt;&lt; std::endl;
      return 0;
  }
  
  std::string FileChecksum(std::string file_path, std::string algorithm)
  {
       EVP_MD_CTX *mdctx;
       const EVP_MD *md;
       unsigned char md_value[EVP_MAX_MD_SIZE];
       int i;
       unsigned int md_len;
  
       OpenSSL_add_all_digests();
       md = EVP_get_digestbyname(algorithm.c_str());
  
       if(!md) {
              printf("Unknown message digest %s\n",algorithm);
              exit(1);
       }
  
       mdctx = EVP_MD_CTX_create();
       std::ifstream readfile(file_path,std::ifstream::in|std::ifstream::binary);
       if(!readfile.is_open())
       {
           std::cout &lt;&lt; "COuldnot open file\n";
           return 0;
       }
       readfile.seekg(0, std::ios::end);
       long filelen = readfile.tellg();
       std::cout &lt;&lt; "LEN IS " &lt;&lt; filelen &lt;&lt; std::endl;
       readfile.seekg(0, std::ios::beg);
       if(filelen == -1)
       {
           std::cout &lt;&lt; "Return Null \n";
           return 0;
       }
  
       EVP_DigestInit_ex(mdctx, md, NULL);
       long temp_fil = filelen;
       while(!readfile.eof() &amp;&amp; readfile.is_open() &amp;&amp; temp_fil&gt;0)
       {
  
           int bufferS = (temp_fil &lt; MAX_BUFFER_SIZE) ? temp_fil : MAX_BUFFER_SIZE;
           char *buffer = new char[bufferS+1];
           buffer[bufferS] = 0;
           readfile.read(buffer, bufferS);
           std::cout &lt;&lt; strlen(buffer) &lt;&lt; std::endl;
           EVP_DigestUpdate(mdctx, buffer, strlen(buffer));
           temp_fil -= bufferS;
           delete[] buffer;
       }
       EVP_DigestFinal_ex(mdctx, md_value, &amp;md_len);
       EVP_MD_CTX_destroy(mdctx);
  
       printf("Digest is: ");
       //char *checksum_msg = new char[md_len];
       //int cx(0);
       for(i = 0; i &lt; md_len; i++)
       {
          //_snprintf(checksum_msg+cx,md_len-cx,"%02x",md_value[i]);
           printf("%02x", md_value[i]);
       }
       //std::string res(checksum_msg);
       //delete[] checksum_msg;
  
       printf("\n");
  
       /* Call this once before exit. */
       EVP_cleanup();
       return "";
  }
  </code></pre>
  
  <p>I tried to write the hash generated by program as string using <code>_snprintf</code> but it didn't worked. How can I generate the correct hash and return the value as string from <code>FileChecksum</code> Function? Platform is Windows.</p>
  
  <p><strong>EDIT:</strong>  It seems the problem was because of <code>CRLF</code> issue. As Windows in saving file using <code>\r\n</code>, the Checksum calculated was different. How to handle this?</p>