-
-
Save rbhushan90/eb7d4ad6045caad9025d0b8da27579c8 to your computer and use it in GitHub Desktop.
PHP – Generate/Create strong passwords, uuid, random string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The code is from http://www.mcgarvie.net/2009/07/17/programming/php/php-generatecreate-strong-passwords-uuid-random-string made by Brian M McGarvie/ | |
/** | |
Perfect for: PASSWORD GENERATION | |
Options: variable length | |
generateRandStr(8) | |
result: 4sRBfahW | |
**/ | |
<?php | |
function generateRandStr_md5 ($length) { | |
// Perfect for: PASSWORD GENERATION | |
// Generate a random string based on an md5 hash | |
$randStr = strtoupper(md5(rand(0, 1000000))); // Create md5 hash | |
$rand_start = rand(5,strlen($randStr)); // Get random start point | |
if($rand_start+$length > strlen($randStr)) { | |
$rand_start -= $length; // make sure it will always be $length long | |
} if($rand_start == strlen($randStr)) { | |
$rand_start = 1; // otherwise start at beginning! | |
} | |
// Extract the 'random string' of the required length | |
$randStr = strtoupper(substr(md5($randStr), $rand_start, $length)); | |
return $randStr; // Return the string | |
} | |
function generateRand_uuid ( $prefix = 'W' ) { | |
// Perfect for: UNIQUE ID GENERATION | |
// Create a UUID made of: PREFIX:TIMESTAMP:UUID | |
$my_random_id = $prefix; | |
$my_random_id .= chr ( rand ( 65, 90 ) ); | |
$my_random_id .= time (); | |
$my_random_id .= uniqid ( $prefix ); | |
return $my_random_id; | |
} | |
function generateRand_uuid_l ( $prefix = 'W', $length=0 ) { | |
// Perfect for: UNIQUE ID GENERATION | |
// Create a UUID made of: PREFIX:TIMESTAMP:UUID(PART - LENGTH - or FULL) | |
$my_random_id = $prefix; | |
$my_random_id .= chr ( rand ( 65, 90 ) ); | |
$my_random_id .= time (); | |
$my_uniqid = uniqid ( $prefix ); | |
if($length > 0) { | |
$my_random_id .= substr($my_uniqid, $length); | |
} else { | |
$my_random_id .= $my_uniqid; | |
} | |
return $my_random_id; | |
} | |
function generateRand_md5uid(){ | |
// Perfect for: UNIQUE ID GENERATION | |
// Create a really STRONG UUID | |
// Very high odds of creating a unique string 1:1000000+ | |
$better_token = md5(uniqid(rand(), true)); | |
$unique_code = substr($better_token, 64); | |
$uniqueid = $unique_code; | |
return $better_token; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment