所以我试图实现一个系统,其中用户身份验证基于外部 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(password, the_same_function(但给出完全不同的值);
成功地
我觉得我在这里遗漏了一些明显的东西,有人可以解释一下吗?我知道这是基本的。
再次感谢
这里的主要魔法有两行:
你提到的那个:
return crypt($strPassword, '$6$' . **md5(uniqid(mt_rand(), true))** . '$');
以及检查密码是否更改的那个:
if (crypt($pw, $this->User->password) === $this->User->password)
第一个生成密码的盐,第二个来自数据库的散列密码用于提取必须在crypt()
方法中使用的盐
在这里您有更多信息:http : //php.net/manual/en/faq.passwords.php#faq.passwords.salt
减号可能是因为你一开始没有做足够的工作(这让我觉得更难)你没有放密码验证码,也没有放你的python代码。
我认为您现在已经足够编写自己的代码了。查看 python hashlib 文档https://docs.python.org/3/library/hashlib.html并使用盐。
如果不发布您的python代码,我希望它足以让您前进。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句