0
我正在学习php安全在线(使用php 5.4),并遇到以下代码,我想了解/使用。下面的代码是否使用bcrypt并且它是blowfish的良好实现? 如果存在问题,请您提供修复或资源建议。谢谢。这段代码是否使用Bcrypt或者只是普通的河豚?
class PassHash {
// blowfish
private static $algo = '$2a';
// cost parameter
private static $cost = '$10';
// mainly for internal use
public static function unique_salt() {
return substr(sha1(mt_rand()),0,22);
}
// this will be used to generate a hash
public static function hash($password) {
return crypt($password,
self::$algo .
self::$cost .
'$' . self::unique_salt());
}
// this will be used to compare a password against a hash
public static function check_password($hash, $password) {
$full_salt = substr($hash, 0, 29);
$new_hash = crypt($password, $full_salt);
return ($hash == $new_hash);
}
}
下面是用户注册时的用法:
// include the class
require ("PassHash.php");
// ...
// read all form input from $_POST
// ...
// do your regular form validation stuff
// ...
// hash the password
$pass_hash = PassHash::hash($_POST['password']);
// store all user info in the DB, excluding $_POST['password']
// store $pass_hash instead
// ...
这里是在用户登录过程中使用:
// include the class
require ("PassHash.php");
// read all form input from $_POST
// ...
// fetch the user record based on $_POST['username'] or similar
// ...
// ...
// check the password the user tried to login with
if (PassHash::check_password($user['pass_hash'], $_POST['password']) {
// grant access
// ...
} else {
// deny access
// ...
}
另外:我是否需要将盐存储在mysql中,还是将它作为passhash类的内部函数? –
您不需要存储salt,它已经在PassHash :: hash的返回值中。是的,这是一种类似于bcrypt的技术。关于只用核心PHP就可以得到的一样好。 – Artefact2
所以独特的盐包含在散列中?是不是像向黑客移交密钥或盐部分是否加密一样......盐和哈希应该不分开吗? –