Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this a good hashing password function in PHP? If not, why not?
    primarykey
    data
    text
    <p>I'm wondering if this function (which is in part taken from a ~2 year old phpBB version), is good enough. </p> <p>If not, why?<br> And how would you change it (making the transition seamless for existing users) ?</p> <p>The result of hash_pwd() is what will be saved in a DB.</p> <p> <pre><code>function hash_pwd($password) { $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; $random_state = $this-&gt;unique_id(); $random = ''; $count = 6; if (($fh = @fopen('/dev/urandom', 'rb'))) { $random = fread($fh, $count); fclose($fh); } if (strlen($random) &lt; $count) { $random = ''; for ($i = 0; $i &lt; $count; $i += 16) { $random_state = md5($this-&gt;unique_id() . $random_state); $random .= pack('H*', md5($random_state)); } $random = substr($random, 0, $count); } $hash = $this-&gt;_hash_crypt_private($password, $this-&gt;_hash_gensalt_private($random, $itoa64), $itoa64); if (strlen($hash) == 34) { return $hash; } return false; } function unique_id() { $val = microtime(); $val = md5($val); return substr($val, 4, 16); } function _hash_crypt_private($password, $setting, &amp;$itoa64) { $output = '*'; // Check for correct hash if (substr($setting, 0, 3) != '$H$') { return $output; } $count_log2 = strpos($itoa64, $setting[3]); if ($count_log2 &lt; 7 || $count_log2 &gt; 30) { return $output; } $count = 1 &lt;&lt; $count_log2; $salt = substr($setting, 4, 8); if (strlen($salt) != 8) { return $output; } /** * We're kind of forced to use MD5 here since it's the only * cryptographic primitive available in all versions of PHP * currently in use. To implement our own low-level crypto * in PHP would result in much worse performance and * consequently in lower iteration counts and hashes that are * quicker to crack (by non-PHP code). */ if (PHP_VERSION &gt;= 5) { $hash = md5($salt . $password, true); do { $hash = md5($hash . $password, true); } while (--$count); } else { $hash = pack('H*', md5($salt . $password)); do { $hash = pack('H*', md5($hash . $password)); } while (--$count); } $output = substr($setting, 0, 12); $output .= $this-&gt;_hash_encode64($hash, 16, $itoa64); return $output; } function _hash_gensalt_private($input, &amp;$itoa64, $iteration_count_log2 = 6) { if ($iteration_count_log2 &lt; 4 || $iteration_count_log2 &gt; 31) { $iteration_count_log2 = 8; } $output = '$H$'; $output .= $itoa64[min($iteration_count_log2 + ((PHP_VERSION &gt;= 5) ? 5 : 3), 30)]; $output .= $this-&gt;_hash_encode64($input, 6, $itoa64); return $output; } function _hash_encode64($input, $count, &amp;$itoa64) { $output = ''; $i = 0; do { $value = ord($input[$i++]); $output .= $itoa64[$value &amp; 0x3f]; if ($i &lt; $count) { $value |= ord($input[$i]) &lt;&lt; 8; } $output .= $itoa64[($value &gt;&gt; 6) &amp; 0x3f]; if ($i++ &gt;= $count) { break; } if ($i &lt; $count) { $value |= ord($input[$i]) &lt;&lt; 16; } $output .= $itoa64[($value &gt;&gt; 12) &amp; 0x3f]; if ($i++ &gt;= $count) { break; } $output .= $itoa64[($value &gt;&gt; 18) &amp; 0x3f]; } while ($i &lt; $count); return $output; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload