title: How can I encrypt SHA512 hash with salt in C and ubuntu?
body: <p>I'm trying to encrypt hash with salt.</p>
  <p>In command line, I found right hash written in the shadow file using below command.</p>
  <p>openssl passwd -6 -salt abcd appple
  $6$abcd$vIWAp1OzuGuo376cRkZ5DXcgI8KIlnUibsk.iydtfCFqb9okOz.S70Ysu7.qRRg9Me5XwAyTjkZBQJXiIxwpL/</p>
  <p>And I tried to get it in C using openssl(<a href="https://stackoverflow.com/questions/47702475/how-to-convert-a-raw-64-byte-binary-to-hex-or-ascii-in-c">How to convert a raw 64-byte binary to Hex or ASCII in C</a>).</p>
  <pre><code>#include &lt;openssl/sha.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main() {
          char data[] = &quot;abcdapple&quot;;
          unsigned char hash[SHA512_DIGEST_LENGTH*2+1];
          SHA512((unsigned char *)&amp;data, strlen(data), (unsigned char *)&amp;hash);
  
          char buffer[SHA512_DIGEST_LENGTH*2+1];
          for(int i =0; i &lt; SHA512_DIGEST_LENGTH; ++i)
                  sprintf(&amp;buffer[i*2],&quot;%02x&quot;, (unsigned int)hash[i]);
          printf(&quot;digest: %s\n&quot;, buffer);
          printf(&quot;\n&quot;);
  }
  </code></pre>
  <p>but generated hash is ea1d5a8b11297d20f954a3ab15092d21b733484b2eb9b7226b2b138639f0df30627774945458a774eb279cd83d2e977a2bc5599606d6a9a3b2f075f9b1895ecd, which is different from right thing.</p>
  <p>I think I didn't use salt well. Also there seems to be a fundamental problem.
  Can't I implement encrypt program with salt using C?</p>