mysql散列 in_php – MySQL散列函数实现

我本来在这个问题上偶然发现我自己寻找一个

PHP实现的两个MySQL密码哈希函数.我无法找到任何实现,所以我自己从MySQL源代码(sql / password.c)修改了自己.以下测试和工作在PHP 5.2:

// 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 < $inlen; $i++) {

$byte = substr($input, $i, 1);

if ($byte == ' ' || $byte == "\t") continue;

$tmp = ord($byte);

$nr ^= ((($nr & 63) + $add) * $tmp) + (($nr << 8) & 0xFFFFFFFF);

$nr2 += (($nr2 << 8) & 0xFFFFFFFF) ^ $nr;

$add += $tmp;

}

$out_a = $nr & ((1 << 31) - 1);

$out_b = $nr2 & ((1 << 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 < $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

希望别人会发现这个有用的:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值