Want more features on Pastebin? Sign Up, it's FREE!
Guest

Yii Phpass

By: a guest on Feb 10th, 2014  |  syntax: PHP  |  size: 1.38 KB  |  views: 57  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }
clone this paste RAW Paste Data