Advertisement
Guest User

Untitled

a guest
Sep 4th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.06 KB | None | 0 0
  1. <?php
  2. #
  3. # Portable PHP password hashing framework.
  4. #
  5. # Version 0.3 / genuine.
  6. #
  7. # Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in
  8. # the public domain.  Revised in subsequent years, still public domain.
  9. #
  10. # There's absolutely no warranty.
  11. #
  12. # The homepage URL for this framework is:
  13. #
  14. #   http://www.openwall.com/phpass/
  15. #
  16. # Please be sure to update the Version line if you edit this file in any way.
  17. # It is suggested that you leave the main version number intact, but indicate
  18. # your project name (after the slash) and add your own revision information.
  19. #
  20. # Please do not change the "private" password hashing method implemented in
  21. # here, thereby making your hashes incompatible.  However, if you must, please
  22. # change the hash type identifier (the "$P$") to something different.
  23. #
  24. # Obviously, since this code is in the public domain, the above are not
  25. # requirements (there can be none), but merely suggestions.
  26. #
  27. # MODIFIED BY JORDANARSENO -- ADDED CONSTRUCTOR FOR CODEIGNITER.
  28. #
  29. class Phpass {
  30.     var $itoa64;
  31.     var $iteration_count_log2;
  32.     var $portable_hashes;
  33.     var $random_state;
  34.  
  35.     function __construct($params)
  36.     {  
  37.         $iteration_count_log2 = $params['iteration_count_log2'];
  38.         $portable_hashes = $params['portable_hashes'];
  39.  
  40.         $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  41.  
  42.         if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
  43.             $iteration_count_log2 = 8;
  44.         $this->iteration_count_log2 = $iteration_count_log2;
  45.  
  46.         $this->portable_hashes = $portable_hashes;
  47.  
  48.         $this->random_state = microtime();
  49.         if (function_exists('getmypid'))
  50.             $this->random_state .= getmypid();
  51.     }
  52.  
  53.     function get_random_bytes($count)
  54.     {
  55.         $output = '';
  56.         if (is_readable('/dev/urandom') &&
  57.             ($fh = @fopen('/dev/urandom', 'rb'))) {
  58.             $output = fread($fh, $count);
  59.             fclose($fh);
  60.         }
  61.  
  62.         if (strlen($output) < $count) {
  63.             $output = '';
  64.             for ($i = 0; $i < $count; $i += 16) {
  65.                 $this->random_state =
  66.                     md5(microtime() . $this->random_state);
  67.                 $output .=
  68.                     pack('H*', md5($this->random_state));
  69.             }
  70.             $output = substr($output, 0, $count);
  71.         }
  72.  
  73.         return $output;
  74.     }
  75.  
  76.     function encode64($input, $count)
  77.     {
  78.         $output = '';
  79.         $i = 0;
  80.         do {
  81.             $value = ord($input[$i++]);
  82.             $output .= $this->itoa64[$value & 0x3f];
  83.             if ($i < $count)
  84.                 $value |= ord($input[$i]) << 8;
  85.             $output .= $this->itoa64[($value >> 6) & 0x3f];
  86.             if ($i++ >= $count)
  87.                 break;
  88.             if ($i < $count)
  89.                 $value |= ord($input[$i]) << 16;
  90.             $output .= $this->itoa64[($value >> 12) & 0x3f];
  91.             if ($i++ >= $count)
  92.                 break;
  93.             $output .= $this->itoa64[($value >> 18) & 0x3f];
  94.         } while ($i < $count);
  95.  
  96.         return $output;
  97.     }
  98.  
  99.     function gensalt_private($input)
  100.     {
  101.         $output = '$P$';
  102.         $output .= $this->itoa64[min($this->iteration_count_log2 +
  103.             ((PHP_VERSION >= '5') ? 5 : 3), 30)];
  104.         $output .= $this->encode64($input, 6);
  105.  
  106.         return $output;
  107.     }
  108.  
  109.     function crypt_private($password, $setting)
  110.     {
  111.         $output = '*0';
  112.         if (substr($setting, 0, 2) == $output)
  113.             $output = '*1';
  114.  
  115.         $id = substr($setting, 0, 3);
  116.         # We use "$P$", phpBB3 uses "$H$" for the same thing
  117.         if ($id != '$P$' && $id != '$H$')
  118.             return $output;
  119.  
  120.         $count_log2 = strpos($this->itoa64, $setting[3]);
  121.         if ($count_log2 < 7 || $count_log2 > 30)
  122.             return $output;
  123.  
  124.         $count = 1 << $count_log2;
  125.  
  126.         $salt = substr($setting, 4, 8);
  127.         if (strlen($salt) != 8)
  128.             return $output;
  129.  
  130.         # We're kind of forced to use MD5 here since it's the only
  131.         # cryptographic primitive available in all versions of PHP
  132.         # currently in use.  To implement our own low-level crypto
  133.         # in PHP would result in much worse performance and
  134.         # consequently in lower iteration counts and hashes that are
  135.         # quicker to crack (by non-PHP code).
  136.         if (PHP_VERSION >= '5') {
  137.             $hash = md5($salt . $password, TRUE);
  138.             do {
  139.                 $hash = md5($hash . $password, TRUE);
  140.             } while (--$count);
  141.         } else {
  142.             $hash = pack('H*', md5($salt . $password));
  143.             do {
  144.                 $hash = pack('H*', md5($hash . $password));
  145.             } while (--$count);
  146.         }
  147.  
  148.         $output = substr($setting, 0, 12);
  149.         $output .= $this->encode64($hash, 16);
  150.  
  151.         return $output;
  152.     }
  153.  
  154.     function gensalt_extended($input)
  155.     {
  156.         $count_log2 = min($this->iteration_count_log2 + 8, 24);
  157.         # This should be odd to not reveal weak DES keys, and the
  158.         # maximum valid value is (2**24 - 1) which is odd anyway.
  159.         $count = (1 << $count_log2) - 1;
  160.  
  161.         $output = '_';
  162.         $output .= $this->itoa64[$count & 0x3f];
  163.         $output .= $this->itoa64[($count >> 6) & 0x3f];
  164.         $output .= $this->itoa64[($count >> 12) & 0x3f];
  165.         $output .= $this->itoa64[($count >> 18) & 0x3f];
  166.  
  167.         $output .= $this->encode64($input, 3);
  168.  
  169.         return $output;
  170.     }
  171.  
  172.     function gensalt_blowfish($input)
  173.     {
  174.         # This one needs to use a different order of characters and a
  175.         # different encoding scheme from the one in encode64() above.
  176.         # We care because the last character in our encoded string will
  177.         # only represent 2 bits.  While two known implementations of
  178.         # bcrypt will happily accept and correct a salt string which
  179.         # has the 4 unused bits set to non-zero, we do not want to take
  180.         # chances and we also do not want to waste an additional byte
  181.         # of entropy.
  182.         $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  183.  
  184.         $output = '$2a$';
  185.         $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
  186.         $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
  187.         $output .= '$';
  188.  
  189.         $i = 0;
  190.         do {
  191.             $c1 = ord($input[$i++]);
  192.             $output .= $itoa64[$c1 >> 2];
  193.             $c1 = ($c1 & 0x03) << 4;
  194.             if ($i >= 16) {
  195.                 $output .= $itoa64[$c1];
  196.                 break;
  197.             }
  198.  
  199.             $c2 = ord($input[$i++]);
  200.             $c1 |= $c2 >> 4;
  201.             $output .= $itoa64[$c1];
  202.             $c1 = ($c2 & 0x0f) << 2;
  203.  
  204.             $c2 = ord($input[$i++]);
  205.             $c1 |= $c2 >> 6;
  206.             $output .= $itoa64[$c1];
  207.             $output .= $itoa64[$c2 & 0x3f];
  208.         } while (1);
  209.  
  210.         return $output;
  211.     }
  212.  
  213.     function HashPassword($password)
  214.     {
  215.         $random = '';
  216.  
  217.         if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
  218.             $random = $this->get_random_bytes(16);
  219.             $hash =
  220.                 crypt($password, $this->gensalt_blowfish($random));
  221.             if (strlen($hash) == 60)
  222.                 return $hash;
  223.         }
  224.  
  225.         if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
  226.             if (strlen($random) < 3)
  227.                 $random = $this->get_random_bytes(3);
  228.             $hash =
  229.                 crypt($password, $this->gensalt_extended($random));
  230.             if (strlen($hash) == 20)
  231.                 return $hash;
  232.         }
  233.  
  234.         if (strlen($random) < 6)
  235.             $random = $this->get_random_bytes(6);
  236.         $hash =
  237.             $this->crypt_private($password,
  238.             $this->gensalt_private($random));
  239.         if (strlen($hash) == 34)
  240.             return $hash;
  241.  
  242.         # Returning '*' on error is safe here, but would _not_ be safe
  243.         # in a crypt(3)-like function used _both_ for generating new
  244.         # hashes and for validating passwords against existing hashes.
  245.         return '*';
  246.     }
  247.  
  248.     function CheckPassword($password, $stored_hash)
  249.     {
  250.         $hash = $this->crypt_private($password, $stored_hash);
  251.         if ($hash[0] == '*')
  252.             $hash = crypt($password, $stored_hash);
  253.  
  254.         return $hash == $stored_hash;
  255.     }
  256. }
  257.  
  258. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement