微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

参见英文答案 > 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 */

原文地址:https://www.jb51.cc/php/138243.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐