Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 7.90 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Authenticating plain text password against md5 hash
  2. $salt  = JUserHelper::genRandomPassword(32);
  3. $crypt = JUserHelper::getCryptedPassword($array['password'], $salt);
  4. $array['password'] = $crypt.':'.$salt;
  5.        
  6. /**
  7.  * Generate a random password
  8.  *
  9.  * @static
  10.  * @param   int     $length Length of the password to generate
  11.  * @return  string          Random Password
  12.  * @since   1.5
  13.  */
  14. public static function genRandomPassword($length = 8)
  15. {
  16.     $salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  17.     $len = strlen($salt);
  18.     $makepass = '';
  19.  
  20.     $stat = @stat(__FILE__);
  21.     if (empty($stat) || !is_array($stat)) $stat = array(php_uname());
  22.  
  23.     mt_srand(crc32(microtime() . implode('|', $stat)));
  24.  
  25.     for ($i = 0; $i < $length; $i ++) {
  26.         $makepass .= $salt[mt_rand(0, $len -1)];
  27.     }
  28.  
  29.     return $makepass;
  30. }
  31.        
  32. /**
  33.  * Formats a password using the current encryption.
  34.  *
  35.  * @access  public
  36.  * @param   string  $plaintext  The plaintext password to encrypt.
  37.  * @param   string  $salt       The salt to use to encrypt the password. []
  38.  *                              If not present, a new salt will be
  39.  *                              generated.
  40.  * @param   string  $encryption The kind of pasword encryption to use.
  41.  *                              Defaults to md5-hex.
  42.  * @param   boolean $show_encrypt  Some password systems prepend the kind of
  43.  *                              encryption to the crypted password ({SHA},
  44.  *                              etc). Defaults to false.
  45.  *
  46.  * @return string  The encrypted password.
  47.  */
  48. public static function getCryptedPassword($plaintext, $salt = '', $encryption = 'md5-hex', $show_encrypt = false)
  49. {
  50.     // Get the salt to use.
  51.     $salt = JUserHelper::getSalt($encryption, $salt, $plaintext);
  52.  
  53.     // Encrypt the password.
  54.     switch ($encryption)
  55.     {
  56.         case 'plain' :
  57.             return $plaintext;
  58.  
  59.         case 'sha' :
  60.             $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext));
  61.             return ($show_encrypt) ? '{SHA}'.$encrypted : $encrypted;
  62.  
  63.         case 'crypt' :
  64.         case 'crypt-des' :
  65.         case 'crypt-md5' :
  66.         case 'crypt-blowfish' :
  67.             return ($show_encrypt ? '{crypt}' : '').crypt($plaintext, $salt);
  68.  
  69.         case 'md5-base64' :
  70.             $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext));
  71.             return ($show_encrypt) ? '{MD5}'.$encrypted : $encrypted;
  72.  
  73.         case 'ssha' :
  74.             $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext.$salt).$salt);
  75.             return ($show_encrypt) ? '{SSHA}'.$encrypted : $encrypted;
  76.  
  77.         case 'smd5' :
  78.             $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext.$salt).$salt);
  79.             return ($show_encrypt) ? '{SMD5}'.$encrypted : $encrypted;
  80.  
  81.         case 'aprmd5' :
  82.             $length = strlen($plaintext);
  83.             $context = $plaintext.'$apr1$'.$salt;
  84.             $binary = JUserHelper::_bin(md5($plaintext.$salt.$plaintext));
  85.  
  86.             for ($i = $length; $i > 0; $i -= 16) {
  87.                 $context .= substr($binary, 0, ($i > 16 ? 16 : $i));
  88.             }
  89.             for ($i = $length; $i > 0; $i >>= 1) {
  90.                 $context .= ($i & 1) ? chr(0) : $plaintext[0];
  91.             }
  92.  
  93.             $binary = JUserHelper::_bin(md5($context));
  94.  
  95.             for ($i = 0; $i < 1000; $i ++) {
  96.                 $new = ($i & 1) ? $plaintext : substr($binary, 0, 16);
  97.                 if ($i % 3) {
  98.                     $new .= $salt;
  99.                 }
  100.                 if ($i % 7) {
  101.                     $new .= $plaintext;
  102.                 }
  103.                 $new .= ($i & 1) ? substr($binary, 0, 16) : $plaintext;
  104.                 $binary = JUserHelper::_bin(md5($new));
  105.             }
  106.  
  107.             $p = array ();
  108.             for ($i = 0; $i < 5; $i ++) {
  109.                 $k = $i +6;
  110.                 $j = $i +12;
  111.                 if ($j == 16) {
  112.                     $j = 5;
  113.                 }
  114.                 $p[] = JUserHelper::_toAPRMD5((ord($binary[$i]) << 16) | (ord($binary[$k]) << 8) | (ord($binary[$j])), 5);
  115.             }
  116.  
  117.             return '$apr1$'.$salt.'$'.implode('', $p).JUserHelper::_toAPRMD5(ord($binary[11]), 3);
  118.  
  119.         case 'md5-hex' :
  120.         default :
  121.             $encrypted = ($salt) ? md5($plaintext.$salt) : md5($plaintext);
  122.             return ($show_encrypt) ? '{MD5}'.$encrypted : $encrypted;
  123.     }
  124. }
  125.  
  126. /**
  127.  * Returns a salt for the appropriate kind of password encryption.
  128.  * Optionally takes a seed and a plaintext password, to extract the seed
  129.  * of an existing password, or for encryption types that use the plaintext
  130.  * in the generation of the salt.
  131.  *
  132.  * @access public
  133.  * @param string $encryption  The kind of pasword encryption to use.
  134.  *                          Defaults to md5-hex.
  135.  * @param string $seed      The seed to get the salt from (probably a
  136.  *                          previously generated password). Defaults to
  137.  *                          generating a new seed.
  138.  * @param string $plaintext The plaintext password that we're generating
  139.  *                          a salt for. Defaults to none.
  140.  *
  141.  * @return string  The generated or extracted salt.
  142.  */
  143. public static function getSalt($encryption = 'md5-hex', $seed = '', $plaintext = '')
  144. {
  145.     // Encrypt the password.
  146.     switch ($encryption)
  147.     {
  148.         case 'crypt' :
  149.         case 'crypt-des' :
  150.             if ($seed) {
  151.                 return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 2);
  152.             } else {
  153.                 return substr(md5(mt_rand()), 0, 2);
  154.             }
  155.             break;
  156.  
  157.         case 'crypt-md5' :
  158.             if ($seed) {
  159.                 return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
  160.             } else {
  161.                 return '$1$'.substr(md5(mt_rand()), 0, 8).'$';
  162.             }
  163.             break;
  164.  
  165.         case 'crypt-blowfish' :
  166.             if ($seed) {
  167.                 return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 16);
  168.             } else {
  169.                 return '$2$'.substr(md5(mt_rand()), 0, 12).'$';
  170.             }
  171.             break;
  172.  
  173.         case 'ssha' :
  174.             if ($seed) {
  175.                 return substr(preg_replace('|^{SSHA}|', '', $seed), -20);
  176.             } else {
  177.                 return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  178.             }
  179.             break;
  180.  
  181.         case 'smd5' :
  182.             if ($seed) {
  183.                 return substr(preg_replace('|^{SMD5}|', '', $seed), -16);
  184.             } else {
  185.                 return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  186.             }
  187.             break;
  188.  
  189.         case 'aprmd5' :
  190.             /* 64 characters that are valid for APRMD5 passwords. */
  191.             $APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  192.  
  193.             if ($seed) {
  194.                 return substr(preg_replace('/^$apr1$(.{8}).*/', '\1', $seed), 0, 8);
  195.             } else {
  196.                 $salt = '';
  197.                 for ($i = 0; $i < 8; $i ++) {
  198.                     $salt .= $APRMD5 {
  199.                         rand(0, 63)
  200.                         };
  201.                 }
  202.                 return $salt;
  203.             }
  204.             break;
  205.  
  206.         default :
  207.             $salt = '';
  208.             if ($seed) {
  209.                 $salt = $seed;
  210.             }
  211.             return $salt;
  212.             break;
  213.     }
  214. }
  215.        
  216. SimpleAuthenticationInfo info = new SimpleAuthenticationInfo("TestUser",
  217.             "564c6d2c10a7135fe0ddf0b21d1a1226", getName());
  218.     info.setCredentialsSalt(new SimpleByteSource("B9YEkhvnV8pZ8BU7fvVlIVTbEux5N17J"));
  219.  
  220.  
  221.     return info;
  222.        
  223. Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - TestUser, rememberMe=false] did not match the expected credentials.
  224.        
  225. jimport( 'joomla.user.helper' );
  226. $userId = JUserHelper::getUserId( $un );
  227. $user = JUser::getInstance( $userId );
  228.  
  229. $existingPasswordParts = explode( ':', $user->password );
  230. $salt = $existingPasswordParts[1];
  231. $crypt = JUserHelper::getCryptedPassword( $pw, $salt );
  232. $password = $crypt . ':' . $salt;
  233.  
  234. if ( $user->password == $password )
  235. {
  236.   /* match */
  237. }