问题描述
Possible Duplicate:
How to validate WordPress generated password in DB using PHP?
我正在使用与 WordPress 完成的网站,我需要添加一些 WP 以外的部分,并检查用户登录,但我无法找到 WP 在密码写入 DB 之前如何加密密码…
我试过 md5 但不是…
有没有人知道如何检查 WP 外的密码,而不使用它的插件/检查器,清除 PHP 代码?
最佳解决方案
密码加密库位于/wp-includes/class-phpass.php 。它是 Portable PHP Password hashing framework 。
次佳解决方案
这听起来像是要使用外部代码来验证 WP 数据库的用户名/密码。如果这是正确的,你将花费大量的时间重塑轮子。但是,关于如何使用直接 PHP 的一个很好的例子,看一下已经做到的核心 WP 功能是一个好主意。
例子,`wp_check_password()’:
function wp_check_password($password, $hash, $user_id = '') {
global $wp_hasher;
// If the hash is still md5...
if ( strlen($hash) <= 32 ) {
$check = ( $hash == md5($password) );
if ( $check && $user_id ) {
// Rehash using new hash.
wp_set_password($password, $user_id);
$hash = wp_hash_password($password);
}
return apply_filters('check_password', $check, $password, $hash, $user_id);
}
// If the stored hash is longer than an MD5, presume the
// new style phpass portable hash.
if ( empty($wp_hasher) ) {
require_once ( ABSPATH . 'wp-includes/class-phpass.php');
// By default, use the portable hash from phpass
$wp_hasher = new PasswordHash(8, TRUE);
}
$check = $wp_hasher->CheckPassword($password, $hash);
return apply_filters('check_password', $check, $password, $hash, $user_id);
}
首先,WordPress 检查用户的散列密码是否仍然使用 old-school MD5 进行安全。这是为了保持更新的向后兼容性。如果密码为 MD5,则 WordPress 将使用新系统 (调用 wp_set_password()
) 自动将其替换为新散列。如果不是 MD5,则 WP 移动到较新的哈希设置。
首先,我们包括便携式 PHP 散列框架 (在另一个答案中已经由 @John Watson 提到),并创建了一个实例,将其存储在全局 $wp_hasher
变量中。
然后,我们使用库的 CheckPassword()
方法传递明文密码和哈希值来对其进行验证。
如果要在外部库中使用,您必须首先使用 include
/require
库,然后将其实例化,然后传入纯文本密码及其哈希值。所以一些未经测试的 psuedo-code …
function validate_password( $plaintext, $hash ) {
require_one( 'class-phpass.php' );
$hasher = new PasswordHash(8, TRUE);
return $hasher->CheckPassword( $plaintext, $hash );
}
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。