加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用crypt(),我的应用程序如何验证随机生成盐的密码?

发布时间:2020-12-13 21:40:09 所属栏目:PHP教程 来源:网络整理
导读:我一直在关注Stackoverflow上的 PHP’s crypt function和一些 questions,我正在试图找出盐渍和哈希密码. 我在PHP社区页面上找到了这个: ?phpfunction md5crypt($password){ // create a salt that ensures crypt creates an md5 hash $base64_alphabet='ABC
我一直在关注Stackoverflow上的 PHP’s crypt function和一些 questions,我正在试图找出盐渍和哈希密码.

我在PHP社区页面上找到了这个:

<?php
function md5crypt($password){
    // create a salt that ensures crypt creates an md5 hash
    $base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                    .'abcdefghijklmnopqrstuvwxyz0123456789+/';
    $salt='$1$';
    for($i=0; $i<9; $i++){
        $salt.=$base64_alphabet[rand(0,63)];
    }
    // return the crypt md5 password
    return crypt($password,$salt.'$');
}
?>

这样的事情与以下相比如何:

<?php
// Slightly modified example from PHP community page

$password = trim(mysql_prep($_POST['password']));

// Get the hash,letting the salt be automatically generated
$hashed_password = crypt($password);
?>

这是另一个question的摘录:

However,the PHP crypt() function can
use a variety of different hashes to
compute the hash. When you prefix your
salt with “$1$” you get a hash with an
MD5. When you prefix with $2$you get
a crypt with blowfish,which is more
secure.

The “$1$” is prefixed to the output so
that the hash can be verified. If it
wasn’t included,there would be no way
to know from the stored hash which
algorithm should be used! This
information would have to be stored
elsewhere. To save you this trouble
PHP includes the algorithm in the hash
output.

我的头脑有点关于哈希,加密和盐…但真正让我困惑的部分是如何比较用户输入的密码与盐渍哈希密码,如果盐是在用户创建时随机生成的.. .并且没有存储 – 并且最重要的是,如果您必须指定正确的前缀以便能够在用户返回时再次验证密码,那么将crypt()与自动盐一起使用的重点是什么?

解决方法

您需要与用户一起存储salt.

salt的目的是确保具有相同密码的两个用户获得不同的哈希值.
这可以防止彩虹表攻击.

编辑:您的盐应包含(加密安全)随机字节.现在,您将其限制为仅包含字母和数字

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读