关于 WordPress 密码生成过程

Nov 13, 2009

以下内容恢复自 Wordpress 时期的数据库备份,内容已经严重过期,仅留作纪念。


最近在摆弄数据库的时候,无意间瞥见 WordPress 数据库里保存的密码全是一长串以 $P$ 开头的字符。这一下激起了我的好奇心,于是顺藤摸瓜找出了 WP 生成密码的底层代码,打算好好研究一番。

简易版调用代码

<?php
require( './wp-includes/class-phpass.php');
$psw = '123456';
global $psw1;
$psw1 = new PasswordHash(8, TRUE);
echo $psw1->HashPassword($psw);
?>

密码的生成过程

简单来说,它的加密流程主要分为下面这几步:

  • 随机产生一个 salt(盐值),然后把这个 salt 和明文密码拼接在一起。
  • 接着进行 count 次循环的 MD5 加密,再将得到的 hash 值进行 base64 编码(encode64)累加。
  • 最后,在生成的字符串前加上特定的 $P$ 前缀,大功告成!

简易版代码说明

最近恰好在努力学习 PHP,所以试着自己写了这么一小段代码。 只要把这段代码丢到 WordPress 的根目录下运行,就能快速生成一个加密后的密码了。 首先,代码里引用了核心的 class-phpass.php 文件。 接着,把你想要加密的明文密码赋给 $psw 这个变量。 最后,用 $psw1 代表并实例化一个 PasswordHash 类,调用一下它的生成方法就搞定了!

完整版底层代码

如果不想依赖 WordPress 的环境,可以直接把核心类搬出来单独用。下面是提取出的完整版代码:

<?php

class PasswordHash {
var $itoa64;
var $iteration_count_log2;
var $portable_hashes;
var $random_state;

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

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() . 
(function_exists('getmypid') ? getmypid() : '') . uniqid(rand(), TRUE);

}

function get_random_bytes($count)
{
$output = '';
if (($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 gensalt_private($input)
{
$output = '$P$';
$output .= $this->itoa64[min($this->iteration_count_log2 +
((PHP_VERSION >= '5') ? 5 : 3), 30)];
$output .= $this->encode64($input, 6);

return $output;
}

function crypt_private($password, $setting)
{
$output = '*0';
if (substr($setting, 0, 2) == $output)
$output = '*1';

if (substr($setting, 0, 3) != '$P$')
return $output;

$count_log2 = strpos($this->itoa64, $setting[3]);
if ($count_log2 < 7 || $count_log2 > 30)
return $output;

$count = 1 << $count_log2;

$salt = substr($setting, 4, 8);
if (strlen($salt) != 8)
return $output;

if (PHP_VERSION >= '5') {
$hash = md5($salt . $password, TRUE);
do {
$hash = md5($hash . $password, TRUE);
} while (--$count);
} else {
$hash = pack('H*', md5($salt . $password));
do {
$hash = pack('H*', md5($hash . $password));
} while (--$count);
}

$output = substr($setting, 0, 12);
$output .= $this->encode64($hash, 16);

return $output;
}

function gensalt_extended($input)
{
$count_log2 = min($this->iteration_count_log2 + 8, 24);
$count = (1 << $count_log2) - 1;

$output = '_';
$output .= $this->itoa64[$count & 0x3f];
$output .= $this->itoa64[($count >> 6) & 0x3f];
$output .= $this->itoa64[($count >> 12) & 0x3f];
$output .= $this->itoa64[($count >> 18) & 0x3f];

$output .= $this->encode64($input, 3);

return $output;
}

function gensalt_blowfish($input)
{
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

$output = '$2a$';
$output .= chr(ord('0') + $this->iteration_count_log2 / 10);
$output .= chr(ord('0') + $this->iteration_count_log2 % 10);
$output .= '$';

$i = 0;
do {
$c1 = ord($input[$i++]);
$output .= $itoa64[$c1 >> 2];
$c1 = ($c1 & 0x03) << 4;
if ($i >= 16) {
$output .= $itoa64[$c1];
break;
}

$c2 = ord($input[$i++]);
$c1 |= $c2 >> 4;
$output .= $itoa64[$c1];
$c1 = ($c2 & 0x0f) << 2;

$c2 = ord($input[$i++]);
$c1 |= $c2 >> 6;
$output .= $itoa64[$c1];
$output .= $itoa64[$c2 & 0x3f];
} while (1);

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;

return '*';
}

function CheckPassword($password, $stored_hash)
{
$hash = $this->crypt_private($password, $stored_hash);
if ($hash[0] == '*')
$hash = crypt($password, $stored_hash);

return $hash == $stored_hash;
}
}

$psw = '123456';
global $psw1;
$psw1 = new PasswordHash(8, TRUE);
echo $psw1->HashPassword($psw);

?>

完整代码段说明

上面这段代码本身就已经把 class-phpass.php 的核心实现包含进去了,所以你完全不必再去 require 那个源文件。 它的优点在于可以独立运行——你不需要像上一段代码那样非得把它丢在 WP 根目录下,随便找个能跑 PHP 的环境执行一下,它就能顺畅生成加密字符。


[back]