The perfect Multithreaded programming solution

Source: Internet
Author: User

This is the last year in order to find a job, wrote a technical demonstration: a multithreaded violence to crack MD5 password, as an example, to demonstrate a perfect multi-threaded programming program.

Ten years ago, I wrote a single-threaded brute force hack MD5 password program (because the CPU was still single-core), this time the original program multithreading.
This technical demonstration, which wrote several versions of several different implementations, took 2 days (May 3, 2014 – May 4, 2014).

The principle of brute force for encrypting passwords is not the focus of this article, and is not described in detail here.
For a simple example, for an attempt to set a A-Z 26 lowercase letters, try a length of 8 bits, that is, A, B, ... z, AA, AB, ... zy, ZZ, aaa, ... aaaaaaaa, Aaaaaaab, ... zzzzzzzy, ZZ zzzzzz, these 26 lowercase letters, within 8 bits of length, all combinations are tried once, so called brute force. The result of the attempt, if the original encryption password within this range, will inevitably find. If the original encryption password is not within this range, then all the possible attempts in that range are not found. The way to try is to each of the attempts to generate a string, and encrypt the password encryption method of the same encryption, and then the encrypted attempt string, and encryption password comparison, the same is found.

This brute force hack requires an ultra-large amount of CPU computation. It is a very important application scenario for multi-threaded technology that the CPU is required to perform super-large computation.
We use examples to form some concepts of brute force calculation:
Try it with a combination of 26 lowercase letters:
After trying out the individual 1-bit, you need to calculate and compare the 26 attempt strings (A-Z).
After testing the individual 2-bit, we need to calculate and compare the 26+26*26=702 type of experiment string.
After testing the individual 8-bit, it is necessary to calculate and compare 26 of the 8-square-+...+26=2171.80147158 billion-million-dollar-a-side attempt string.
Using four-core @3.4ghz CPU, with four threads (the number of threads exceeds the actual number of CPUs, the speed will not increase) run out of 217.18 billion possible, estimated to 4 hours. and a single-threaded run of 217.18 billion possible, estimated to be more than 16 hours.
If you are interested, you can download the program that accompanies this article and try it yourself.
You can see how it makes sense to use multithreaded technology in scenarios where the CPU is required to perform super-large computations.

Multi-threaded common access and processing of the same data source, a very important problem is that access requires mutual exclusion, otherwise, may cause the omission of processing data, or repeated processing of data. A small amount of repetitive processing data is acceptable, but the omission of processing data is unacceptable. Why there are duplicates and omissions, we do not unfold here to speak. Therefore, multi-threaded common access and processing of the same data source, access must be mutually exclusive.

Let's take a look at some of the implementation scenarios I've tried.

Multithreading Scenario 1:
Try a string of only 1. Multiple threads take turns and then perform calculations and comparisons.
In this case, the continuity of each thread's attempt is good. However, when a thread is generating an attempt string, another thread cannot access the attempt string, and the thread needs to block the wait. This is expected to happen frequently and can affect speed.

Program implementation,
If the mutex is used for thread mutex, the impact on speed is enormous, because the implementation of part of the mutex code, four thread version even more than a single-threaded version is much slower. ("Ddzzzz" the MD5 of the crack. Four threads use mutex mutex, 118984ms. The single thread is approximately 13500ms. Four threads are more than 8 times times slower than a single thread)
If you use InterlockedIncrement64, the restrictions on the program are large.

Multithreading Scenario 2:
Divide the task into roughly equal n parts (n is the number of cores of the CPU) and then create n threads, which each thread completes 1 copies.
There is no mutex problem.

Logical clarity is poor, and in the future if used in different scenarios, the code is more difficult to modify.

Not implemented as a program.

Multithreading Scenario 3:
Divide the task into a fixed number of copies, then create n threads (n is the number of cores of the CPU), make 1 copies per thread, and after that, take 1 copies until all the copies are done.

Within the thread, you need to complete the code to receive a copy. To receive a part requires mutual exclusion. The mutex is rarely seen in the frequency.

Not implemented as a program.

Multithreading Scenario 4:
Creates n threads (n is the number of cores of the CPU).
Each thread processes a portion of the task from the total task, which is processed and then taken. Until all the tasks are processed. The advantage is that the distribution is more evenly distributed. Continuity is also better.

Program implementation,
"Ddzzzz" of the MD5 hack, 3 thread 4641ms. Works best.

Why multithreading Scenario 4, is a more perfect multithreaded programming scenario:
1, the execution efficiency is very high in a scenario where there is a high demand for execution efficiency.
2, code logic is very good, applicability is very wide. Few changes can be applied in many applications.

The procedure (HTTP://PAN.BAIDU.COM/S/1I3IP4NB) that accompanies this article explains:
MD5Calc.exe the graphical interface to convert the specified plaintext password to a MD5 encrypted password.
JiurlMd5CrackGui.exe graphical interface, multithreaded brute force cracking demo program to this article multithreading scenario 4 implementation. You can set the number of threads you use, with the default number of threads minus 1 for the CPU cores.
TryCharset.txt Specifies the attempt character set.
CryptedPassword.txt Specifies the password to be cracked.

Temporary homepage: HTTP://BLOG.SINA.COM.CN/DDQQPPB
QQ Tall Upper Exchange Group: 91877299

Perfect multi-threaded programming solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.