从PHP服务访问cakePHP哈希密码

时间:2013-08-20 08:53:25

标签: php cakephp passwords cakephp-2.0

我们有一些应用程序使用相同的数据库。用户密码由cakePHP应用程序进行哈希处理。我们想要做的是比较php服务的散列密码和cakePHP散列的密码。

是否有一个PHP函数可以模仿CakePHP的散列来比较密码?如果没有,最简单的方法是什么?

1 个答案:

答案 0 :(得分:1)

我相信CakePHP使用hash内的函数lib\Cake\Utility\Security.php来获取用户的散列数据,并将其与密码字段中存储的散列进行比较:

https://github.com/cakephp/cakephp/blob/master/lib/Cake/Utility/Security.php#L107

我也会说它默认使用PHP的sha1函数,它使用用户的passwordSecurity.salt值(在core.php中定义)作为输入字符串。 因此,您可以执行以下操作以将值保存在password表的users字段中:

sha1('cce93fda02c7f3ebf1g46c583589f1fd257e9d5d'. 'mypassword');

这是CakePHP中使用sha1

的完整功能
public static function hash($string, $type = null, $salt = false) {
    if (empty($type)) {
        $type = self::$hashType;
    }
    $type = strtolower($type);

    if ($type === 'blowfish') {
        return self::_crypt($string, $salt);
    }
    if ($salt) {
        if (!is_string($salt)) {
            $salt = Configure::read('Security.salt');
        }
        $string = $salt . $string;
    }

    if (!$type || $type === 'sha1') {
        if (function_exists('sha1')) {
            return sha1($string);
        }
        $type = 'sha256';
    }

    if ($type === 'sha256' && function_exists('mhash')) {
        return bin2hex(mhash(MHASH_SHA256, $string));
    }

    if (function_exists('hash')) {
        return hash($type, $string);
    }
    return md5($string);
}

您可以在CakePHP documentation中了解更多相关信息。