0

I'm playing about creating a database that contains plaintext and hash values for any combination of passwords, however when they are added to the database I end up with many duplicates before it moves on to the next combination... Any ideas as to why and how to stop it?

<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$numberof=0;
$alphabet = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");

for($i=0; $i<count($alphabet); $i++){
    for($j=0; $j<count($alphabet); $j++){
        for($k=0; $k<count($alphabet); $k++){
            for($l=0; $l<count($alphabet); $l++){
                $final = $alphabet[$i].$alphabet[$j].$alphabet[$k];
                $hash = md5($final);

                mysqli_query($con,"INSERT INTO hashes (plain, hash) VALUES ('$final', '$hash')");
            }
        }
    }
}
echo $numberof;
?>
4
  • 4
    You are not adding the $alphabet[$l] to your $final string. Commented Jul 25, 2013 at 12:04
  • Just as an information : Because md5 generates a string of a fixed length duplicate hashes are possible Commented Jul 25, 2013 at 12:05
  • Of course! They're not duplicates, they're just missing the 4th letter! Duuuuh!! :P Thanks, Hidde :) Commented Jul 25, 2013 at 12:05
  • To create $alphabet array you can use range. Like range('A','Z'); Commented Jul 25, 2013 at 12:38

2 Answers 2

1

You have four nested for loops, with counters $i $j $k and $l. However, your final string that you use in your query only uses $i $j and $k So you'll have about 26 duplicates. I assume you meant to append the value of $l to the end of your string?

Sign up to request clarification or add additional context in comments.

2 Comments

Second problem, after 30 seconds of generation it has an error, I'm guessing because MySQL needs to finish whatever within 30 seconds... any way around that?
For PHP itself, you can use ini_set('max_execution_time', ###);, but there may be other limitations put in place on the server that you're hosting this on.
0

just remove the last loop. i have update your code as below:

for($i=0; $i<count($alphabet); $i++){
    for($j=0; $j<count($alphabet); $j++){
        for($k=0; $k<count($alphabet); $k++){
            $final = $alphabet[$i].$alphabet[$j].$alphabet[$k];
            $hash = md5($final);
            mysqli_query($con,"INSERT INTO hashes (plain, hash) VALUES ('$final', '$hash')");           
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.