我是一个编程新手,现在正在学习密码存储的加密,在我的实验中我遇到了这个登录问题。我试图使用php crypt()函数登录比较数据库中的哈希密码和用户输入:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="index.php">
user name :<input type="text" name="uid" />
password :<input type="text" name="pas" />
<input type="submit"/>
</form>
<?php
if(isset($_POST['pas'])&&isset($_POST['uid']))
{
$uid=$_POST['uid'];
$pas=$_POST['pas'];
require_once('class.DBConnect.php');
$ob=new DBConnect('test');
$ob->getData("select * from log where uid='$uid'",array('pas'));
foreach($ob->columnData as $value)
{
$hashed_pas=$value;
}
if(crypt($pas,$hashed_pas)==$hashed_pas)
{
echo "loggin in";
}
else
{
echo "fail!!!";
}
}
?>
</body>
</html>密码是'san‘。$hashed_pas的价值是:$1$mG5.1k/.$/。而$pas,$hashed_pas则是:$1$mG5.1k/.$.LHc4JCN6GRznyYWZ/Mi。
我想知道为什么会这样。
我使用自动生成的salt作为存储在数据库中的散列。
class.DBConnect.php:
<?php
class DBConnect
{
public $columnData=array();
private $con,$rs;
public function __construct($database)
{
$this->con=mysqli_connect("localhost","root","root",$database);
if(mysqli_connect_errno())
{
echo "DB error is:".mysqli_connect_error();
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function make_safe($variable)
{
$variable = mysqli_real_escape_string($this->con,trim($variable));
return $variable;
}
function getData($query,$column=array())
{
$this->rs=mysqli_query($this->con,$query);
while($row=mysqli_fetch_array($this->rs))
{
foreach($column as $a)
{
$this->columnData=array($row[$a]);
}
}
}
function checkDataExist()
{
if(mysqli_num_rows($this->rs)==0)
{
return 0;
}
else
{
return 1;
}
}
}
?>请帮助我这个问题,并建议我的行业水平的这些标准。
编辑:我尝试了以下代码并输出如下:
san:$1$pb2.8C3$WhJ/zOEWZUXc/7fTEbcJe。san:$1$pb2.8C3$WhJ/zOEWZUXc/7fTEbcJe。
$pas="san";
$hash= crypt($pas);
echo "$pas : $hash<br />";
$hash= crypt($pas,$hash);
echo "$pas : $hash";所以我想知道为什么数据库比较不起作用。
发布于 2014-03-01 07:51:35
数据库字段的长度似乎不足以容纳整个散列字符串。如果要存储的字符串超过表字段的长度,数据将被截断。
https://stackoverflow.com/questions/22110761
复制相似问题