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 */

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...