Return to Snippet

Revision: 19582
at October 28, 2009 07:10 by AzizLight


Updated Code
#!/usr/bin/php
<?php

/**
* Generate a random password
*/
class Password
{
	/**
	 * Generate the new password
	 *
	 * @access public
	 * @param array $params
	 * @return string
	 **/
	public function generate($params = array())
	{
		
		$length     = (!array_key_exists('length', $params))     ? 15     :     $params['length'];
		$use_lower  = (!array_key_exists('use_lower', $params))  ? TRUE   :  $params['use_lower'];
		$use_upper  = (!array_key_exists('use_upper', $params))  ? TRUE   :  $params['use_upper'];
		$use_number = (!array_key_exists('use_number', $params)) ? TRUE   : $params['use_number'];
		$use_custom = (!array_key_exists('use_custom', $params)) ? '!#%-_()' : $params['use_custom'];
		
		$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$lower = "abcdefghijklmnopqrstuvwxyz";
		$number = "0123456789";
		
		$seed_length = 0;
		$seed        = '';
		$password    = '';
		
		if($use_upper === TRUE){
			$seed_length += 26;
			$seed .= $upper;
		}
		if($use_lower === TRUE){
			$seed_length += 26;
			$seed .= $lower;
		}
		if($use_number === TRUE){
			$seed_length += 10;
			$seed .= $number;
		}
		if(!empty($use_custom)){
			$seed_length +=strlen($use_custom);
			$seed .= $use_custom;
		}
		for($i = 1; $i <= $length; $i++){
			$password .= $seed{rand(0,$seed_length-1)};
		}
		return $password;
	} // End of generate
} // End of Class Password

// ------------------------------------------------------------------------

/**
 * With Special Characters
 **/
echo "\nWith Special Characters: \n";
echo "-------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5)) . "\n";
for ($i=10; $i <= 80; $i += 10) { 
	$password = new Password;
	echo $i . ': ' . $password->generate(array('length' => $i)) . "\n";
}

// ------------------------------------------------------------------------

/**
 * Without Special Characters
 **/
echo "\nWithout Special Characters: \n";
echo "----------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5, 'use_custom' => '')) . "\n";
for ($i=10; $i <= 80; $i += 10) { 
	$password = new Password;
	echo $i . ': ' . $password->generate(array('length' => $i, 'use_custom' => '')) . "\n";
}

// ------------------------------------------------------------------------

/**
 * SHA1 Based
 * These passwords will only contain lowercase letters from a to f and numbers from 0 to 9
 **/
$seed = sha1(uniqid(mt_rand(), true));
$hash1 = sha1(uniqid($seed . mt_rand(), true));
$hash2 = sha1(uniqid($seed . mt_rand(), true)).sha1(uniqid($hash1 . mt_rand(), true));
echo "\nSHA1 Based: \n";
echo "------------\n";
echo " 5: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 5) . "\n";
echo "10: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 10) . "\n";
echo "20: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 20) . "\n";
echo "30: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 30) . "\n";
echo "40: ${hash1}\n";
echo "50: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 50) . "\n";
echo "60: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 60) . "\n";
echo "70: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 70) . "\n";
echo "80: ${hash2}\n";

Revision: 19581
at October 26, 2009 11:43 by AzizLight


Updated Code
#!/usr/bin/php
<?php

/**
* Generate a random password
*/
class Password
{
	/**
	 * Generate the new password
	 *
	 * @access public
	 * @param array $params
	 * @return string
	 **/
	public function generate($params = array())
	{
		
		$length     = (!array_key_exists('length', $params))     ? 15     :     $params['length'];
		$use_lower  = (!array_key_exists('use_lower', $params))  ? TRUE   :  $params['use_lower'];
		$use_upper  = (!array_key_exists('use_upper', $params))  ? TRUE   :  $params['use_upper'];
		$use_number = (!array_key_exists('use_number', $params)) ? TRUE   : $params['use_number'];
		$use_custom = (!array_key_exists('use_custom', $params)) ? '!#%-_()' : $params['use_custom'];
		
		$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$lower = "abcdefghijklmnopqrstuvwxyz";
		$number = "0123456789";
		
		$seed_length = 0;
		$seed        = '';
		
		if($use_upper === TRUE){
			$seed_length += 26;
			$seed .= $upper;
		}
		if($use_lower === TRUE){
			$seed_length += 26;
			$seed .= $lower;
		}
		if($use_number === TRUE){
			$seed_length += 10;
			$seed .= $number;
		}
		if(!empty($use_custom)){
			$seed_length +=strlen($use_custom);
			$seed .= $use_custom;
		}
		for($i = 1; $i <= $length; $i++){
			$password .= $seed{rand(0,$seed_length-1)};
		}
		return $password;
	} // End of generate
} // End of Class Password

// ------------------------------------------------------------------------

/**
 * With Special Characters
 **/
echo "\nWith Special Characters: \n";
echo "-------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5)) . "\n";
for ($i=10; $i <= 80; $i += 10) { 
	$password = new Password;
	echo $i . ': ' . $password->generate(array('length' => $i)) . "\n";
}

// ------------------------------------------------------------------------

/**
 * Without Special Characters
 **/
echo "\nWithout Special Characters: \n";
echo "----------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5, 'use_custom' => '')) . "\n";
for ($i=10; $i <= 80; $i += 10) { 
	$password = new Password;
	echo $i . ': ' . $password->generate(array('length' => $i, 'use_custom' => '')) . "\n";
}

// ------------------------------------------------------------------------

/**
 * SHA1 Based
 * These passwords will only contain lowercase letters from a to f and numbers from 0 to 9
 **/
$seed = sha1(uniqid(mt_rand(), true));
$hash1 = sha1(uniqid($seed . mt_rand(), true));
$hash2 = sha1(uniqid($seed . mt_rand(), true)).sha1(uniqid($hash1 . mt_rand(), true));
echo "\nSHA1 Based: \n";
echo "------------\n";
echo " 5: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 5) . "\n";
echo "10: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 10) . "\n";
echo "20: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 20) . "\n";
echo "30: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 30) . "\n";
echo "40: ${hash1}\n";
echo "50: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 50) . "\n";
echo "60: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 60) . "\n";
echo "70: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 70) . "\n";
echo "80: ${hash2}\n";

Revision: 19580
at October 26, 2009 11:30 by AzizLight


Initial Code
#!/usr/bin/php
<?php

/**
* Generate a random password
*/
class Password
{
	/**
	 * Generate the new password
	 *
	 * @access public
	 * @param array $params
	 * @return string
	 **/
	public function generate($params = array())
	{
		
		$length     = (!array_key_exists('length', $params))     ? 15     :     $params['length'];
		$use_lower  = (!array_key_exists('use_lower', $params))  ? TRUE   :  $params['use_lower'];
		$use_upper  = (!array_key_exists('use_upper', $params))  ? TRUE   :  $params['use_upper'];
		$use_number = (!array_key_exists('use_number', $params)) ? TRUE   : $params['use_number'];
		$use_custom = (!array_key_exists('use_custom', $params)) ? '-_()' : $params['use_custom'];
		
		$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$lower = "abcdefghijklmnopqrstuvwxyz";
		$number = "0123456789";
		
		$seed_length = 0;
		$seed        = '';
		
		if($use_upper === TRUE){
			$seed_length += 26;
			$seed .= $upper;
		}
		if($use_lower === TRUE){
			$seed_length += 26;
			$seed .= $lower;
		}
		if($use_number === TRUE){
			$seed_length += 10;
			$seed .= $number;
		}
		if(!empty($use_custom)){
			$seed_length +=strlen($use_custom);
			$seed .= $use_custom;
		}
		for($i = 1; $i <= $length; $i++){
			$password .= $seed{rand(0,$seed_length-1)};
		}
		return $password;
	} // End of generate
} // End of Class Password

// ------------------------------------------------------------------------

/**
 * With Special Characters
 **/
echo "\nWith Special Characters: \n";
echo "-------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5)) . "\n";
for ($i=10; $i <= 80; $i += 10) { 
	$password = new Password;
	echo $i . ': ' . $password->generate(array('length' => $i)) . "\n";
}

// ------------------------------------------------------------------------

/**
 * Without Special Characters
 **/
echo "\nWithout Special Characters: \n";
echo "----------------------------\n";
$password = new Password;
echo ' 5: ' . $password->generate(array('length' => 5, 'use_custom' => '')) . "\n";
for ($i=10; $i <= 80; $i += 10) { 
	$password = new Password;
	echo $i . ': ' . $password->generate(array('length' => $i, 'use_custom' => '')) . "\n";
}

// ------------------------------------------------------------------------

/**
 * SHA1 Based
 * These passwords will only contain lowercase letters from a to f and numbers from 0 to 9
 **/
$seed = sha1(uniqid(mt_rand(), true));
$hash1 = sha1(uniqid($seed . mt_rand(), true));
$hash2 = sha1(uniqid($seed . mt_rand(), true)).sha1(uniqid($hash1 . mt_rand(), true));
echo "\nSHA1 Based: \n";
echo "------------\n";
echo " 5: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 5) . "\n";
echo "10: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 10) . "\n";
echo "20: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 20) . "\n";
echo "30: " . substr(sha1(uniqid($hash1 . mt_rand(), true)), 0, 30) . "\n";
echo "40: ${hash1}\n";
echo "50: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 50) . "\n";
echo "60: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 60) . "\n";
echo "70: " . substr(sha1(uniqid($hash1 . mt_rand(), true)).sha1(uniqid($hash2 . mt_rand(), true)), 0, 70) . "\n";
echo "80: ${hash2}\n";

Initial URL

                                

Initial Description

                                

Initial Title
Random Password Generator Shell Script - PHP Class + SHA1 based

Initial Tags
class, php, script

Initial Language
PHP