> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/hashing/07.-md5.md).

# 07. MD5

## MD5 (Message Digest 5)

### 1. What MD5 Actually Is

MD5 stands for **Message Digest 5**.

It is a **cryptographic hash function**, not encryption.

It takes any input (text, file, password) and produces a fixed-size output.

* Output size: **128-bit**
* Displayed as: **32 hexadecimal characters**

Example:

```
Input  → hello  
MD5    → 5d41402abc4b2a76b9719d911017c592
```

### 2. Important Clarification

#### MD5 is NOT encryption

| Concept          | MD5 |
| ---------------- | --- |
| Encrypts data    | No  |
| Decrypts data    | No  |
| Reversible       | No  |
| One-way function | Yes |

There is no decryption process. MD5 cannot be reversed.

### 3. How MD5 Works (Simple View)

Internally MD5 processes data like this:

#### Step 1: Padding

Input is padded to fit 512-bit blocks.

#### Step 2: Block splitting

Data is split into 512-bit chunks.

#### Step 3: Initialization

Four internal buffers are used:

```
A, B, C, D
```

#### Step 4: Compression

Each block goes through:

* bitwise operations
* modular addition
* logical functions (AND, OR, XOR, NOT)

#### Step 5: Output

Final A, B, C, D → combined into 128-bit hash.

### 4. Why MD5 Cannot Be Reversed

MD5 destroys original structure:

* Infinite inputs → fixed output space
* Multiple inputs can map to same hash

So:

```
"hello" → hash  
"helloo" → completely different hash
```

But:

```
hash → original input ❌ impossible
```

## 5. MD5 Practical Usage (Real World + CTF)

***

### 5.1 Generate MD5

#### Linux

```bash
echo -n "hello" | md5sum
```

#### Python

```python
import hashlib

print(hashlib.md5("hello".encode()).hexdigest())
```

### 5.2 File integrity check

Before:

```
file → hash published
```

After download:

```
file → hash → compare
```

If mismatch → file modified.

### 5.3 Password storage (old systems)

```
password → MD5 → stored in DB
```

Now considered insecure.

## 6. MD5 “Cracking” (Important Practical Section)

MD5 cannot be decrypted. Instead, we **recover input using guessing techniques**.

***

### 6.1 Online lookup (fast method)

* Paste hash into online MD5 databases
* Works for common passwords

### 6.2 Dictionary attack (John the Ripper)

```bash
echo "5d41402abc4b2a76b9719d911017c592" > hash.txt
```

Run:

```bash
john --format=raw-md5 hash.txt --wordlist=/usr/share/wordlists/rockyou.txt
```

Show result:

```bash
john --show hash.txt
```

### 6.3 GPU cracking (Hashcat)

```bash
hashcat -m 0 hash.txt rockyou.txt
```

#### Meaning:

* `-m 0` → MD5 mode
* `hash.txt` → target hash
* `rockyou.txt` → wordlist

### 6.4 Brute force (Python)

For small input spaces:

```python
import hashlib
import itertools
import string

target = "5d41402abc4b2a76b9719d911017c592"
chars = string.ascii_lowercase

for length in range(1, 6):
    for attempt in itertools.product(chars, repeat=length):
        guess = ''.join(attempt)
        if hashlib.md5(guess.encode()).hexdigest() == target:
            print("Found:", guess)
            exit()
```

### 6.5 Why cracking works

MD5 is weak because:

* Extremely fast → many guesses per second
* No built-in protection (salt in old systems missing)
* Precomputed databases exist (rainbow tables)
* Small password space in real life

### 7. Why MD5 is Insecure

#### 1. Collision attacks

Different inputs can produce same hash:

```
m1 ≠ m2  
MD5(m1) = MD5(m2)
```

#### 2. Speed problem

Attackers can brute force billions of hashes quickly.

#### 3. Rainbow tables

Precomputed hash → password mappings.

#### 4. No protection by default

Without salt:

* same password = same hash

### 8. CTF Strategy (Very Important)

When you see MD5 in a challenge:

#### Step 1: Identify

* 32 hex characters → MD5 likely

#### Step 2: Try lookup

* check online hash databases

#### Step 3: Dictionary attack

* use John or Hashcat

#### Step 4: Brute force

* only if input space is small

#### Step 5: Check tricks

* double hashing
* encoding layers
* salt added

### 9. Real-World Status

MD5 is:

* Broken for security
* Deprecated for cryptographic use
* Still used for:
  * file checksums
  * legacy systems
  * CTF challenges

### 10. Final Summary

MD5 is:

* A one-way hash function
* Not reversible
* Extremely fast
* Cryptographically broken

#### Core idea:

You do not decrypt MD5. You **guess the input until the hash matches**.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/hashing/07.-md5.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
