我建议使用PHPass库来保护您的密码,因为md5和shai以及其他加密算法并不安全。
PHPass需要
- 加密的照顾。
- 腌制。
- 拉伸。
而且该方法尚未破解(暴力强制不能被视为PHPass的破解)。
你可以从这里得到它:http://www.openwall.com/phpass/
这很容易凑一旦你设置代码:
$hash = PasswordHash($iteration_count_log2, $portable_hashes)
然后你就可以从数据库读取存储的哈希,和通过功能相匹配的登录密码哈希与现有的哈希
$auth = CheckPassword($incomingPasswordNotHashed, $hashFromDatabase);
我喜欢更简单的做这样的事情做出一个哈希:
// Usage: $hash = PHPhass($password);
// Pre: $password is of type string,
// indicating the password which
// the user want's to hash.
// Post: $hash is the hashed password.
function PHPhass($password)
{
$unHashed = $password;
require '../phpass/PasswordHash.php';
header('Content-type: text/plain');
$t_hasher = new PasswordHash(12, FALSE); // Define the iterations once.
$hash = $t_hasher->HashPassword($unHashed);
unset($t_hasher);
return $hash;
}
而且这样做是为了匹配散列和非散列密码:
// Usage: $check = PHPhassMatch($password, $hash)
// Pre: $password is of type string,
// indicating a user's passwordd
// $hash is a hashed password.
// Post: $check is true if $password's hash
// value is equal to $hash.
function PHPhassMatch($password, $hash)
{
$unHashed = $password;
$theHashed = $hash;
require '../phpass/PasswordHash.php';
header('Content-type: text/plain');
$t_hasher = new PasswordHash(8, FALSE);
$check = $t_hasher->CheckPassword($unHashed, $theHashed);
unset($t_hasher);
return $check;
}
此外,像新鲜提到:
- 不要使用mysql_ *,因为它的弃用,我建议使用PDO或mysqli。
- 使用预准备语句来防止sql注入。
- 不要以为张贴变量张贴,检查它们是否与isset()函数
你提到shai1存在,你肯定是在数据库中的密码不SHA1,但你对匹配MD5?
请请请不要使用MD5使用https://github.com/ircmaxell/password_compat – iConnor
您可能需要阅读:http://stackoverflow.com/问题/ 401656/secure-hash-and-salt-for-php-passwords – Baba
所以你'sha1()'在注册时输入密码,而在登录时输入'md5()'密码? – Wiseguy