注册Get registration datetime并哈希

时间:2014-10-03 06:24:51

标签: php mysql datetime passwords

我只想通过将注册date_time作为盐来哈希我的用户密码。我将使用此功能:

function create_hash($pass, $created_date, $hash_method = 'md5') {
// the salt will be the reverse of the user's created date
// in seconds since the epoch
$salt = strrev(date('U', strtotime($created_date));
if (function_exists('hash') && in_array($hash_method, hash_algos()) {
    return hash($hash_method, $salt.$pass);
}
return md5($salt.$pass);
}

还有类似的密码验证方法。但我正在考虑注册过程。在我的注册表单中,我只有用户名和密码字段,在我的mysql数据库中,创建日期将自动添加。

但是第一次注册时我还需要使用creation_date进行哈希密码?怎么克服这个?所以基本上我想象我必须先获取creation_date才能在数据库中插入数据进行盐析。怎么做?

我想的一种方法是,在我的注册表单中调用此函数之前我会这样做:

$created_date = date("Y-m-d H:i:s");
$salt = strrev(date('U', strtotime($created_date));

但我担心这个盐时间和数据库的creation_date时间之间会有一个小的时间差。如果是这样,那么我的password_validation将无效..

***顺便说一句,我知道md5()不是安全的。所以我不需要关于md5或其他哈希算法的建议。我只对腌制感兴趣

2 个答案:

答案 0 :(得分:0)

Maeby最好写一个创建盐的函数

function salt($n=3)
{
     $key = '';
     $pattern = '1234567890abcdefghijklmnopqrstuvwxyz.,*_-=+#@!&^%';
     $counter = strlen($pattern)-1;
     for($i=0; $i<$n; $i++)
     {
         $key .= $pattern{rand(0,$counter)};
     }
     return $key;
}

当新用户注册调用此功能并执行以下操作时:

$salt=salt();
$hash_pass=md5(md5($_POST['password']).$salt);
mysql_query("INSERT INTO `my_table` SET `name`='".$_POST["name"]."', `pass`='".$hash_pass."' `regDate`=NOW() ");

当用户进入时,您从表中选择用户数据作为名称,并将他的散列密码与已发布的密码与他的盐进行比较

答案 1 :(得分:0)

你可以拥有的最好的盐,不是来自任何其他参数,并且是不可预测的。在确定性计算机上,您可以做的最好的事情是从操作系统的随机源获取盐。这个随机源将从用户操作,启动时间等事件中收集真正的机会......

PHP已经有一个专用函数password_hash()来生成一个带有这种随机盐的安全BCrypt哈希。它将salt包含在生成的哈希值中,因此您可以将其存储到数据库中的单个字段中。

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);

// 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);

使用胡椒也是一个好主意,但有更好的方法来添加这样的服务器端密码。无论你的辣椒是否含有特殊字符或者只是更长时间都无关紧要,熵就很重要。如果您有兴趣,可以查看我关于secure password storing的教程,在最后几页我试图解释胡椒的利弊。