title: Many hash iterations: append salt every time?
body: <p>I have used unsalted md5/sha1 for long time, but as this method isn't really secure (and is getting even less secure as time goes by) I decided to switch to a salted sha512. Furthermore I want to slow the generation of the hash down by using many iterations (e.g. 100).</p>
  
  <p>My question is whether I should append the salt on every iteration or only once at the beginning. Here are the two possible codes:</p>
  
  <p><strong>Append every time:</strong></p>
  
  <pre><code>// some nice big salt
  $salt = hash($algorithm, $salt);
  
  // apply $algorithm $runs times for slowdown
  while ($runs--) {
      $string = hash($algorithm, $string . $salt, $raw);
  }
  
  return $string;
  </code></pre>
  
  <p><strong>Append once:</strong></p>
  
  <pre><code>// add some nice big salt
  $string .= hash($algorithm, $salt);
  
  // apply $algorithm $runs times for slowdown
  while ($runs--) {
      $string = hash($algorithm, $string, $raw);
  }
  
  return $string;
  </code></pre>
  
  <p>I first wanted to use the second version (append once) but then found some scripts appending the salt every time.</p>
  
  <p>So, I wonder whether adding it every time adds some strength to the hash. For example, would it be possible that an attacker found some clever way to create a 100timesSha512 function which were way faster than simply executing sha512 100 times?</p>