MySQL哈希函数实现 [英] MySQL Hashing Function Implementation

查看:2932
本文介绍了MySQL哈希函数实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道php有md5(),sha1()和hash()函数,但我想使用MySQL PASSWORD()函数创建一个哈希。到目前为止,我能想到的唯一方法就是查询服务器,但我想要一个函数(最好在php或Perl中),它们可以在不查询MySQL的情况下执行相同的操作。



例如:

MySQL hash - > 464bb2cb3cf18b66

MySQL5哈希 - > * 01D01F5CA7CA8BA771E03F4AC55EC73C11EFA229



谢谢!

解决方案

我原本在我的这个问题上偶然发现自己搜索两个MySQL密码哈希函数的PHP实现。我无法找到任何实现,所以我从MySQL源代码(sql / password.c)中调整了我自己的实现。以下内容在PHP 5.2中进行了测试和工作:

  //以下免费用于任何用途,只要信用在适当的地方给出。 
//此代码不附带任何形式的担保,包括任何隐含的担保。
$ b / **
* MySQLOLD_PASSWORD()也称为MySQL323哈希函数
*这是MySQL 4.1.1之前的密码哈希函数
*由Rev. Dustin Fineout 10/9/2009 9:12:16 AM
** /
函数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& 63)+ $ add)* $ tmp)+(($ nr< 8)& 0xFFFFFFFF);
$ nr ^
$ 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函数mysql_old_password_hash
$ b $ ** / **
* MySQLPASSWORD()另存为MySQLSHA1 HASH函数
*这是MySQL中使用的密码散列函数从4.1.1版本开始
*由Rev. Dustin Fineout 10/9/2009 9:36:20 AM
** /
函数mysql_password_hash($ input,$ hex = true)
{
$ sha1_stage1 = sha1($ input,true);
$ output = sha1($ sha1_stage1,!$ hex);
return $ output;
} // END函数mysql_password_hash
$ b $ / **
*将每个十六进制对计算成相应的二进制八位字节。
*与MySQL hex2octet函数类似。
** /
函数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;
}
返回$ bin;
} // END函数hex_hash_to_bin

希望别人也能找到这个有用的:)

I know that php has md5(), sha1(), and the hash() functions, but I want to create a hash using the MySQL PASSWORD() function. So far, the only way I can think of is to just query the server, but I want a function (preferably in php or Perl) that will do the same thing without querying MySQL at all.

For example:

MySQL hash -> 464bb2cb3cf18b66

MySQL5 hash -> *01D01F5CA7CA8BA771E03F4AC55EC73C11EFA229

Thanks!

解决方案

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:

// 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

Hopefully someone else will find this useful as well :)

这篇关于MySQL哈希函数实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆