Bri*_*ian 17 php security encryption hash salt
我一直在使用PHP crypt()作为在我的数据库中存储和验证密码的方法.我对其他东西使用散列,但是crypt()对于密码.文档不是很好,似乎有很多争论.我正在使用河豚和两种盐来加密密码并将其存储在数据库中.在我存储salt和加密密码之前(如盐腌哈希)但实现了它的冗余,因为salt是加密密码字符串的一部分.
我对彩虹表攻击如何工作有点困惑crypt(),无论如何从安全的角度来看这看起来是正确的.我使用第二个盐来附加密码以增加短密码的熵,可能是矫枉过正,但为什么不呢?
function crypt_password($password) {
if ($password) {
//find the longest valid salt allowed by server
$max_salt = CRYPT_SALT_LENGTH;
//blowfish hashing with a salt as follows: "$2a$", a two digit cost parameter, "$", and 22 base 64
$blowfish = '$2a$10$';
//get the longest salt, could set to 22 crypt ignores extra data
$salt = get_salt ( $max_salt );
//get a second salt to strengthen password
$salt2 = get_salt ( 30 ); //set to whatever
//append salt2 data to the password, and crypt using salt, results in a 60 char output
$crypt_pass = crypt ( $password . $salt2, $blowfish . $salt );
//insert crypt pass along with salt2 into database.
$sql = "insert into database....";
return true;
}
}
function get_salt($length) {
$options = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
$salt = '';
for($i = 0; $i <= $length; $i ++) {
$options = str_shuffle ( $options );
$salt .= $options [rand ( 0, 63 )];
}
return $salt;
}
function verify_password($input_password)
{
if($input_password)
{
//get stored crypt pass,and salt2 from the database
$stored_password = 'somethingfromdatabase';
$stored_salt2 = 'somethingelsefromdatabase';
//compare the crypt of input+stored_salt2 to the stored crypt password
if (crypt($input_password . $stored_salt2, $stored_password) == $stored_password) {
//authenticated
return true;
}
else return false;
}
else return false;
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*Ros 15
你真的应该看一下PHPASS:http://www.openwall.com/phpass/它是一个使用crypt()的密码哈希框架,用于Wordpress和phpBB等项目.
本网站上还有一篇关于使用crypt()进行密码散列,盐析和拉伸的优秀文章:http://www.openwall.com/articles/PHP-Users-Passwords
更新: 目前还有PHPASS库的替代品.在下一版本的PHP中,有一些用于散列和验证密码的特殊函数(使用bcrypt):http://www.php.net/manual/en/ref.password.php .有一个兼容性库,为PHP 5.3.7+实现这些功能:https://github.com/ircmaxell/password_compat
caf*_*caf 11
你的使用crypt()很好. crypt($input, $stored) == $stored是它的设计使用方式.
你的get_salt()功能不是很好,因为它使用的rand()功能往往很差.您应该考虑使用更强大的随机函数,例如openssl_random_pseudo_bytes().
| 归档时间: |
|
| 查看次数: |
17174 次 |
| 最近记录: |