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

php – 如何为一个密码实现sha 512,md5和salt加密

发布时间:2020-12-13 13:40:14 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 Secure hash and salt for PHP passwords14个 $pass="test" 上面的变量包含一个名为test的密码.我想使用sha512 md5和salt来哈希这个密码我是怎么做到的,因为我发现只有盐和sha512的好处,我已经知道md5 encryption.please我需要解决方案,因为我
参见英文答案 > Secure hash and salt for PHP passwords14个
$pass="test"

上面的变量包含一个名为test的密码.我想使用sha512 md5和salt来哈希这个密码我是怎么做到的,因为我发现只有盐和sha512的好处,我已经知道md5 encryption.please我需要解决方案,因为我的系统是vunerable

请用代码示例解释它,因为我仍然附加到md5

从我的意见和你的评论我已经得到以下代码

$pass="test";
$hashed_pass= openssl_digest($pass,'sha512');

好吧看起来足够坚固但是[salt =”]是什么?
它会生成一个随机的盐串或其他东西,如果是这样的话,如何实现它?

编辑:由于这个答案似乎仍然引起了一些兴趣,让我引导你们走向 password_hash(),这本质上是一个关于crypt()的包装,但更简单易用.如果您使用的是PHP< 5.5,那么 password_compat是由同一个人编写的,实际上是与官方文档相关联的.

如果您已经在使用crypt(),那么值得注意的是password_verify()password_needs_rehash()都可以使用所有crypt()风格的密码,因此几乎没有理由不更新!

使用crypt(),它提供了更强大的散列方法.

哈希新密码:

// generate a 16-character salt string
$salt = substr(str_replace('+','.',base64_encode(md5(mt_rand(),true))),16);
// how many times the string will be hashed
$rounds = 10000;
// pass in the password,the number of rounds,and the salt
// $5$specifies SHA256-CRYPT,use $6$if you really want SHA512
echo crypt('password123',sprintf('$5$rounds=%d$%s$',$rounds,$salt));
// output: $5$rounds=10000$3ES3C7XZpT7WQIuC$BEKSvZv./Y3b4ZyWLqq4BfIJzVHQweHqGBukFmo5MI8

比较现有密码:

// the hash stored for the user
$given_hash = '$5$rounds=10000$3ES3C7XZpT7WQIuC$BEKSvZv./Y3b4ZyWLqq4BfIJzVHQweHqGBukFmo5MI8';
$test_pw = 'password123';

// extract the hashing method,number of rounds,and salt from the stored hash
// and hash the password string accordingly
$parts = explode('$',$given_hash);
$test_hash = crypt($test_pw,sprintf('$%s$%s$%s$',$parts[1],$parts[2],$parts[3]));

// compare
echo $given_hash . "n" . $test_hash . "n" . var_export($given_hash === $test_hash,true);
/* output:
$5$rounds=10000$3ES3C7XZpT7WQIuC$BEKSvZv./Y3b4ZyWLqq4BfIJzVHQweHqGBukFmo5MI8
$5$rounds=10000$3ES3C7XZpT7WQIuC$BEKSvZv./Y3b4ZyWLqq4BfIJzVHQweHqGBukFmo5MI8
true */

(编辑:李大同)

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

    推荐文章
      热点阅读