title: How to change a SALT password in a database using PHP?
body: <p>I am using a HTTP POST from android to some php script in order to update a users password in the database.</p>
  
  <p>I am using the same SALT hash as is done when the user creates the account and the database update is running and changing the values of SALT however when I try to log in with the new password it is coming as incorrect.</p>
  
  <p>The initial code for creating the password is:</p>
  
  <pre><code>public function storeUser($name, $email, $password, $rand) {
      $uuid = uniqid('', true);
      $hash = $this-&gt;hashSSHA($password);
      $encrypted_password = $hash["encrypted"]; // encrypted password
      $salt = $hash["salt"]; // salt
      $auth = 0;
      $result = mysql_query("INSERT INTO users(unique_id, authorized, auth_code, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$auth', '$rand', '$name', '$email', '$encrypted_password', '$salt', NOW())");
      // check for successful store
      if ($result) {
          // get user details 
          $uid = mysql_insert_id(); // last inserted id
          $result = mysql_query("SELECT * FROM users WHERE uid = $uid");
          // return user details
          return mysql_fetch_array($result);
      } else {
          return false;
      }
  }
  </code></pre>
  
  <p>The hashing functions are:</p>
  
  <pre><code> /**
   * Encrypting password
   * @param password
   * returns salt and encrypted password
   */
  public function hashSSHA($password) {
  
      $salt = sha1(rand());
      $salt = substr($salt, 0, 10);
      $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
      $hash = array("salt" =&gt; $salt, "encrypted" =&gt; $encrypted);
      return $hash;
  }
  
  /**
   * Decrypting password
   * @param salt, password
   * returns hash string
   */
  public function checkhashSSHA($salt, $password) {
  
      $hash = base64_encode(sha1($password . $salt, true) . $salt);
  
      return $hash;
  }
  </code></pre>
  
  <p>And finally the update function (where the problem is):</p>
  
  <pre><code> /**
   * Updating a users
   * password
   */
  public function updatePassword($email, $password) {
      $uuid = uniqid('', true);
      $hash = $this-&gt;hashSSHA($password);
      $encrypted_password = $hash["encrypted"]; // encrypted password
      $salt = $hash["salt"]; // salt
  
      $result = mysql_query("UPDATE users SET encrypted_password='$encrypted_password',  updated_at = NOW() WHERE email='$email'");
      $result = mysql_query("UPDATE users SET salt='$salt',  updated_at = NOW() WHERE email='$email'");
  
  
      // check for successful store
      if ($result) {
          // get user details
          $uid = mysql_insert_id(); // last inserted id
          $result = mysql_query("SELECT * FROM users WHERE email = '$email'");
          // return user details
          return mysql_fetch_array($result);
      } else {
          return false;
      }
  }
  </code></pre>
  
  <p>As always any help greatly appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>Login function as requested:</p>
  
  <pre><code> /**
   * Get user by email and password
   */
  public function getUserByEmailAndPassword($email, $password) {
      $result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
      // check for result
      $no_of_rows = mysql_num_rows($result);
      if ($no_of_rows &gt; 0) {
          $result = mysql_fetch_array($result);
          $salt = $result['salt'];
          $encrypted_password = $result['encrypted_password'];
          $hash = $this-&gt;checkhashSSHA($salt, $password);
          // check for password equality
          if ($encrypted_password == $hash) {
              // user authentication details are correct
              return $result;
          }
      } else {
          // user not found
          return false;
      }
  }
  </code></pre>