title: Using ruby to generate SHA512 crypt-style hashes formatted for /etc/shadow?
body: <p>I want to generate SHA512 hashed passwords for inclusion directly into a /etc/shadow file for use with <a href="http://wiki.opscode.com/display/chef/Resources#Resources-User" rel="noreferrer">chef's user resource</a>.  Normally I'd go to the stdlib's <code>Digest</code> library for this, but it doesn't generate the hash in the right format:</p>
  
  <pre><code>ruby-1.9.2-p136 :001 &gt; require 'digest/sha2'
   =&gt; true 
  ruby-1.9.2-p136 :002 &gt; Digest::SHA512.hexdigest('test')
   =&gt; "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff" 
  </code></pre>
  
  <p>The format that the shadow file wants is: </p>
  
  <pre><code>$6$/ShPQNXV$HJnibH9lw01qtYqyJQiBf81ggJB2BGUvKA7.kv39HGCeE.gD4C/SS9zAf5BrwOv3VJzvl99FpHYli9E8jykRC0
  </code></pre>
  
  <p>Things I've looked at:</p>
  
  <ul>
  <li>The openssl "dgst" module returns the same format as .hexdigest, and its "passwd" module doesn't include SHA512 support.</li>
  <li>String#crypt, but that does not support SHA512. (edit: this is only the case on OSX - modern Linux distros will work if you specify "$6$somesalt" as the salt)</li>
  <li><a href="http://crypt.rubyforge.org/" rel="noreferrer">ruby-crypt</a> gem, but it does not support SHA512</li>
  </ul>
  
  <p>For comparison, something that does return the proper format is PHP's <a href="http://us3.php.net/crypt" rel="noreferrer">crypt</a>, but I'd rather not have to exec out to PHP for something that should be simple.</p>