I made a multi-threaded program in C++ to break passwords of 7 characters long (lower case characters only) using a brute-force algorithm.
My algorithm is mostly 7 nested for-loops going from a to z and testing every possible combination.
Right now, I’m dividing my work this way :
If I have 3 working threads,
Thread 1 : axxxxxx to ixxxxxx
Thread 2 : jxxxxxx to rxxxxxx
Thread 3 : sxxxxxx to zxxxxxx
So the 3 threads will go on and loop until they find a match.
The main thread will wait for the first thread to return.
My question is : Is this the best way to divide the work between my threads? Do you have any idea on how I could be more efficient?
Also, even if it’s not the main part of my interrogation, can you think of a better way than the 7 for-loop iteration?
(Please note that this program is for a school project and not for really cracking passwords)
If all keys are equally likely, and if the cost to evaluate a key is the same for every key, and if each thread can expect to be assigned to one CPU without very many interruptions (e.g. your process is the only CPU intensive one running), evenly partitioning the keyspace as you have done will be very efficient.
If some of those assumptions are invalid, a more flexible way to structure the program would be to have one thread (producer thread) hand out key ranges to 1 or more consumer threads for processing. Once a given thread completes its chunk of work, it would go back to the producer and request a new key range to analyze.
There’s some overhead in the producer/consumer pattern, but it is more flexible.