我知道php有md5(),sha1()和hash()函数,但是我想用MySQL PASSWORD()函数创建一个哈希.到目前为止,我能想到的唯一的办法是只查询服务器,但我想一个函数(最好是在PHP或Perl),将做同样的事情,而无需查询的MySQL在所有.
例如:
MySQL哈希 - > 464bb2cb3cf18b66
MySQL5哈希 - >*01D01F5CA7CA8BA771E03F4AC55EC73C11EFA229
谢谢!
def*_*nes 27
我最初在我自己搜索两个MySQL密码散列函数的PHP实现时偶然发现了这个问题.我无法找到任何实现,所以我从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 Defines 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 Defines 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
Run Code Online (Sandbox Code Playgroud)
希望其他人会发现这也很有用:)
是的,为时已晚,但我刚刚在该页面上提出了这个实现:http://dev.mysql.com/doc/refman/5.1/en/password-hashing.html
这是mysql密码的等效php函数;
function mysql_41_password($in) {
$p = sha1($in, true);
$p = sha1($p);
return '*'. strtoupper($p);
}
Run Code Online (Sandbox Code Playgroud)
为什么要使用mysql的password()函数?甚至 Mysql 文档也建议不要这样做:
http://dev.mysql.com/doc/refman/5.0/en/encryption-functions.html#function_password
PASSWORD()函数供MySQL Server中的身份验证系统使用;你不应该在你自己的应用程序中使用它
例如,您可以使用 md5(),它几乎存在于所有编程语言中,包括 php 和 perl。