【问题标题】:sha1 in php not storing properly in mysqlphp中的sha1没有正确存储在mysql中
【发布时间】:2012-10-17 19:30:34
【问题描述】:

我使用以下代码将密码存储到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';
}

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

【问题讨论】:

    标签: php mysql sha1


    【解决方案1】:

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

     $pwd = sha1($password, $salt);
    

    不是您使用sha1 函数的方式。 http://php.net/manual/en/function.sha1.php

    time() 将始终评估为TRUE,因此您将原始二进制格式插入到您的 char 密码字段中。导致您看到的问题。

    你可能想做的是

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

    【讨论】:

    • time() 似乎正常。
    • @Geoff - php 中的任何整数 != 0 计算为 TRUE 您的时间被计算为 TRUE 用于 sha1 函数的第二个参数。使其返回二进制而不是您期望的字符串并导致密码问题。你可能想做的是sha1($password . $salt)
    【解决方案2】:

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

    // 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)。

    【讨论】:

    • 我看不出这适用于什么地方。能否请您引用代码。
    • 对不起,我明白你的意思了。你说的太对了。我一定是瞎了。我复制了代码并弄错了 .对于一个,
    【解决方案3】:

    sha1 默认返回二进制哈希,您将其存储在 char 字段中 - char 字段受字符集转换的影响,这意味着 mysql 正在修改哈希。将字段转换为二进制/varbinary,而不是进行字符集转换

    【讨论】:

    • 我尝试将其存储在 BLOB 字段中,但结果也很差。我将如何将其转换为二进制。我尝试了以下方法:'function hex2bin($h) { if (!is_string($h)) return null; $r=''; for ($a=0; $a
    • 我的错,它实际上不是默认值,但你传入了 2 个参数,第二个实际上是二进制标志。 $salt 字段被视为二进制 true,强制二进制返回。
    • 感谢大家的帮助。
    猜你喜欢
    • 2010-10-11
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2019-06-27
    • 2012-04-24
    • 1970-01-01
    相关资源
    最近更新 更多