2012-10-17 41 views
2

我使用下面的代码到一个密码存储到MySQLSHA1在PHP未在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'; 
} 

有谁知道为什么发生这种情况?

回答

0

将简单的拼写错误(,)而不是点(.)插入数据库时​​会出现错字。

// 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控制什么样的数据的函数将返回,明文(如果为假,默认)与原始数据(如果为true)。

+0

我不明白这会适用于哪里。你可以引用代码吗? – Geoff

+0

@Geoff查看我的帖子编辑。 –

+0

对不起,我明白你的意思。你是对的。我必须失明。我复制了代码,并误以为。为一个 , – Geoff

3

不知道这是你唯一的问题,但

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

不是你如何使用sha1功能。 http://php.net/manual/en/function.sha1.php

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

你可能想要做什么是

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

的时间()似乎在确定走了。 – Geoff

+0

@Geoff - 任意整数= 0在PHP中的计算结果为TRUE;你的时间被评估为TRUE;为'sha1'函数的第二个参数。让它返回二进制而不是你期望的字符串,并导致密码问题。你可能想要做的是'sha1($ password。$ salt)' – Erik

0

SHA1默认返回的二进制散列,这你在一个字符字段存储 - 焦炭领域都受到字符集转换,这意味着MySQL是重整的哈希值。将字段转换为二进制/ varbinary,而不受字符集转换的影响

+0

我已经尝试将它存储在BLOB字段中,结果相似。我将如何将其转换为二进制。我曾尝试以下:(!IS_STRING($ H))“函数HEX2BIN($ H) { 如果返回NULL; $ r =''; 为($一个= 0; $一个 Geoff

+0

我的不好,它实际上并不是默认的,但你传递了2个参数,而第二个实际上是二进制标志。 $ salt字段被视为二元真,强制二元返回。 –

+0

感谢您的协助。 – Geoff

相关问题