Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 哈希/盐析时密码不匹配_Php_Mysql_Hash_Passwords_Salt - Fatal编程技术网

Php 哈希/盐析时密码不匹配

Php 哈希/盐析时密码不匹配,php,mysql,hash,passwords,salt,Php,Mysql,Hash,Passwords,Salt,好的,下面所有的代码都不是我自己的。我一直在网上学习一个教程,但当我尝试运行它时,似乎没有匹配的密码。我相信在添加密码时可能会出现错误,因为数据库中的内容与login.php脚本中描述的64个字符相差甚远。我不知道。代码如下: register.php // Create a 256 bit (64 characters) long random salt // Let's add 'something random' and the username // to the salt as wel

好的,下面所有的代码都不是我自己的。我一直在网上学习一个教程,但当我尝试运行它时,似乎没有匹配的密码。我相信在添加密码时可能会出现错误,因为数据库中的内容与login.php脚本中描述的64个字符相差甚远。我不知道。代码如下:

register.php

// Create a 256 bit (64 characters) long random salt
// Let's add 'something random' and the username
// to the salt as well for added security
$salt = hash('sha256', uniqid(mt_rand(), true) . 'something random' . strtolower($username));

// Prefix the password with the salt
$hash = $salt . $password;

// Hash the salted password a bunch of times
for ( $i = 0; $i < 100000; $i ++ )
{
    $hash = hash('sha256', $hash);
}

// Prefix the hash with the salt so we can find it back later
$hash = $salt . $hash;

// carry on with registration code...
//创建一个256位(64个字符)长的随机salt
//让我们添加'something random'和用户名
//为了增加安全性,也要加入盐
$salt=hash('sha256',uniqid(mt_rand(),true)。'something random'。strtolower($username));
//在密码前面加上salt
$hash=$salt$暗语
//多次对加密密码进行哈希运算
对于($i=0;$i<100000;$i++)
{
$hash=hash('sha256',$hash);
}
//在杂烩前面加上盐,这样我们以后就能找到它了
$hash=$salt$搞砸
//继续使用注册码。。。
login.php

$email = $_POST['email'];
$password = $_POST['password'];

$con = mysql_connect("localhost", "redacted", "redacted", "redacted");

$sql = '
    SELECT
        `password`
    FROM `users`
        WHERE `email` = "' . mysql_real_escape_string($email) . '"
    LIMIT 1
    ;';

$r = mysql_fetch_assoc(mysql_query($sql));

// The first 64 characters of the hash is the salt
$salt = substr($r['password'], 0, 64);

$hash = $salt . $password;

// Hash the password as we did before
for ( $i = 0; $i < 100000; $i ++ )
{
    $hash = hash('sha256', $hash);
}

$hash = $salt . $hash;

if ( $hash == $r['password'] )
{
    session_start();
    header('Location: /quiz/index.php');
}

if( $hash != $r['password'] ){
    session_start();
    header('Location: /?error=4');
}

// end login script
$email=$\u POST['email'];
$password=$_POST['password'];
$con=mysql_connect(“localhost”、“redacted”、“redacted”、“redacted”);
$sql='1
选择
`密码`
来自`用户`
其中`email`='.mysql\u real\u escape\u string($email)。”
限制1
;';
$r=mysql\u fetch\u assoc(mysql\u query($sql));
//散列的前64个字符是salt
$salt=substr($r['password'],0,64);
$hash=$salt$暗语
//像以前一样散列密码
对于($i=0;$i<100000;$i++)
{
$hash=hash('sha256',$hash);
}
$hash=$salt$搞砸
如果($hash==$r['password']))
{
会话_start();
标题('Location:/quick/index.php');
}
如果($hash!=$r['password'])){
会话_start();
标题(“位置:/?错误=4”);
}
//结束登录脚本

看起来您添加了两次盐:

$hash = $salt . $password;

// Hash the password as we did before
for ( $i = 0; $i < 100000; $i ++ )
{
    $hash = hash('sha256', $hash);
}

//skip the below one
$hash = $salt . $hash;
$hash=$salt$暗语
//像以前一样散列密码
对于($i=0;$i<100000;$i++)
{
$hash=hash('sha256',$hash);
}
//跳过下面的一个
$hash=$salt$搞砸
更新:

事实上,在这种情况下,需要添加两次盐

尽管如此,salt应该保存在单独的db列中,因此代码将更加简化——避免存储/检索salt的所有字符串连接


除此之外,通过将每条信息存储在单独的插槽中,您的数据库结构将更接近第三标准格式。

常见错误是数据库字段的长度不足以存储所有字符。然后,密码将永远不会等于用户输入的密码


对于此类功能,请始终编写单元测试,检查功能(仍然)是否按预期工作。总有一天会有人修改数据库,更改哈希算法,修改salt。。。没有人可以登录

哪个教程?PHP在网络上有很多糟糕的教程。这似乎是其中之一。一个常见的错误是数据库字段不够长,无法存储所有字符。啊。。。这就是为什么。我的密码字段只有30个字符。干杯@Konerak:-)@Konerak干杯!再次干杯。:-)也跳过整个“多次散列”。只要在可以引入功因子的地方使用,通常建议使用bcrypt。您需要将salt与哈希一起存储,以便知道它后面是什么。结果表明,我的数据库密码字段太短。但是谢谢你的建议。如果你把salt放在另一个db列中,你的代码会大大简化,你的db会被使用。对不起,这是错误的。你需要添加salt两次:第一次,所以它是用密码散列的(salt的实际点)。第二次,为了能够找回盐。更好的选择是将salt存储在单独的db列中,重命名脚本中的变量也不会有任何影响。在散列之前和之后,所有内容都是
$hash
$tohash
$hashed
$salt\u with\u hashed
可能是更清晰的变量名。