title: How to generate a salted SHA-512 password hash for Dovecot with PHP
body: <p>I am getting stuck by creating a working salted SHA-512 hash for Dovecot using PHP. When create a non-salted hex-formated hash everthing works fine. add a salt to the hash, I am not able to verify the hash with "doveadm pw" anymore.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>    $password       = 'test';
      $salt           = '2fec1ee0940e7c436ef2037e89e4c06ca20b281a90dbb2d6cbd3534aa4ce7e19';
  
      // Create non-salted hashes
      $hash_hex       = "{SHA512.hex}" . hash('sha512', $password);
      $hash_b64       = "{SHA512.b64}" . base64_encode(hash('sha512', $password));
  
      // Create salted hashes
      $salted_hash_hex= "{SSHA512.hex}" . hash('sha512', $password . $salt) . $salt;
      $salted_hash_b64= "{SSHA512.b64}" . base64_encode(hash('sha512', $password . $salt) . $salt);
  
      // Output
      var_dump($hash_hex);
      var_dump($hash_b64);
      var_dump($salted_hash_hex);
      var_dump($salted_hash_b64);
  </code></pre>
  
  <p>The output of the var_dumps() are as follows:</p>
  
  <pre><code>string '{SHA512.hex}ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff' (length=140)
  string '{SHA512.b64}ZWUyNmIwZGQ0YWY3ZTc0OWFhMWE4ZWUzYzEwYWU5OTIzZjYxODk4MDc3MmU0NzNmODgxOWE1ZDQ5NDBlMGRiMjdhYzE4NWY4YTBlMWQ1Zjg0Zjg4YmM4ODdmZDY3YjE0MzczMmMzMDRjYzVmYTlhZDhlNmY1N2Y1MDAyOGE4ZmY=' (length=184)
  string '{SSHA512.hex}4a1e8a61780f449ef6cbc883b5cf57279d32fd004cb7298ddb6f8c46bf246187c03f1bf9447044708767a826e65f977e5c95a490abf8f2c3ca90c7a0ea2b89e82fec1ee0940e7c436ef2037e89e4c06ca20b281a90dbb2d6cbd3534aa4ce7e19' (length=205)
  string '{SSHA512.b64}NGExZThhNjE3ODBmNDQ5ZWY2Y2JjODgzYjVjZjU3Mjc5ZDMyZmQwMDRjYjcyOThkZGI2ZjhjNDZiZjI0NjE4N2MwM2YxYmY5NDQ3MDQ0NzA4NzY3YTgyNmU2NWY5NzdlNWM5NWE0OTBhYmY4ZjJjM2NhOTBjN2EwZWEyYjg5ZTgyZmVjMWVlMDk0MGU3YzQzNmVmMjAzN2U4OWU0YzA2Y2EyMGIyODFhOTBkYmIyZDZjYmQzNTM0YWE0Y2U3ZTE5' (length=269)
  </code></pre>
  
  <p>Now when I try to verify the hashes against doveadm, only the first hash (non-salted in hex-format) works. The second one gives me an error regarding the length of the string (Input length isn't valid (128 instead of 64)). The third and fourth tells that the password isn't matching:</p>
  
  <pre><code>root@jupiter[0][75]:/etc/dovecot# dovecot pw -t {SHA512.hex}ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff -p test
  {SHA512.hex}ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff (verified)
  
  root@jupiter[0][0]:/etc/dovecot# dovecot pw -t {SHA512.b64}ZWUyNmIwZGQ0YWY3ZTc0OWFhMWE4ZWUzYzEwYWU5OTIzZjYxODk4MDc3MmU0NzNmODgxOWE1ZDQ5NDBlMGRiMjdhYzE4NWY4YTBlMWQ1Zjg0Zjg4YmM4ODdmZDY3YjE0MzczMmMzMDRjYzVmYTlhZDhlNmY1N2Y1MDAyOGE4ZmY= -p test
  doveadm(root): Fatal: reverse decode check failed: Input length isn't valid (128 instead of 64)
  
  root@jupiter[0][75]:/etc/dovecot# dovecot pw -t {SSHA512.hex}4a1e8a61780f449ef6cbc883b5cf57279d32fd004cb7298ddb6f8c46bf246187c03f1bf9447044708767a826e65f977e5c95a490abf8f2c3ca90c7a0ea2b89e82fec1ee0940e7c436ef2037e89e4c06ca20b281a90dbb2d6cbd3534aa4ce7e19 -p test
  doveadm(root): Fatal: reverse password verification check failed: Password mismatch
  
  root@jupiter[0][75]:/etc/dovecot# dovecot pw -t {SSHA512.b64}NGExZThhNjE3ODBmNDQ5ZWY2Y2JjODgzYjVjZjU3Mjc5ZDMyZmQwMDRjYjcyOThkZGI2ZjhjNDZiZjI0NjE4N2MwM2YxYmY5NDQ3MDQ0NzA4NzY3YTgyNmU2NWY5NzdlNWM5NWE0OTBhYmY4ZjJjM2NhOTBjN2EwZWEyYjg5ZTgyZmVjMWVlMDk0MGU3YzQzNmVmMjAzN2U4OWU0YzA2Y2EyMGIyODFhOTBkYmIyZDZjYmQzNTM0YWE0Y2U3ZTE5 -p test
  doveadm(root): Fatal: reverse password verification check failed: Password mismatch
  </code></pre>
  
  <p>I tried to find out how that works by reading several pages like</p>
  
  <ul>
  <li><a href="http://wiki2.dovecot.org/Authentication/PasswordSchemes" rel="nofollow">http://wiki2.dovecot.org/Authentication/PasswordSchemes</a></li>
  <li><a href="http://www.tunnelsup.com/using-salted-sha-hashes-with-dovecot-authentication" rel="nofollow">http://www.tunnelsup.com/using-salted-sha-hashes-with-dovecot-authentication</a></li>
  <li><a href="https://stackoverflow.com/questions/6713521/how-to-securely-generate-ssha256-or-ssha512-hashes-in-php">How to securely generate SSHA256 or SSHA512 hashes in PHP?</a></li>
  </ul>
  
  <p>and thought that I am doing it right. But: No no ;-)</p>
  
  <p>Can anyone pinpoint me to my mistake, please?</p>
  
  <p>Thank you very much!</p>