I need to write a recursive function that can guess a 4 character long password. I am using this to generate a random "password":
/*start random password generating code*/
$random_password = "";
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for($i = 0; $i < 4; $i++){
$random_int = mt_rand();
$random_password .= $charset[$random_int % strlen($charset)];
}
echo "The password to guess is: " . $random_password . "\n";
/*end random password generating code*/
I can easily do the same and check if it matches against the password with a while loop but this exercise is meant to do it with a recursive function.
How would I do this? My previous exercise was just to calculate the Fibonacci number but this goes a bit past that.
Should I do it something like this:
$counter = array(1, 0, 0, 0);
function enumurate(){
global $counter;
if($counter[0] != 0){
echo $counter[0];
if($counter[1] != 0){
echo $counter[1];
if($counter[2] != 0){
echo $counter[2];
if($counter[3] != 0){
echo $counter[3];
}else{
echo 'error! $counter[3] is: ' . $counter[3];
}
}else{
echo 'error! $counter[2] is: ' . $counter[2];
}
}else{
echo 'error! $counter[1] is: ' . $counter[1];
}
}else{
echo 'error! $counter[0] is: ' . $counter[0];
}
}
enumurate();
I believe the thing I am looking for is something among bit shifting(after I have iterated 62 times[26 + 26 + 10= lowercase + UPPERCASE + numerical]) and then recursively call the function again but I am at a loss.
Or am I overthinking this?
PS: I did look for it on google but for some reason I can't find any on the specific of recursive function matching against a string enumeration as well as checking on here. My 1337 searching skills may have failed me though.