Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * An instance of a Password storage object
- */
- class Password {
- private $salt;
- private $password;
- /**
- * Construct the Password object
- * @param String $password the password to pass the object
- * @param number $length The length of the salt (Optional)
- * @param string $salt The Salt to use
- */
- function __construct($password, $randomsaltlength=10, $salt = null){
- $this->salt = $salt ?: "";
- if($this->salt == "") $this->setRandomSalt(($randomsaltlength > 4) ? $randomsaltlength : 5);
- $this->password = Password::getHash($password, $this->salt);
- }
- /**
- * Compares another password to the password in the object
- * @param string $password The password to compare
- * @return boolean true if passwords match, false if they don't
- */
- public function compareTo($password){
- return (Password::getHash($password, $this->salt) == $this->password);
- }
- /**
- * Gets the encrypted version of the password, using sha256 and the salt
- * @return string The encrypted password
- */
- public function getEncryptedPassword(){
- return $this->password;
- }
- /**
- * Gets the salt used to encrypt the password
- * @return string The salt used to encrypt the password
- */
- public function getSalt(){
- return $this->salt;
- }
- /**
- * Sets a random initial value for $salt
- * @param Integer $length the length of the initial string
- */
- private function setRandomSalt($length){
- while($length--) $this->salt .= chr(mt_rand(97, 122)) ;
- $this->salt = hash('sha256', $this->salt);
- }
- /**
- * Returns a password has based on the data passed
- * @param String $password The password to use
- * @param String $salt The salt to use
- * @return String The password hash
- */
- public static function getHash($password, $salt){
- return pbkdf2("sha256", $password, $salt, 1000, 50);
- }
- /**
- * Private method for generating pbkdf2 hash
- */
- static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
- {
- $algorithm = strtolower($algorithm);
- if(!in_array($algorithm, hash_algos(), true))
- trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR);
- if($count <= 0 || $key_length <= 0)
- trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR);
- if (function_exists("hash_pbkdf2")) {
- // The output length is in NIBBLES (4-bits) if $raw_output is false!
- if (!$raw_output) {
- $key_length = $key_length * 2;
- }
- return hash_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output);
- }
- $hash_length = strlen(hash($algorithm, "", true));
- $block_count = ceil($key_length / $hash_length);
- $output = "";
- for($i = 1; $i <= $block_count; $i++) {
- // $i encoded as 4 bytes, big endian.
- $last = $salt . pack("N", $i);
- // first iteration
- $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
- // perform the other $count - 1 iterations
- for ($j = 1; $j < $count; $j++) {
- $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
- }
- $output .= $xorsum;
- }
- if($raw_output)
- return substr($output, 0, $key_length);
- else
- return bin2hex(substr($output, 0, $key_length));
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement