[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/ -> class.passwordhash.php (source)

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


Generated: Tue Mar 16 03:57:15 2010 Hosted by follow the white rabbit.