Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I originally stumbled across this question in my own search for a PHP implementation of the two MySQL password hashing functions. I was unable to find any implementations, so I adapted my own from the MySQL source code (sql/password.c). The following are tested and working in PHP 5.2:</p> <pre><code>// The following is free for any use provided credit is given where due. // This code comes with NO WARRANTY of any kind, including any implied warranty. /** * MySQL "OLD_PASSWORD()" AKA MySQL323 HASH FUNCTION * This is the password hashing function used in MySQL prior to version 4.1.1 * By Rev. Dustin Fineout 10/9/2009 9:12:16 AM **/ function mysql_old_password_hash($input, $hex = true) { $nr = 1345345333; $add = 7; $nr2 = 0x12345671; $tmp = null; $inlen = strlen($input); for ($i = 0; $i &lt; $inlen; $i++) { $byte = substr($input, $i, 1); if ($byte == ' ' || $byte == "\t") continue; $tmp = ord($byte); $nr ^= ((($nr &amp; 63) + $add) * $tmp) + (($nr &lt;&lt; 8) &amp; 0xFFFFFFFF); $nr2 += (($nr2 &lt;&lt; 8) &amp; 0xFFFFFFFF) ^ $nr; $add += $tmp; } $out_a = $nr &amp; ((1 &lt;&lt; 31) - 1); $out_b = $nr2 &amp; ((1 &lt;&lt; 31) - 1); $output = sprintf("%08x%08x", $out_a, $out_b); if ($hex) return $output; return hex_hash_to_bin($output); } //END function mysql_old_password_hash /** * MySQL "PASSWORD()" AKA MySQLSHA1 HASH FUNCTION * This is the password hashing function used in MySQL since version 4.1.1 * By Rev. Dustin Fineout 10/9/2009 9:36:20 AM **/ function mysql_password_hash($input, $hex = true) { $sha1_stage1 = sha1($input, true); $output = sha1($sha1_stage1, !$hex); return $output; } //END function mysql_password_hash /** * Computes each hexidecimal pair into the corresponding binary octet. * Similar to mysql hex2octet function. **/ function hex_hash_to_bin($hex) { $bin = ""; $len = strlen($hex); for ($i = 0; $i &lt; $len; $i += 2) { $byte_hex = substr($hex, $i, 2); $byte_dec = hexdec($byte_hex); $byte_char = chr($byte_dec); $bin .= $byte_char; } return $bin; } //END function hex_hash_to_bin </code></pre> <p>Hopefully someone else will find this useful as well :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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