Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3 from Yendrzei/trunk

generated password - at least one char from each group
  • Loading branch information...
commit b51bf1bf87e2651efec7a791b41b646ad7f3ae69 2 parents 55417dd + d140134
@CloCkWeRX CloCkWeRX authored
Showing with 47 additions and 35 deletions.
  1. +47 −35 Text/Password.php
View
82 Text/Password.php
@@ -484,45 +484,57 @@ function _createUnpronounceable($length, $chars)
/**
* List of character which could be use in the password
*/
- switch($chars) {
-
- case 'alphanumeric':
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 62;
- break;
-
- case 'alphabetical':
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
- $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 52;
- break;
-
- case 'numeric':
- $chars = '0123456789';
- $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 10;
- break;
-
- case '':
- $chars = '_#@%&ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 67;
- break;
-
- default:
- /**
- * Some characters shouldn't be used
- */
- $chars = trim($chars);
- $chars = str_replace(array('+', '|', '$', '^', '/', '\\', ','), '', $chars);
-
- $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = strlen($chars);
- }
+ $lower = 'abcdefghijklmnopqrstuvwxyz';
+ $upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+ $decimal = '0123456789';
+ $special = '_#@%&';
+
+ switch($chars) {
+
+ case 'alphanumeric':
+ $chars = array($lower, $upper, $decimal);
+ break;
+
+ case 'alphabetical':
+ $chars = array($lower, $upper);
+ break;
+
+ case 'numeric':
+ $chars = array($decimal);
+ break;
+
+ case '':
+ $chars = array($lower, $upper, $decimal, $special);
+ break;
+
+ default:
+ /**
+ * Some characters shouldn't be used
+ */
+ $chars = array(trim($chars));
+ $chars = str_replace(array('+', '|', '$', '^', '/', '\\', ','), '', $chars);
+ }
+
+ $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 0;
+ foreach ($chars as $charsItem) {
+ $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] += strlen($charsItem);
+ }
/**
* Generate password
*/
- for ($i = 0; $i < $length; $i++) {
- $num = mt_rand(0, $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] - 1);
- $password .= $chars{$num};
- }
+ while (strlen($password) < $length) {
+ foreach ($chars as $k => $v) {
+ $random_char = $v[rand(0, strlen($v) - 1)];
+ $random_position = rand(0, strlen($password) - 1);
+ $password = substr($password, 0, $random_position)
+ . $random_char
+ . substr($password, $random_position);
+ }
+ }
+ if (strlen($password) > $length) {
+ $password = substr($password, 0, $length);
+ }
/**
* Return password
Please sign in to comment.
Something went wrong with that request. Please try again.