PDA

View Full Version : سوال: function قوی برای هش پسورد



soroush.r70
شنبه 19 اسفند 1391, 14:01 عصر
دوستان کسی یه فانکشن قوی برای هش پسورد سراغ نداره تا هم من و هم بقیه دوستان استفاده کنند..؟

Javidhb
شنبه 19 اسفند 1391, 16:06 عصر
http://www.openwall.com/phpass

rezaonline.net
شنبه 19 اسفند 1391, 16:50 عصر
<?php
class reHash
{
private $round = NULL ,
$salt = NULL ,
$error = array() ,
$configSet = false;

const defaultRound = 4000 ,
defaultSalt = 'aTyTIOPdf15AdswPOS8a9erpOaq8';


static private $ins = NULL ;


//return instance
static public function ins()
{
if(is_null(self::$ins))
self::$ins = new self ;
return self::$ins;
}

// set config only one time!
public function setConfig( array $con = array())
{
if($this->configSet)
{
$this->error[] = 'Config was setted !';
return false;
}
else
{
$this->configSet = true;
if(empty($con['round']))
$this->round = self::defaultRound;
else
$this->round = $con['round'];

if(empty($con['salt']))
$this->salt = self::defaultSalt;
else
$this->salt = $con['salt'];

return true;
}
}


// random character
static public function randomChar($max=8 , $salt='')
{
$max = (int) $max ;
$str = $salt . time() . mt_rand(1,80). $salt . $_SERVER['REMOTE_ADDR'] .microtime(true);
$random = md5($str);
for(;;)
{
$random .= md5($random);
if(strlen($random) >= $max)
break;
}
return substr($random , 1 , $max);
}


//random number
static public function randomNum($max = 8 , $salt='')
{
$max = (int) $max;
$num = preg_replace('@([^1-9])@','',self::randomChar($max*3,$salt));
for(;;)
{
$num .= preg_replace('@([^0-9])@','',md5($num)) ;
if(strlen($num) >= $max)
break;
}
return substr($num , 1 , $max);
}


//create hash
public function create($pass = '')
{
if( ! $this->configSet)
$this->error[] = 'Config not setted yet!';

if(empty($pass))
{
$this->error[] = 'Password can not be empty in `create` method!';
return false;
}

//create
$rand = self::randomNum(16,$this->salt);

$this->round += 5 ;
for($i=0 ; $i<=$this->round ; $i++)
$hash = hash('sha256', $this->salt.$rand.$pass, true);

return base64_encode($this->round .'*'.$rand.'*'.$hash);
}

//check
public function check($pass = '' , $hash='')
{
if( ! $this->configSet)
$this->error[] = 'Config not setted yet!';

if(empty($pass))
{
$this->error[] = 'Password can not be empty in `check` method!';
return false;
}

if(empty($hash))
{
$this->error[] = 'Hash can not be empty in `check` method!';
return false;
}

//check
$str = explode('*',base64_decode($hash));
$round = $str[0];
$rand = $str[1];
$_hash = $str[2];
for($i=0 ; $i<=$round ; $i++)
$hash = hash('sha256', $this->salt.$rand.$pass, true);
return $_hash === $hash;
}

//return error
public function getError()
{
return $this->error;
}
}


/// usage \\\
/*
reHash::ins()->setConfig(array(
'salt'=>'a78958yTIOPdf15AswPOS8a9ddddddddderperfg' ,
'round'=>5000,
));

echo $reza_hash = reHash::ins()->create('reza');
echo '<hr>';



echo reHash::ins()->check('reza',$reza_hash);

echo '<hr>';
print_r(reHash::ins()->getError());
*/
مثالهاش خطهای پائین هست

eshpilen
شنبه 19 اسفند 1391, 21:40 عصر
من اینو طراحی و استفاده میکنم؛ فکر کنم در مجموع درحال حاضر کافی باشه: http://www.hamidreza-mz.tk/?p=519
ضمنا خیلی کدهای دیگر رو اگر دقت کنید، مجهز به تابع رندوم درحد استانداردهای Cryptography نیستن؛ البته تابع رندوم من هم شاید استاندارد نباشه، اما از اون کدهای دیگه بسیار سرتره.
چون عملیات اساسا در مقولهء رمزنگاری است، پس باید تابع رندوم استفاده شده هم از نوع امنیتی باشه.