【问题标题】:salts stored wrong in database.!盐在数据库中存储错误。!
【发布时间】:2017-01-04 01:18:21
【问题描述】:

我的测试功能:

<?php

include_once("core/init.php");

$admin = new Admin();
$name = "akhil";
$password = "daydreamers";
$salt = Hash::salt(24);
$hash = Hash::make($password,$salt);
/*echo $hash;
echo "<br/>";*/
$admin->newAdmin($name,$hash,$salt);
$dsalt = $admin->getSalt($name);
if($salt != $dsalt){
    echo "Wrong";
}
/*echo Hash::make($password,$dsalt);
echo "<br/>";
//$admin->verify($name,$password);
echo $admin->getPassword($name);*/

?>

哈希类:

<?php

class Hash{
    public static function make($string,$salt=''){
        return hash('sha256', $string . $salt);
    }
    public static function salt($length){
        return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
    }
}

?>

database structure

我存储的盐和从数据库中检索的盐不匹配。我浏览了其他似乎建议增加列大小但不起作用的帖子。

【问题讨论】:

    标签: php salt


    【解决方案1】:

    由于您正在使用密码,我建议切换到password_hash() 功能,忘记上面的不安全实现。 password_hash() 函数会处理盐,您不需要单独存储它,只需将哈希存储在单个 varchar(255) 字段中即可。

    // Hash a new password for storing in the database.
    // The function automatically generates a cryptographically safe salt.
    $hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);
    
    // Check if the hash of the entered login password, matches the stored hash.
    // The salt and the cost factor will be extracted from $existingHashFromDb.
    $isPasswordCorrect = password_verify($password, $existingHashFromDb);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-27
      • 2014-09-08
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      相关资源
      最近更新 更多