douweng1935 2014-07-19 07:42
浏览 82

PHP网站为桌面和移动浏览器返回不同的SHA512值

I am working on a website which requires a membership section. So, when the user registers a new members account, I am saving the username, a random salt, and a SHA512 hashed password using the random salt and the user's password in order to not save the password in plain text in my database.

The problem I seem to have when a user logs in is that the SHA512 javascript function is returning a different value when I'm using the website from my desktop vs. from a mobile browser (both Android and IOS devices). The desktop appears to be the correct one.

Here is the PHP function I'm using to check when the user logs in:

// login function, check email and password against the database.
function login($email, $password, $mysqli) {
// Using prepared statements means that SQL injection is not possible. 
if ($stmt = $mysqli->prepare("SELECT id, username, password, salt 
    FROM members
   WHERE email = ?
    LIMIT 1")) {
    $stmt->bind_param('s', $email);  // Bind "$email" to parameter.
    $stmt->execute();    // Execute the prepared query.
    $stmt->store_result();

    // get variables from result.
    $stmt->bind_result($user_id, $username, $db_password, $salt);
    $stmt->fetch();

    // 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
            $_SESSION['temp'] = "account locked.";
            return false;
        } else {
            // Check if the password in the database matches
            // the password the user submitted.
            if ($db_password == $password) {
                // Password is correct!
                // Get the user-agent string of the user.
                $user_browser = $_SERVER['HTTP_USER_AGENT'];
                // XSS protection as we might print this value
                $user_id = preg_replace("/[^0-9]+/", "", $user_id);
                $_SESSION['user_id'] = $user_id;

                $_SESSION['temp'] = $user_browser;

                // XSS protection as we might print this value
                $username = preg_replace("/[^a-zA-Z0-9_\-]+/", 
                                                            "", 
                                                            $username);
                $_SESSION['username'] = $username;
                $_SESSION['login_string'] = hash('sha512', 
                          $password . $user_browser);
                // Login successful.
                return true;
            } else {
                // Password is not correct
                // We record this attempt in the database
                $now = time();
                $mysqli->query("INSERT INTO login_attempts(user_id, time)
                                VALUES ('$user_id', '$now')");
                $_SESSION['temp'] = "Invalid password. ";// . $username . " " . $password . " " . $db_password;                
                return false;
            }
        }
    } else {
        $_SESSION['temp'] = "User not Found.";
        // No user exists.
        return false;
    }
}
}

Here are the SHA values I got from the same username/password: Desktop (which let me log in correctly): 091b2e6cda1e1ee4de89b1164703a238021fa6c092551cfea2c967051bd01f3992dbe160bbc25a14694f119a819b88534b21d0f7c8da9c80bb8c06a79f75074c

From my Android phone (Invalid password returned):20036264d5fccd56658e45364112ae0e3356878d84150aa817e049a288aa491b88fab9384df71a3aae7e3d0145acd3dc5608030961181b1bf8336e6019824daf

The javascript SHA512 function I'm using can be found here: http://pajhome.org.uk/crypt/md5

  • 写回答

1条回答 默认 最新

  • doufen3134 2014-07-19 08:09
    关注

    I believe this anomaly is because of the representation of characters. In different OSs, browsers and devices the representation may be different. Try converting the password to a common format such as UNICODE , before calculating the digests.

    In php you can use utf8_encode() function.

    in javascript

    function encode_utf8( s ) {
      return unescape( encodeURIComponent( s ) );
    }
    
    function decode_utf8( s ) {
      return decodeURIComponent( escape( s ) );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛