Advertisement
j0h4n54ntr1

libraries

Feb 23rd, 2018
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Password {
  4.  
  5.  
  6.     const PBKDF2_HASH_ALGORITHM = 'sha256';
  7.     const PBKDF2_ITERATIONS = 1000;
  8.     const PBKDF2_SALT_BYTE_SIZE = 24;
  9.     const PBKDF2_HASH_BYTE_SIZE = 24;
  10.    
  11.     const HASH_SECTIONS = 4;
  12.     const HASH_ALGORITHM_INDEX = 0;
  13.     const HASH_ITERATION_INDEX = 1;
  14.     const HASH_SALT_INDEX = 2;
  15.     const HASH_PBKDF2_INDEX = 3;
  16.  
  17.     function create_hash($password)
  18.     {
  19.         // catatan ini tidak berlaku untuk php versi terbaru, karena sudah tidak di dukung
  20.         $salt = base64_encode(mcrypt_create_iv(self::PBKDF2_SALT_BYTE_SIZE, MCRYPT_DEV_URANDOM));
  21.         return self::PBKDF2_HASH_ALGORITHM . ":" . self::PBKDF2_ITERATIONS . ":" .  $salt . ":" .
  22.             base64_encode($this->pbkdf2(
  23.                 self::PBKDF2_HASH_ALGORITHM,
  24.                 $password,
  25.                 $salt,
  26.                 self::PBKDF2_ITERATIONS,
  27.                 self::PBKDF2_HASH_BYTE_SIZE,
  28.                 true
  29.             ));
  30.     }
  31.  
  32.     function validate_password($password, $correct_hash)
  33.     {
  34.         $params = explode(":", $correct_hash);
  35.         if(count($params) < self::HASH_SECTIONS)
  36.            return false;
  37.         $pbkdf2 = base64_decode($params[self::HASH_PBKDF2_INDEX]);
  38.         return $this->slow_equals(
  39.             $pbkdf2,
  40.             $this->pbkdf2(
  41.                 $params[self::HASH_ALGORITHM_INDEX],
  42.                 $password,
  43.                 $params[self::HASH_SALT_INDEX],
  44.                 (int)$params[self::HASH_ITERATION_INDEX],
  45.                 strlen($pbkdf2),
  46.                 true
  47.             )
  48.         );
  49.     }
  50.  
  51.     // Compares two strings $a and $b in length-constant time.
  52.     function slow_equals($a, $b)
  53.     {
  54.         $diff = strlen($a) ^ strlen($b);
  55.         for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
  56.         {
  57.             $diff |= ord($a[$i]) ^ ord($b[$i]);
  58.         }
  59.         return $diff === 0;
  60.     }
  61.  
  62.     /*
  63.      * PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
  64.      * $algorithm - The hash algorithm to use. Recommended: SHA256
  65.      * $password - The password.
  66.      * $salt - A salt that is unique to the password.
  67.      * $count - Iteration count. Higher is better, but slower. Recommended: At least 1000.
  68.      * $key_length - The length of the derived key in bytes.
  69.      * $raw_output - If true, the key is returned in raw binary format. Hex encoded otherwise.
  70.      * Returns: A $key_length-byte key derived from the password and salt.
  71.      *
  72.      * Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
  73.      *
  74.      * This implementation of PBKDF2 was originally created by https://defuse.ca
  75.      * With improvements by http://www.variations-of-shadow.com
  76.      */
  77.     function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
  78.     {
  79.         $algorithm = strtolower($algorithm);
  80.         if(!in_array($algorithm, hash_algos(), true))
  81.             die('PBKDF2 ERROR: Invalid hash algorithm.');
  82.         if($count <= 0 || $key_length <= 0)
  83.             die('PBKDF2 ERROR: Invalid parameters.');
  84.  
  85.         $hash_length = strlen(hash($algorithm, "", true));
  86.         $block_count = ceil($key_length / $hash_length);
  87.  
  88.         $output = "";
  89.         for($i = 1; $i <= $block_count; $i++) {
  90.             // $i encoded as 4 bytes, big endian.
  91.             $last = $salt . pack("N", $i);
  92.             // first iteration
  93.             $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
  94.             // perform the other $count - 1 iterations
  95.             for ($j = 1; $j < $count; $j++) {
  96.                 $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
  97.             }
  98.             $output .= $xorsum;
  99.         }
  100.  
  101.         if($raw_output)
  102.             return substr($output, 0, $key_length);
  103.         else
  104.             return bin2hex(substr($output, 0, $key_length));
  105.     }
  106.    
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement