首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在登录时从数据库中提取河豚加密w/ salt

在登录时从数据库中提取河豚加密w/ salt
EN

Stack Overflow用户
提问于 2018-03-21 10:57:35
回答 1查看 72关注 0票数 0

我显然是刚开始使用河豚加密技术来问这个问题。我相信有一个方程的一边,但不知道如何登录一旦哈希是在DB。我有以下在注册时加密密码的方法:

代码语言:javascript
复制
    $blowfish_hash = "$2y$10$";
    $salt_length = 22;

    $salt = Generate_Salt($salt_length);
    $hash_combined = $blowfish_hash . $salt;

    $hash = crypt($password, $hash_combined);

    $password = $hash;

Generate_Salt()函数如下:

代码语言:javascript
复制
function Generate_Salt($length) {

  $unique_rndm_str = md5(uniqid(mt_rand(), true));
  $base64_string = base64_encode($unique_rndm_str);

  $mod_Base64_str = str_replace('+', '.', $base64_string); 
  $salt = substr($mod_Base64_str, 0, $length);

    return $salt;
}

一旦我注册,我就会得到这个很好的长哈希-太棒了!但是,当我登录时,我不确定如何调用哈希来检查给定的密码:$_POST['log_password'];

使用md5是很容易的,我只是用这种方式加密了$password = md5($password);,并以这种方式回忆起了$password = md5($_POST['log_password']);,但是我发现这不是一种安全的方法。

我已经干了好几个小时了,有人能帮我解释一下吗?任何帮助都将不胜感激。

ep

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-21 12:14:04

这比你想象的要容易得多。只需使用函数散列(),它将执行对crypt()函数的调用并处理安全盐的生成。

代码语言:javascript
复制
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($_POST['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($_POST['password'], $existingHashFromDb);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49404502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档