CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Post a PHP snippet (http://www.codingforums.com/forumdisplay.php?f=41)
-   -   Password, Salt, any length Random string Generator (http://www.codingforums.com/showthread.php?t=270020)

Arcticwarrio 08-08-2012 09:20 PM

Password, Salt, any length Random string Generator
 
i made this for adding salt to a password also for temporary passwords or pin numbers, could also be used for creating password reset keys i was that impressed with it i decided to share it with you all.

unique string chars or duplicated,
upper case, lowercase + numbers
Upper case + numbers
Numbers only

feel free to message me if you need help modifying it for your own needs
could easily be altered to generate lottery numbers etc
added lottery numbers generator

function:
PHP Code:

function GenCode($len,$type 1,$uni 0){
    
    
//GenCode(length of string,$type = 1, 2 or 3,unique chars?)
    //Unique Switch 1 for on 0 or ommited for off
    //Type Switch 1 = a-z, A-Z, 0-9     Usage: GenCode(8); or GenCode(8,1);
    //Type Switch 2 = A-Z, 0-9          Usage: GenCode(10,2);
    //Type Switch 3 = 0-9               Usage: GenCode(4,3,1);
    //Type Switch 4 = 0-49               Usage: GenCode(6,4,1); <-- Lottery Numbers

    //Reset Function
    
$code ='';
    
//Array of chars to choose from, feel free to add more arrays
    
$codestring['1'] = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0');
    
$codestring['2'] = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0');
    
$codestring['3'] = array('1','2','3','4','5','6','7','8','9','0');
    
$codestring['4'] = array('|1|','|2|','|3|','|4|','|5|','|6|','|7|','|8|','|9|','|10|','|11|','|12|','|13|','|14|','|15|','|16|','|17|','|18|','|19|','|20|','|21|','|22|','|23|','|24|','|25|','|26|','|27|','|28|','|29|','|30|','|31|','|32|','|33|','|34|','|35|','|36|','|37|','|38|','|39|','|40|','|41|','|42|','|43|','|44|','|45|','|46|','|47|','|48|','|49|');
    
    
//see if the string should contain unique chars
    
if ($uni == 1){
        
//check if theres enough chars to have a unique string
        
if ($len count($codestring[$type])-1){ echo "error length too long for unique string"; exit;}
        
//loop for lenght of $len NOT allowing duplicate chars
        
for ($i=1$i<=$len$i++){
            
//add char from chosen array to the end of the variable $code
            
$tempx '"'.$codestring[$type][rand(0,count($codestring[$type])-1)].'"';
            if (
strstr($code$tempx) != false){
                
$i--;
            }else{
                
$code .= $tempx;
            }
        }
 
    }else{
        
//loop for lenght of $len allowing duplicate chars
        
for ($i=1$i<=$len$i++){
            
//add char from chosen array to the end of the variable $code
            
$code .= $codestring[$type][rand(0,count($codestring[$type])-1)];
        }
    }
    
//clean up the code
    
$code str_replace('"','',$code);
    
$code str_replace('|',' ',$code);
        
//send $code back to where GenCode() was called from
    
return $code;




All times are GMT +1. The time now is 11:36 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.