Advertisement
Guest User

PHP Password Class

a guest
Oct 9th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. <?php
  2.     /**
  3.      * An instance of a Password storage object
  4.      */
  5.     class Password {
  6.         private $salt;
  7.         private $password;
  8.         /**
  9.          * Construct the Password object
  10.          * @param String $password the password to pass the object
  11.          * @param number $length The length of the salt (Optional)
  12.          * @param string $salt The Salt to use
  13.          */
  14.         function __construct($password, $randomsaltlength=10, $salt = null){
  15.             $this->salt = $salt ?: "";
  16.             if($this->salt == "") $this->setRandomSalt(($randomsaltlength > 4) ? $randomsaltlength : 5);
  17.             $this->password = Password::getHash($password, $this->salt);
  18.         }
  19.    
  20.         /**
  21.          * Compares another password to the password in the object
  22.          * @param string $password The password to compare
  23.          * @return boolean true if passwords match, false if they don't
  24.          */
  25.         public function compareTo($password){
  26.             return (Password::getHash($password, $this->salt) == $this->password);
  27.         }
  28.    
  29.         /**
  30.          * Gets the encrypted version of the password, using sha256 and the salt
  31.          * @return string The encrypted password
  32.          */
  33.         public function getEncryptedPassword(){
  34.             return $this->password;
  35.         }
  36.    
  37.         /**
  38.          * Gets the salt used to encrypt the password
  39.          * @return string The salt used to encrypt the password
  40.          */
  41.         public function getSalt(){
  42.             return $this->salt;
  43.         }
  44.    
  45.         /**
  46.          * Sets a random initial value for $salt
  47.          * @param Integer $length the length of the initial string
  48.          */
  49.         private function setRandomSalt($length){
  50.             while($length--) $this->salt .= chr(mt_rand(97, 122)) ;
  51.             $this->salt = hash('sha256', $this->salt);
  52.         }
  53.        
  54.         /**
  55.          * Returns a password has based on the data passed
  56.          * @param String $password The password to use
  57.          * @param String $salt The salt to use
  58.          * @return String The password hash
  59.          */
  60.         public static function getHash($password, $salt){
  61.             return pbkdf2("sha256", $password, $salt, 1000, 50);
  62.         }
  63.        
  64.         /**
  65.          * Private method for generating pbkdf2 hash
  66.          */
  67.         static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
  68.         {
  69.             $algorithm = strtolower($algorithm);
  70.             if(!in_array($algorithm, hash_algos(), true))
  71.                 trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR);
  72.             if($count <= 0 || $key_length <= 0)
  73.                 trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR);
  74.        
  75.             if (function_exists("hash_pbkdf2")) {
  76.                 // The output length is in NIBBLES (4-bits) if $raw_output is false!
  77.                 if (!$raw_output) {
  78.                     $key_length = $key_length * 2;
  79.                 }
  80.                 return hash_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output);
  81.             }
  82.        
  83.             $hash_length = strlen(hash($algorithm, "", true));
  84.             $block_count = ceil($key_length / $hash_length);
  85.        
  86.             $output = "";
  87.             for($i = 1; $i <= $block_count; $i++) {
  88.                 // $i encoded as 4 bytes, big endian.
  89.                 $last = $salt . pack("N", $i);
  90.                 // first iteration
  91.                 $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
  92.                 // perform the other $count - 1 iterations
  93.                 for ($j = 1; $j < $count; $j++) {
  94.                     $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
  95.                 }
  96.                 $output .= $xorsum;
  97.             }
  98.        
  99.             if($raw_output)
  100.                 return substr($output, 0, $key_length);
  101.             else
  102.                 return bin2hex(substr($output, 0, $key_length));
  103.         }
  104.     }
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement