title: Yii user login system with password rehashing
body: <p>I am using Yii framework and I want to create a user login system with rehashing passwords. so when user loges in system geerates new salt and rehashes the password with new salt. I am getting no errors but when I am checking password and salt they don't change in database. so here is what I have done for now:</p>
  
  <pre><code>&lt;?php
  
  /**
   * UserIdentity represents the data needed to identity a user.
   * It contains the authentication method that checks if the provided
   * data can identity the user.
   */
  class UserIdentity extends CUserIdentity
  {
      private $_id;
  
      public function authenticate()
      {
          $record=User::model()-&gt;findByAttributes(array('username'=&gt;$this-&gt;username));
          if($record===null)
              $this-&gt;errorCode=self::ERROR_USERNAME_INVALID;
          else if($record-&gt;password !== hash('sha512', $this-&gt;password.Security::Decrypt($record-&gt;salt)))
              $this-&gt;errorCode=self::ERROR_PASSWORD_INVALID;
          else
          {
              while ($record2 !== null){
                  $salt = Security::GenerateSalt(128);
                  if ($salt === null)
                  {
                      die('can\'t generate salt');
                  }
                  $record2 = User::model()-&gt;findByAttributes(array('salt'=&gt;Security::Encrypt($salt)));
              }
              $record-&gt;salt = Security::Encrypt($salt);
              $record-&gt;password = hash('sha512', $this-&gt;password.$salt);
              $record-&gt;save();
              $this-&gt;_id=$record-&gt;id;
              $this-&gt;setState('user_id', $record-&gt;id);
              $this-&gt;setState('user_username', $record-&gt;username);
              $this-&gt;setState('user_privilages', $record-&gt;privilages);
              $this-&gt;errorCode=self::ERROR_NONE;
          }
          return !$this-&gt;errorCode;
      }
  
      public function getId()
      {
          return $this-&gt;_id;
      }
  }
  </code></pre>