从PHP服务访问cakeHP散列密码


Accessing the cakePHP hashed passwords from PHP service

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

有没有一个PHP函数可以模仿CakePHP的哈希来比较密码?如果没有,绕过它最简单的方法是什么?

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

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

我还想说,它通常默认使用PHP的sha1函数,该函数使用用户的passwordSecurity.salt值(在core.PHP中定义)作为输入字符串。因此,您可以执行类似的操作来获得保存在users表的password字段上的值:

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文档中阅读更多关于它的信息。