Advertisement
MostafaCss

Hash

Aug 10th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. class Bcrypt {
  2.         private $rounds;
  3.         public function __construct($rounds = 12) {
  4.             if(CRYPT_BLOWFISH != 1) {
  5.                 throw new Exception("bcrypt not supported in this installation. See http://php.net/crypt");
  6.             }
  7.  
  8.             $this->rounds = $rounds;
  9.         }
  10.  
  11.         public function hash($input) {
  12.             $hash = crypt($input, $this->getSalt());
  13.  
  14.             if(strlen($hash) > 13)
  15.                 return $hash;
  16.  
  17.             return false;
  18.         }
  19.  
  20.         public function verify($input, $existingHash) {
  21.             $hash = crypt($input, $existingHash);
  22.  
  23.             return $hash === $existingHash;
  24.         }
  25.  
  26.         private function getSalt() {
  27.             $salt = sprintf('$2a$%02d$', $this->rounds);
  28.  
  29.             $bytes = $this->getRandomBytes(16);
  30.  
  31.             $salt .= $this->encodeBytes($bytes);
  32.  
  33.             return $salt;
  34.         }
  35.  
  36.         private $randomState;
  37.         private function getRandomBytes($count) {
  38.             $bytes = '';
  39.  
  40.             if(function_exists('openssl_random_pseudo_bytes') &&
  41.                 (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) { // OpenSSL slow on Win
  42.                 $bytes = openssl_random_pseudo_bytes($count);
  43.             }
  44.  
  45.             if($bytes === '' && is_readable('/dev/urandom') &&
  46.                 ($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
  47.                 $bytes = fread($hRand, $count);
  48.                 fclose($hRand);
  49.             }
  50.  
  51.             if(strlen($bytes) < $count) {
  52.                 $bytes = '';
  53.  
  54.                 if($this->randomState === null) {
  55.                     $this->randomState = microtime();
  56.                     if(function_exists('getmypid')) {
  57.                         $this->randomState .= getmypid();
  58.                     }
  59.                 }
  60.  
  61.                 for($i = 0; $i < $count; $i += 16) {
  62.                     $this->randomState = md5(microtime() . $this->randomState);
  63.  
  64.                     if (PHP_VERSION >= '5') {
  65.                         $bytes .= md5($this->randomState, true);
  66.                     } else {
  67.                         $bytes .= pack('H*', md5($this->randomState));
  68.                     }
  69.                 }
  70.  
  71.                 $bytes = substr($bytes, 0, $count);
  72.             }
  73.  
  74.             return $bytes;
  75.         }
  76.  
  77.         private function encodeBytes($input) {
  78.             // The following is code from the PHP Password Hashing Framework
  79.             $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  80.  
  81.             $output = '';
  82.             $i = 0;
  83.             do {
  84.                 $c1 = ord($input[$i++]);
  85.                 $output .= $itoa64[$c1 >> 2];
  86.                 $c1 = ($c1 & 0x03) << 4;
  87.                 if ($i >= 16) {
  88.                     $output .= $itoa64[$c1];
  89.                     break;
  90.                 }
  91.  
  92.                 $c2 = ord($input[$i++]);
  93.                 $c1 |= $c2 >> 4;
  94.                 $output .= $itoa64[$c1];
  95.                 $c1 = ($c2 & 0x0f) << 2;
  96.  
  97.                 $c2 = ord($input[$i++]);
  98.                 $c1 |= $c2 >> 6;
  99.                 $output .= $itoa64[$c1];
  100.                 $output .= $itoa64[$c2 & 0x3f];
  101.             } while (1);
  102.  
  103.             return $output;
  104.         }
  105.  
  106.  
  107.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement