将加密方法从php转换为python

2024-06-08 15:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试实现一个系统,其中用户身份验证基于外部sql

这个外部sql出现在我的第一个网站上,我有很多用户,我只想让他们用相同的凭据登录

数据库中存储的密码是加密的,所以我需要找出加密方法。我完全可以访问该网站,因此我找到了以下php代码(该网站基于php):

/**
 * Generate a password hash
 *
 * @param string $strPassword The unencrypted password
 *
 * @return string The encrypted password
 *
 * @throws \Exception If none of the algorithms is available
 */
public static function hash($strPassword)
{
    if (CRYPT_SHA512 == 1)
    {
        return crypt($strPassword, '$6$' . md5(uniqid(mt_rand(), true)) . '$');
    }
    elseif (CRYPT_SHA256 == 1)
    {
        return crypt($strPassword, '$5$' . md5(uniqid(mt_rand(), true)) . '$');
    }
    elseif (CRYPT_BLOWFISH == 1)
    {
        return crypt($strPassword, '$2a$07$' . md5(uniqid(mt_rand(), true)) . '$');
    }
    else
    {
        throw new \Exception('None of the required crypt() algorithms is available');
    }
}

还有这个:

/**
 * Run the controller and parse the password template
 */
public function run()
{
    $this->Template = new BackendTemplate('be_password');

    if (Input::post('FORM_SUBMIT') == 'tl_password')
    {
        $pw = Input::postRaw('password');
        $cnf = Input::postRaw('confirm');

        // The passwords do not match
        if ($pw != $cnf)
        {
            Message::addError($GLOBALS['TL_LANG']['ERR']['passwordMatch']);
        }
        // Password too short
        elseif (utf8_strlen($pw) < Config::get('minPasswordLength'))
        {
            Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['passwordLength'], Config::get('minPasswordLength')));
        }
        // Password and username are the same
        elseif ($pw == $this->User->username)
        {
            Message::addError($GLOBALS['TL_LANG']['ERR']['passwordName']);
        }
        // Save the data
        else
        {
            // Make sure the password has been changed
            if (crypt($pw, $this->User->password) === $this->User->password)
            {
                Message::addError($GLOBALS['TL_LANG']['MSC']['pw_change']);
            }
            else
            {
                $this->loadDataContainer('tl_user');

                // Trigger the save_callback
                if (is_array($GLOBALS['TL_DCA']['tl_user']['fields']['password']['save_callback']))
                {
                    foreach ($GLOBALS['TL_DCA']['tl_user']['fields']['password']['save_callback'] as $callback)
                    {
                        if (is_array($callback))
                        {
                            $this->import($callback[0]);
                            $pw = $this->$callback[0]->$callback[1]($pw);
                        }
                        elseif (is_callable($callback))
                        {
                            $pw = $callback($pw);
                        }
                    }
                }

                $objUser = UserModel::findByPk($this->User->id);
                $objUser->pwChange = '';
                $objUser->password = Encryption::hash($pw);
                $objUser->save();

                Message::addConfirmation($GLOBALS['TL_LANG']['MSC']['pw_changed']);
                $this->redirect('contao/main.php');
            }
        }

        $this->reload();
    }

我的问题是:

如何将此方法转换为python

我不太明白随机性和如何重复它。库恩;有人解释吗

谢谢你

C级

//编辑:

我是说我不知道在哪里

return crypt($strPassword, '$6$' . **md5(uniqid(mt_rand(), true))** . '$');

这个中间部分被保存了,因此它怎么能和crypt(密码,相同的函数(但是给出了完全不同的值)相比较呢

成功

我觉得我遗漏了一些明显的东西,有人能解释一下吗?我知道这很基本

再次感谢


Tags: themessagelangreturnifiscallbackpassword
1条回答
网友
1楼 · 发布于 2024-06-08 15:18:25

这里的主要魔法有两行:

你提到的那个:

return crypt($strPassword, '$6$' . **md5(uniqid(mt_rand(), true))** . '$');

以及检查密码是否更改的程序:

if (crypt($pw, $this->User->password) === $this->User->password)

第一种是生成密码的salt,第二种是使用数据库中的散列密码来提取salt,salt必须在crypt()方法中使用

这里有更多信息:http://php.net/manual/en/faq.passwords.php#faq.passwords.salt

负可能是因为你没有投入足够的工作开始和(事情让我更难)你没有把你的密码验证码,你没有把你的python代码

我想你现在已经有足够的时间来写你自己的代码了。查看python hashlib文档https://docs.python.org/3/library/hashlib.html和使用salt

如果不发布python代码,我希望这足以让您继续

相关问题 更多 >