Вот, резюмируя все сломанные в этом топике копья, я нарисовал такую функцию-обертку:
<?php
define('IRB_SALT', 'ds$e2(dx#s');
define('IRB_LOGIC', 012331230);
@param @param @param @param @return
function cryptPassword($password, $logic = 3, $hash = '', $round = 10)
{
if(CRYPT_MD5 != 1)
{
trigger_error (__FUNCTION__ .'(). '
.'The system does not support md5 algorithm',
E_USER_WARNING);
return false;
}
$string = 'abcdefghijklmnopqrstuvwxyz0123456789#$%.!-=(){}[]\/';
if(empty($hash))
{
$string = str_pad('', 108, $string);
$string = str_shuffle($string);
$rand = round(microtime(true) - floor(microtime(true)), 2) * 100;
$salt = substr($string, $rand, 8);
}
else
{
preg_match('~(.*)([a-f0-9]{32})$~ui', $hash, $out);
if(empty($out[1]) && !empty($out[0]))
$salt = substr($out[0], 0, 8);
else
$salt = substr(preg_replace('~[^'. preg_quote($string) .']~ui',
'-',
$hash),
0, 8);
}
$hash = crypt($password, '$1$'. $salt);
$crypt = array( array(CRYPT_MD5, '$1$'),
array(CRYPT_BLOWFISH, '$2y$07$'),
array(CRYPT_SHA256, '$5$rounds=1000$'),
array(CRYPT_SHA512, '$6$rounds=1000$'),
);
$logic = (string)$logic;
$cnt = strlen($logic);
$round = ($round < 1) ? 1 : $round;
$round = ($round > 1000) ? 1000 : $round;
while($round--)
{
for($i = 0; $i < $cnt; $i++)
{
if(empty($crypt[$logic[$i]][0]) || $crypt[$logic[$i]][0] != 1)
$crypt[$logic[$i]][1] = '$1$';
preg_match("~[^\$]+$~i", $hash, $out);
$hash = substr($out[0], 0, 8);
$hash = crypt($password, $crypt[$logic[$i]][1] . $hash);
}
}
return $salt . md5($hash);
}
$password = 'test';
echo 'Хэш пароля: '. cryptPassword($password . IRB_SALT, IRB_LOGIC);
echo '<br>';
$hash = 'u)mlx#qtaba14a93c286193f0bd600d13db2a816';
if(cryptPassword($password . IRB_SALT, IRB_LOGIC, $hash) == $hash)
echo 'Добро пожаловать';
Подробности тут.Теперь и моя концепция не нарушена и волки сыты.
Мленькая ремарка про стиль. Если это плохой стиль, если нельзя писать без скобок так
if(empty($crypt[$logic[$i]][0]) || $crypt[$logic[$i]][0] != 1)
$crypt[$logic[$i]][1] = '$1$';
то интересно, почему в таком стиле написан Yii...
Вот самый первый по порядку класс (CApplication):
public function run()
{
if($this->hasEventHandler('onBeginRequest'))
$this->onBeginRequest(new CEvent($this));
register_shutdown_function(array($this,'end'),0,false);
$this->processRequest();
if($this->hasEventHandler('onEndRequest'))
$this->onEndRequest(new CEvent($this));
}
Ведь за него сразу нужно расстреливать. :)
_____________
Если вам недостаточно собственных заблуждений, можно расширить их мнениями экспертов.Нужно уважать мнение оппонета. Ведь заблуждаться - его святое право.Настаивал, настаиваю и буду настаивать на своем. На кедровых орешках.