2012-10-11 28 views
1

我使用mac作为web服务器,并将codeigniter作为框架。为了认证,我申请了坦克认证。发生一个问题:在不同的系统(Mac和Linux)中创建不同的密码哈希码

之前:我使用mac作为web服务器,登录是好的。

现在:我用linux作为web服务器。我导入了相同的数据库,并且网站运行良好。但是,我无法登录。

所以我测试了在mac和linux中使用相同的密码注册,发现它创建了不同的密码哈希码。

的Linux:

$P$Bh3B8uGDw0yGO1e/ytCUw2jXcswkso1 

的Mac:

$2a$08$jBCiR79fHN6xzOw5sB09beFifwU08nQdO0Au8P3hxSvIUnoepKfwW 

我的问题是:是关于系统这个问题?或PHP版本?

或者它是关于php的md5()函数?

我提出密码哈希的一些代码罐身份验证:

function PasswordHash($iteration_count_log2, $portable_hashes) 
{ 
    $this->itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 

    if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) 
     $iteration_count_log2 = 8; 
    $this->iteration_count_log2 = $iteration_count_log2; 

    $this->portable_hashes = $portable_hashes; 

    $this->random_state = microtime(); 
    if (function_exists('getmypid')) 
     $this->random_state .= getmypid(); 
} 

function get_random_bytes($count) 
{ 
    $output = ''; 
    if (is_readable('/dev/urandom') && 
     ($fh = @fopen('/dev/urandom', 'rb'))) { 
     $output = fread($fh, $count); 
     fclose($fh); 
    } 

    if (strlen($output) < $count) { 
     $output = ''; 
     for ($i = 0; $i < $count; $i += 16) { 
      $this->random_state = 
       md5(microtime() . $this->random_state); 
      $output .= 
       pack('H*', md5($this->random_state)); 
     } 
     $output = substr($output, 0, $count); 
    } 

    return $output; 
} 

function encode64($input, $count) 
{ 
    $output = ''; 
    $i = 0; 
    do { 
     $value = ord($input[$i++]); 
     $output .= $this->itoa64[$value & 0x3f]; 
     if ($i < $count) 
      $value |= ord($input[$i]) << 8; 
     $output .= $this->itoa64[($value >> 6) & 0x3f]; 
     if ($i++ >= $count) 
      break; 
     if ($i < $count) 
      $value |= ord($input[$i]) << 16; 
     $output .= $this->itoa64[($value >> 12) & 0x3f]; 
     if ($i++ >= $count) 
      break; 
     $output .= $this->itoa64[($value >> 18) & 0x3f]; 
    } while ($i < $count); 

    return $output; 
} 
    function HashPassword($password) 
{ 
    $random = ''; 

    if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) { 
     $random = $this->get_random_bytes(16); 
     $hash = 
      crypt($password, $this->gensalt_blowfish($random)); 
     if (strlen($hash) == 60) 
      return $hash; 
    } 

    if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) { 
     if (strlen($random) < 3) 
      $random = $this->get_random_bytes(3); 
     $hash = 
      crypt($password, $this->gensalt_extended($random)); 
     if (strlen($hash) == 20) 
      return $hash; 
    } 

    if (strlen($random) < 6) 
     $random = $this->get_random_bytes(6); 
    $hash = 
     $this->crypt_private($password, 
     $this->gensalt_private($random)); 
    if (strlen($hash) == 34) 
     return $hash; 

    # Returning '*' on error is safe here, but would _not_ be safe 
    # in a crypt(3)-like function used _both_ for generating new 
    # hashes and for validating passwords against existing hashes. 
    return '*'; 
} 

任何想法?我在谷歌搜索这个问题,但得到的信息很少,你能否帮我?谢谢你..

哦,也许是关于CRYPT_BLOWFISH。 Linux没有CRYPT_BLOWFISH。

+0

参考http://en.wikipedia.org/wiki/Salt_(cryptography) –

回答

2

我自己得到了解决方案。

这个问题是关于PHP版本。

php 5.2不支持CRYPT_BLOWFISH,但是php 5.3是可以的。

谢谢大家。