Created
September 20, 2018 13:00
-
-
Save HendrikEduard/9907957fe6e129d3e48af02f622de9aa to your computer and use it in GitHub Desktop.
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
<?php | |
function random_passwords($length,$count,$characters) { | |
// $length - the length of the generated password(s) | |
// $count - number of passwords to be generated | |
// $characters - types of characters to be used in the password | |
// define variables used within the function | |
$symbols = []; | |
$passwords = []; | |
$use_chars = ''; | |
// an array of different character types | |
$chars["lower_case"] = 'abcdefghijklmnopqrstuvwxyz'; | |
$chars["upper_case"] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$chars["numbers"] = '1234567890'; | |
$chars["special_symbols"] = '!?~@#-_$+='; | |
$characters = explode(",",$characters); // get characters types to be used for the passsword | |
foreach ($characters as $key=>$value) { | |
$use_chars .= $chars[$value]; // build a string with all characters | |
} | |
$chars_length = strlen($use_chars) - 1; // strlen starts from 0 to get number of characters deduct 1 | |
for ($p = 0; $p < $count; $p++) { | |
$pass = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$n = rand(0, $chars_length); // get a random character from the string with all characters | |
$pass .= $use_chars[$n]; // add the character to the password string | |
} | |
$passwords[] = $pass; | |
} | |
return $passwords; // return the generated passwords | |
} | |
// usage | |
$passwords = random_passwords(32,5,"lower_case,upper_case,numbers,special_symbols"); | |
foreach($passwords as $pass) { | |
echo $pass, "\n"; | |
} |
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
Copyright 2018 Hendrik Eduard Kuiper | |
Permission is hereby granted, free of charge, to any person obtaining a | |
copy of this software and associated documentation files (the "Software"), | |
to deal in the Software without restriction, including without limitation | |
the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
and/or sell copies of the Software, and to permit persons to whom the | |
Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED | |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't generate passwords/tokens using
rand()
, they're insecure and predictable in a way.rand()
is not a cryptographically secure pseudo random number generator (CSPRNG). Here's a piece about cracking rand() seeds and once cracked you can generate same passwords.Since PHP 7.1 rand() is an alias to
mt_rand()
so here'smt_rand()
seed cracker too.You need to use cryptographically secure random numbers so they're not predictable. You can generate them using
random_bytes()
orrandom_int()
.Don't use
rand()
,mt_rand()
,uniqid()
,time()
etc. for generating tokens or passwords.