Walkero

software engineer, Drupal follower, Docker ninja, JS explorer, PHP believer, exotic Hardware beta tester, Amiga enthusiast, truth seeker, parent & husband... at least for now...

Today I had to create a script to create a large amount of unique passwords for a project. To be more specific, we needed more than 200000 passwords. So, I created a nice php script that does the job. These passwords were created in about 33mins on my desktop computer. Do you think it is slow? It would be great if you could provide any faster solution?

The script has two stages of duplication check, with the second one being optional. The first one is performed as soon as a new password is created. The second check happens at the end of the password generation, using the array_unique function of php.

The script outputs the password list as a .csv file and inside the browser. You can select which one you want to use from the parameters array.

You can configure the script at the start of it. I use a preferences array where you can set the way you want the script to run. Follows an explanation of the preferneces and how to use them.

  • numofchars: the maximum number of characters the password will have
  • numofcodes: the number of different passwords to be generated
  • upper: use uppercase letters
  • lower: use lowercase letters
  • nums: use numbers
  • duplicatecheck: use second stage check for duplicated passwords
  • showtime: show the time that needed to complete the job as ms
  • exporttofile: export the result codes in a .csv file. This file will be created at the same folder where the script exists. It needs the folder to be writeable
  • printcodes: print the passwords at the screen
<?php
/*************************************************************
**   PHP Password generator 1.0
**   Created by George Sokianos (2012/03/28)
**
**/

error_reporting(E_ALL);
ini_set('display_errors','On');
ini_set('max_execution_time', 3000);

$parameters = array(
    'numofchars' => 8,
    'numofcodes' => 3000000,
    'upper' => true,
    'lower' => false,
    'nums' => true,
    'duplicatecheck' => false,
    'showtime' => true,
    'exporttofile' => true,
    'printcodes' => false
);

function getmicrotime() 
{ 
   list($usec, $sec) = explode(" ", microtime()); 
   return ((float)$usec + (float)$sec); 
}

function RandomPass( $params ) 
{
    $NameChars = '';
    if($params['lower']) {
        $NameChars .= 'abcdefghijklmnopqrstuvwxyz';
    }

    if($params['upper']) {
        $NameChars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    }

    if($params['nums']) {
        $NameChars .= '123456789';
    }

    $Pass = "";
    $chars_count = strlen($NameChars);
    for ($index = 1; $index <=  $params['numofchars']; $index++) { 
        $Pass .= substr($NameChars, mt_rand(0, $chars_count-1), 1);
    }
    return $Pass;
}   

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

if($parameters['showtime']) {
    $time_start = getmicrotime();
}

$passes = array();
for($i=0; $i<$parameters['numofcodes']; $i++) {
    do {
        $a = RandomPass($parameters);
    } while(in_array($a, $passes));
    array_push($passes, $a);
}

if($parameters['printcodes']) {
    foreach($passes AS $curpass) {
        echo $curpass;
        echo '<br/>';   
    }
}

if($parameters['exporttofile']) {
    $filename = 'files/passes_' . date('YmdHis') . '.csv';
    $fh = fopen($filename, "w");
    if($fh==false)
        die("unable to create file");

    foreach($passes AS $curpass) {
        fputcsv($fh, array($curpass));
    }

    fclose ($fh);
}

echo '<br /><br />';

if($parameters['duplicatecheck']) {
    if(count($passes) == count(array_unique($passes))) {
        echo 'No duplicates detected';
    } else {
        echo 'DUPLICATES FOUND';
    }
}

if($parameters['showtime']) {
    $time_end = getmicrotime();
    $time_passed = $time_end - $time_start;
    echo "<hr />Execution time: " . $time_passed;
}

flush();

?>

Do not forget to include the code into php tags in your php file.

#passwords #generator #php #unique #script
- 2 min read