public static function getSalt($encryption = 'md5-hex', $seed = '', $plaintext = '')
	{
		// Encrypt the password.
		switch ($encryption)
		{
			case 'crypt':
			case 'crypt-des':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 2);
				}
				else
				{
					return substr(md5(mt_rand()), 0, 2);
				}
			break;

			case 'crypt-md5':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
				}
				else
				{
					return '$1$' . substr(md5(mt_rand()), 0, 8) . '$';
				}
			break;

			case 'crypt-blowfish':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 16);
				}
				else
				{
					return '$2$' . substr(md5(mt_rand()), 0, 12) . '$';
				}
			break;

			case 'crypt-sha512':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
				}
				else
				{
					return '$6$' . substr(md5(mt_rand()), 0, 8) . '$';
				}
			break;

			case 'ssha':
				if ($seed)
				{
					return substr(preg_replace('|^{SSHA}|', '', $seed), -20);
				}
				else
				{
					return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
				}
			break;

			case 'smd5':
				if ($seed)
				{
					return substr(preg_replace('|^{SMD5}|', '', $seed), -16);
				}
				else
				{
					return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
				}
			break;

			case 'aprmd5':
				/* 64 characters that are valid for APRMD5 passwords. */
				$APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

				if ($seed)
				{
					return substr(preg_replace('/^\$apr1\$(.{8}).*/', '\\1', $seed), 0, 8);
				}
				else
				{
					$salt = '';
					for ($i = 0; $i < 8; $i++)
					{
						$salt .= $APRMD5{rand(0, 63)};
					}
					return $salt;
				}
			break;

			default:
				$salt = '';
				if ($seed)
				{
					$salt = $seed;
				}
				return $salt;
			break;
		}
	}
public static function getSalt($encryption = 'md5-hex', $seed = '', $plaintext = '')
	{
		// Encrypt the password.
		switch ($encryption)
		{
			case 'crypt':
			case 'crypt-des':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 2);
				}
				else
				{
					return substr(md5(mt_rand()), 0, 2);
				}
			break;

			case 'crypt-md5':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
				}
				else
				{
					return '$1$' . substr(md5(mt_rand()), 0, 8) . '$';
				}
			break;

			case 'crypt-blowfish':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 16);
				}
				else
				{
					return '$2$' . substr(md5(mt_rand()), 0, 12) . '$';
				}
			break;

			case 'crypt-sha512':
				if ($seed)
				{
					return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
				}
				else
				{
					return '$6$' . substr(md5(mt_rand()), 0, 8) . '$';
				}
			break;

			case 'ssha':
				if ($seed)
				{
					return substr(preg_replace('|^{SSHA}|', '', $seed), -20);
				}
				else
				{
					return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
				}
			break;

			case 'smd5':
				if ($seed)
				{
					return substr(preg_replace('|^{SMD5}|', '', $seed), -16);
				}
				else
				{
					return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
				}
			break;

			case 'aprmd5':
				/* 64 characters that are valid for APRMD5 passwords. */
				$APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

				if ($seed)
				{
					return substr(preg_replace('/^\$apr1\$(.{8}).*/', '\\1', $seed), 0, 8);
				}
				else
				{
					$salt = '';
					for ($i = 0; $i < 8; $i++)
					{
						$salt .= $APRMD5{rand(0, 63)};
					}
					return $salt;
				}
			break;

			default:
				$salt = '';
				if ($seed)
				{
					$salt = $seed;
				}
				return $salt;
			break;
		}
	}
public static function getSalt ( $ encryption = 'md5-hex' , $ seed = '' , $ plaintext = '' ) { // Encrypt the password. switch ( $ encryption ) { case 'crypt' : case 'crypt-des' : if ( $ seed ) { return substr ( preg_replace ( '|^{crypt}|i' , '' , $ seed ) , 0 , 2 ) ; } else { return substr ( md5 ( mt_rand ( ) ) , 0 , 2 ) ; } break ; case 'crypt-md5' : if ( $ seed ) { return substr ( preg_replace ( '|^{crypt}|i' , '' , $ seed ) , 0 , 12 ) ; } else { return '$1$' . substr ( md5 ( mt_rand ( ) ) , 0 , 8 ) . '$' ; } break ; case 'crypt-blowfish' : if ( $ seed ) { return substr ( preg_replace ( '|^{crypt}|i' , '' , $ seed ) , 0 , 16 ) ; } else { return '$2$' . substr ( md5 ( mt_rand ( ) ) , 0 , 12 ) . '$' ; } break ; case 'crypt-sha512' : if ( $ seed ) { return substr ( preg_replace ( '|^{crypt}|i' , '' , $ seed ) , 0 , 12 ) ; } else { return '$6$' . substr ( md5 ( mt_rand ( ) ) , 0 , 8 ) . '$' ; } break ; case 'ssha' : if ( $ seed ) { return substr ( preg_replace ( '|^{SSHA}|' , '' , $ seed ) , - 20 ) ; } else { return mhash_keygen_s2k ( MHASH_SHA1 , $ plaintext , substr ( pack ( 'h*' , md5 ( mt_rand ( ) ) ) , 0 , 8 ) , 4 ) ; } break ; case 'smd5' : if ( $ seed ) { return substr ( preg_replace ( '|^{SMD5}|' , '' , $ seed ) , - 16 ) ; } else { return mhash_keygen_s2k ( MHASH_MD5 , $ plaintext , substr ( pack ( 'h*' , md5 ( mt_rand ( ) ) ) , 0 , 8 ) , 4 ) ; } break ; case 'aprmd5' : /* 64 characters that are valid for APRMD5 passwords. */ $ APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' ; if ( $ seed ) { return substr ( preg_replace ( '/^\$apr1\$(.{8}).*/' , '\\1' , $ seed ) , 0 , 8 ) ; } else { $ salt = '' ; for ( $ i = 0 ; $ i < 8 ; $ i ++ ) { $ salt .= $ APRMD5 { rand ( 0 , 63 ) } ; } return $ salt ; } break ; default : $ salt = '' ; if ( $ seed ) { $ salt = $ seed ; } return $ salt ; break ; } }
Returns a salt for the appropriate kind of password encryption.
Optionally takes a seed and a plaintext password, to extract the seed
of an existing password, or for encryption types that use the plaintext
in the generation of the salt.

@param   string  $encryption  The kind of password encryption to use. Defaults to md5-hex.
@param   string  $seed        The seed to get the salt from (probably a previously generated password). Defaults to generating a new seed.
@param   string  $plaintext   The plaintext password that we're generating  a salt for. Defaults to none.
@return  string  The generated or extracted salt.
Returns a salt for the appropriate kind of password encryption . Optionally takes a seed and a plaintext password to extract the seed of an existing password or for encryption types that use the plaintext in the generation of the salt .