2012-05-24 47 views
1

截至目前我有这行代码PHP中的密码哈希

<?php 
    $pass = "12312312"; 
    echo md5(crypt($pass)) 
?> 

我知道隐窝可以随机生成盐,但我没有任何想法,我怎么可以比较它的用户输入。

我在登录表时需要什么字段? 还有,我会在我的表格中插入什么数据?

谢谢!

+1

MD5坏了,所以你应该使用别的东西。尝试bcrypt。 – Oleksi

+0

谢谢!!但我需要什么领域? 嗯我可以在1字段中同时插入密码和盐吗?或者我需要有一个盐场? – user1033600

+0

您需要将盐存储在自己的字段中。 – Oleksi

回答

5

你可以存储在表中的密码如下

$pass = "12312312"; 

// store this in table with separate column 
$salt = md5("any variable"); // may be email or username 

// generate password 
$password = sha1($salt.$pass); 

// now store salt and password in table 

而且你可以检查密码一样

$pass = "User input"; 

// get the user from database based on user id or username 
// and test the password stored in db with 

if($passwordFromTable == sha1($saltFromTable.$pass)) 
{ 
    // correct 
} 
+0

+1为每个用户不同的盐。 – Nightwolf