Advertisement
Guest User

generatePassword.php

a guest
Jun 9th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. <?php
  2. function generatePassword($l = 8, $c = 0, $n = 0, $s = 0) {
  3.     // get count of all required minimum special chars
  4.     $count = $c + $n + $s;
  5.    
  6.     // sanitize inputs; should be self-explanatory
  7.     if(!is_int($l) || !is_int($c) || !is_int($n) || !is_int($s)) {
  8.         trigger_error('Argument(s) not an integer', E_USER_WARNING);
  9.         return false;
  10.     }
  11.     elseif($l < 0 || $l > 20 || $c < 0 || $n < 0 || $s < 0) {
  12.         trigger_error('Argument(s) out of range', E_USER_WARNING);
  13.         return false;
  14.     }
  15.     elseif($c > $l) {
  16.         trigger_error('Number of password capitals required exceeds password length', E_USER_WARNING);
  17.         return false;
  18.     }
  19.     elseif($n > $l) {
  20.         trigger_error('Number of password numerals exceeds password length', E_USER_WARNING);
  21.         return false;
  22.     }
  23.     elseif($s > $l) {
  24.         trigger_error('Number of password capitals exceeds password length', E_USER_WARNING);
  25.         return false;
  26.     }
  27.     elseif($count > $l) {
  28.         trigger_error('Number of password special characters exceeds specified password length', E_USER_WARNING);
  29.         return false;
  30.     }
  31.  
  32.     // all inputs clean, proceed to build password
  33.    
  34.     // change these strings if you want to include or exclude possible password characters
  35.     $chars = "abcdefghijklmnopqrstuvwxyz";
  36.     $caps = strtoupper($chars);
  37.     $nums = "0123456789";
  38.     $syms = "!@#$%^&*()-+?";
  39.    
  40.     // build the base password of all lower-case letters
  41.     for($i = 0; $i < $l; $i++) {
  42.         $out .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  43.     }
  44.    
  45.     // create arrays if special character(s) required
  46.     if($count) {
  47.         // split base password to array; create special chars array
  48.         $tmp1 = str_split($out);
  49.         $tmp2 = array();
  50.        
  51.         // add required special character(s) to second array
  52.         for($i = 0; $i < $c; $i++) {
  53.             array_push($tmp2, substr($caps, mt_rand(0, strlen($caps) - 1), 1));
  54.         }
  55.         for($i = 0; $i < $n; $i++) {
  56.             array_push($tmp2, substr($nums, mt_rand(0, strlen($nums) - 1), 1));
  57.         }
  58.         for($i = 0; $i < $s; $i++) {
  59.             array_push($tmp2, substr($syms, mt_rand(0, strlen($syms) - 1), 1));
  60.         }
  61.        
  62.         // hack off a chunk of the base password array that's as big as the special chars array
  63.         $tmp1 = array_slice($tmp1, 0, $l - $count);
  64.         // merge special character(s) array with base password array
  65.         $tmp1 = array_merge($tmp1, $tmp2);
  66.         // mix the characters up
  67.         shuffle($tmp1);
  68.         // convert to string for output
  69.         $out = implode('', $tmp1);
  70.     }
  71.    
  72.     return $out;
  73. }
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement