Advertisement
Guest User

Yii Phpass

a guest
Feb 10th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. class Phpass extends CApplicationComponent
  2. {
  3.     // Path to the phpass library
  4.     public $libPath = 'application.vendors.phpass.PasswordHash';
  5.  
  6.     // Do we require the hashes to be portable to older systems? This is less secure. (This requires PHP 5.3 or Suhosion.)
  7.     public $hashPortable = false;
  8.  
  9.     // Base-2 logarithm of the iteration count used for password stretching
  10.     public $hashCostLog2 = 10;
  11.  
  12.     public function init()
  13.     {
  14.         $lib = Yii::getPathOfAlias($this->libPath) . '.php';
  15.         if (!file_exists($lib)) {
  16.             Yii::log("phpass lib not found ($lib)!", CLogger::LEVEL_WARNING, 'Phpass');
  17.             throw new CHttpException(500, "phpass lib not found ($lib)!");
  18.         }
  19.  
  20.         Yii::import($this->libPath, true);
  21.  
  22.         return parent::init();
  23.     }
  24.  
  25.     // Reset PasswordHash to generate a new random state
  26.     private function _getHasher()
  27.     {
  28.         return new PasswordHash($this->hashCostLog2, $this->hashPortable);
  29.     }
  30.  
  31.     public function getHash($password)
  32.     {
  33.         $hash = $this->_getHasher()->HashPassword($password);
  34.  
  35.         if (strlen($hash) < 20)
  36.             throw new CHttpException(500, 'Problem hashing password, please contact support.');
  37.  
  38.         return $hash;
  39.     }
  40.  
  41.     public function compare($password, $hash)
  42.     {
  43.         return $this->_getHasher()->CheckPassword($password, $hash);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement