dtkf64283 2015-08-31 14:26
浏览 60
已采纳

登录表单/密码重置

I am using this tutorial http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL to secure access to my web page and I would like to modify it by reset password feature. Unfortunately my reset password addon store something which makes account not able to log in.

This code is performing user registration and works like a charm

  $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));

    // Create salted password 
    $password = hash('sha512', $password . $random_salt);

    // Insert the new user into the database 
    if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) {
        $insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt);
        // Execute the prepared query.
        if (! $insert_stmt->execute()) {
            header('Location: ../error.php?err=Registration failure: INSERT');
        }
    }
    header('Location: ./register_success.php');

Login processing:

    // hash the password with the unique salt.
    $password = hash('sha512', $password . $salt);

    if ($stmt->num_rows == 1) {
        // If the user exists we check if the account is locked
        // from too many login attempts 

        if (checkbrute($user_id, $mysqli) == true) {
            // Account is locked 
            // Send an email to user saying their account is locked
            return false;
        } else {
            // Check if the password in the database matches
            // the password the user submitted.
            if ($db_password == $password) {

also works fine. (not my job :)) and now my part - random string generator followed by hashing

$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 8; $i++) {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
}

$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));

    // Create salted password 
$password = hash('sha512', $randomString . $random_salt);

          if ($insert_stmt = $mysqli->prepare("UPDATE members SET password = ?, salt=? WHERE username= ?")){
        $insert_stmt->bind_param('sss', $password, $random_salt, $username);



$insert_stmt->execute();
                                      $insert_stmt->close();
    }

reset script successfully modify db for the user, store correct salt and correct hashed password. Tested by showing random text and salt on the screen and than combined here http://www.convertstring.com/cs/Hash/SHA512

Any advice how to track hash process or tips how to fix it would be appreciated.

I have a suspicion that there may be some trouble with JS which hash the password on client side

function formhash(form, password) {
    // Create a new element input, this will be our hashed password field. 
    var p = document.createElement("input");

    // Add the new element to our form. 
    form.appendChild(p);
    p.name = "p";
    p.type = "hidden";
    p.value = hex_sha512(password.value);

    // Make sure the plaintext password doesn't get sent. 
    password.value = "";

    // Finally submit the form. 
    form.submit();
}

Thank you very much

  • 写回答

1条回答 默认 最新

  • douyiji3919 2015-08-31 15:13
    关注

    Reverse engineering from your formhash() Login processing would suggests that you should not hash the $randomString, but rather the sha512 version of it. So in your password reset addon instead of this line:

    $password = hash('sha512', $randomString . $random_salt);
    

    Use these two:

    $password = hash('sha512', $randomString);
    $password = hash('sha512', $password . $random_salt);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化