在 WordPress 中是如何加密和验证用户的密码的呢?WordPress 主要使用了两个函数:wp_hash_password()
和 wp_check_password()
:
wp_hash_password($password)
把一个纯文本加密成密文。
function wp_hash_password( $password ) { global $wp_hasher; if ( empty( $wp_hasher ) ) { require_once ABSPATH . WPINC . '/class-phpass.php'; // By default, use the portable hash from phpass. $wp_hasher = new PasswordHash( 8, true ); } return $wp_hasher->HashPassword( trim( $password ) ); }
wp_check_password($password, $hash, $user_id = '')
把纯文本和密文进行比对来验证密码。
function wp_check_password( $password, $hash, $user_id = '' ) { global $wp_hasher; if ( empty( $wp_hasher ) ) { require_once ABSPATH . WPINC . '/class-phpass.php'; $wp_hasher = new PasswordHash( 8, true ); } $check = $wp_hasher->CheckPassword( $password, $hash ); return apply_filters( 'check_password', $check, $password, $hash, $user_id ); }
从上面的代码可以看出,WordPress 是使用一个 phpass(全称是:Portable PHP password hashing framework)开源的类生成和验证密码的。