Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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中的sha1未在mysql中正确存储_Php_Mysql_Sha1 - Fatal编程技术网

php中的sha1未在mysql中正确存储

php中的sha1未在mysql中正确存储,php,mysql,sha1,Php,Mysql,Sha1,我使用以下代码将密码存储到mysql中 if (!$errors) { // include the connection file require_once('connection.inc.php'); $conn = dbConnect('write'); // create a salt using the current timestamp $salt = time(); // encrypt the password and sa

我使用以下代码将密码存储到mysql中

    if (!$errors) {
    // include the connection file
    require_once('connection.inc.php');
    $conn = dbConnect('write');
    // create a salt using the current timestamp
    $salt = time();
    // encrypt the password and salt
    $pwd = sha1($password, $salt);
    echo $pwd;
    // prepare SQL statement
    $sql = 'INSERT INTO users (username, salt, pwd)
            VALUES (?, ?, ?)';
    $stmt = $conn->stmt_init();
    $stmt = $conn->prepare($sql);
    // bind parameters and insert the details into the database
    $stmt->bind_param('sis', $username, $salt, $pwd);
    $stmt->execute();
    if ($stmt->affected_rows == 1) {
        $success = "$username has been registered. You may now log in.";
    } elseif ($stmt->errno == 1062) {
        $errors[] = "$username is already in use. Please choose another username.";
        } else {
            $errors[] = 'Sorry, there was a problem with the database.';
        }

}
密码字段pwd定义为CHAR 40。当我检查它时,我看到它包含以下内容:

ƒ7Ž{9‰ù|EòsŒs”ºþ
不管我输入什么密码。当然,这与我尝试登录时使用以下代码查看的密码不同:

    require_once('connection.inc.php');
$conn = dbConnect('read');
// get the username's details from the database
$sql = 'SELECT salt, pwd FROM users WHERE username = ?';
// initialize and prepare  statement
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
// bind the input parameter
$stmt->bind_param('s', $username);
// bind the result, using a new variable for the password
$stmt->bind_result($salt, $storedPwd);
$stmt->execute();
$stmt->fetch();
// encrypt the submitted password with the salt
// and compare with stored password
if (sha1($password . $salt) == $storedPwd) {
    $_SESSION['authenticated'] = 'Jethro Tull';
    // get the time the session started
    $_SESSION['start'] = time();
    session_regenerate_id();
    header("Location: $redirect");
    exit;
} else {
    // if no match, prepare error message
    echo "  pwd " . $password;
    echo "  salt " . $salt;
    echo "  sha1 " . sha1($password . $salt);
    echo "  St. pwd " . $storedPwd;
    $error = 'Invalid username or password';
}

有人知道为什么会这样吗?

不确定这是否是你唯一的问题,但是

 $pwd = sha1($password, $salt);
不是如何使用
sha1
功能

time()。导致你看到的问题

你可能想做的是

 $pwd = sha1($password . $salt);
                       ^

默认情况下,sha1返回二进制散列,存储在char字段中-char字段需要进行字符集转换,这意味着mysql正在破坏散列。将该字段转换为二进制/varbinary,不受字符集转换的约束

您有一个简单的打字错误,在将数据插入数据库时,您使用逗号(
)而不是点(

// encrypt the password and salt
$pwd = sha1($password, $salt);
将其与验证密码时生成哈希和的位置进行比较:

// encrypt the submitted password with the salt
// and compare with stored password
if (sha1($password . $salt) == $storedPwd) {
因为这两个词的含义截然不同,所以一个小小的错误将在总体上产生巨大的后果

您希望形成一个由
$password
$salt
组成的新字符串,但现在您给出的是
sha1
两个参数,而不是一个


sha1
的第二个参数控制函数将返回哪种类型的数据,纯文本(如果为false,默认值)与原始数据(如果为true)。

我曾尝试将其存储在BLOB字段中,但结果类似。如何将其转换为二进制文件。我尝试了以下方法:'hex2bin($h){if(!is_string($h))返回null;$r='';for($a=0;$amy bad,这实际上不是默认值,但您正在传递2个参数,第二个参数实际上是二进制标志。$salt字段被视为二进制true,强制二进制返回。谢谢大家的帮助。我不知道这将适用于何处。请您引用代码。对不起,我明白您的意思。您是如此正确。我必须e变瞎了。我复制了代码,错把a.当成了a,时间()似乎一切正常。@Geoff-php中的任意整数!=0计算为
TRUE
您的时间在
sha1
函数的第二个参数中被计算为
TRUE
。使其返回二进制而不是您期望的字符串,并导致密码问题。您可能想要做的是
sha1($password.$salt)