Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1.  
  2. /**
  3.  * Verifica se a string equivale ao hash
  4.  *
  5.  * @author Yallison Gabriel
  6.  */
  7. class BcryptHasher {
  8.    
  9.     /**
  10.      * Default crypt cost factor.
  11.      *
  12.      * @var int
  13.      */
  14.     protected $rounds = 10;
  15.  
  16.     /**
  17.      * Hash the given value.
  18.      *
  19.      * @param  string  $value
  20.      * @param  array   $options
  21.      * @return string
  22.      *
  23.      * @throws \RuntimeException
  24.      */
  25.     public function make($value, array $options = [])
  26.     {
  27.         $cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds;
  28.  
  29.         $hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);
  30.  
  31.         if ($hash === false) {
  32.             throw new RuntimeException('Bcrypt hashing not supported.');
  33.         }
  34.  
  35.         return $hash;
  36.     }
  37.  
  38.     /**
  39.      * Check the given plain value against a hash.
  40.      *
  41.      * @param  string  $value
  42.      * @param  string  $hashedValue
  43.      * @param  array   $options
  44.      * @return bool
  45.      */
  46.     public function check($value, $hashedValue, array $options = [])
  47.     {
  48.         if (strlen($hashedValue) === 0) {
  49.             return false;
  50.         }
  51.  
  52.         return password_verify($value, $hashedValue);
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement