MD5 ek cryptographic hash function algorithm hai jo kisi bhi length ka message input ke taur par leta hai aur usko fixed-length 16 bytes ke message me convert kar deta hai. MD5 ka full form Message-Digest Algorithm hai. Yeh algorithm 1991 me Ronald Rivest ne develop kiya tha MD4 ka improved version banane ke liye, jisme security ko aur advance kiya gaya.
MD5 ka output hamesha 128 bits ka hota hai. Aaj bhi MD5 non-cryptographic functions ke liye kaafi use hota hai, jaise ki checksum ke taur par data integrity verify karne ke liye aur bade files ko securely chhoti size me compress karne ke liye. π
-(1).jpg)
Working of the MD5 Algorithm
1. Append Padding Bits (Padding Bits Add Karna)
Is step me, original message ke end me kuch extra padding bits add ki jati hain taaki total length 512 ka exact multiple – 64 bits ho jaye.
π‘ Example:
Agar message 1000 bits ka hai, toh padding bits add karni padegi 472 bits tak ki total length 1472 bits ho jaye.
1472 bits = (512 Γ 3) – 64
(Kyuki 512 ka multiple 1536 hota hai, aur hume 64 bits kam chahiye)
Formula:
TotalΒ Length=(512Γi)β64\text{Total Length} = (512 \times i) – 64TotalΒ Length=(512Γi)β64
Jaha i = 1,2,3… hota hai.
-(2).jpg)
2. Append Length Bits (Length Bits Add Karna) π
Ab jo output Step 1 (padding bits added message) me mila tha, usme 64-bit length value bhi add karni padti hai.
π Kaise?
Output of Step 1: 512Γnβ64512 \times n – 64512Γnβ64 bits
Add Length Bits: +64+ 64+64 bits
Final Length: 512Γn512 \times n512Γn (Jo ek perfect multiple of 512 hai)
Iska matlab, ab jo data hai uski length exact 512 ka multiple ho gayi, jo MD5 algorithm ke processing ke liye zaroori hoti hai. β
3. Initialize MD Buffer (MD Buffer Initialize Karna) π
Ab 4 buffers initialize kiye jate hain, jinme har ek 32-bit ka hota hai.
Buffer | Initial Value |
---|---|
A |
|
B |
|
C |
|
D |
|
Yeh buffers hashing process ke dauraan continuously update hote hain. π
-(1).jpg)
4. Process Each 512-bit Block (Har 512-bit Block Ko Process Karna)
Yeh MD5 ka sabse important step hai, jisme 64 operations perform kiye jate hain 4 rounds me.
Breakdown of 4 Rounds:
1st Round: 16 operations β F function
2nd Round: 16 operations β G function
3rd Round: 16 operations β H function
4th Round: 16 operations β I function
Functions Used in Each Round:
Round | Function | Logic Gates Used |
---|---|---|
1st | F(B, C, D) | AND, OR |
2nd | G(B, C, D) | AND, NOT |
3rd | H(B, C, D) | XOR |
4th | I(B, C, D) | XOR, NOT |
Kaise Kaam Karta Hai?
Har function me B, C, aur D buffers ka use hota hai.
Logical operations (AND, OR, XOR, NOT) ka istemal hota hai data ko modify karne aur hash generate karne ke liye.
64 operations hone ke baad, A, B, C, D buffers update ho jate hain.
Bas ek aur step bacha hai β Final Output Generate Karna! Bataun ?
.jpg)
5. Perform Operations on Each Block
MD5 ke hashing process me har 512-bit block par kuch mathematical operations perform hote hain, jo final hash output generate karne me madad karte hain.
Operations Required:
β
Add Modulo 2322^{32}232 β Overflow handle karne ke liye.
β
M[i]M[i]M[i] β 32-bit message block.
β
K[i]K[i]K[i] β 32-bit constant value.
β
<<< n β Left shift operation (n bits ka shift).
Kaise Kaam Karta Hai? π€
1οΈβ£ B, C, D buffers ka output liya jata hai β Unpar F function lagaya jata hai.
2οΈβ£ Us output ko AAA ke saath Modulo 2322^{32}232 add kiya jata hai.
3οΈβ£ Fir M[i]M[i]M[i] (message bit) add karte hain.
4οΈβ£ Fir ek constant K[i]K[i]K[i] add hota hai.
5οΈβ£ Left shift (<<< n) operation hota hai.
6οΈβ£ Last me result ko BBB me feed kar dete hain.
π₯ Yehi steps baaki 3 functions (G, H, I) ke liye bhi follow kiye jate hain.
6. Final Output (Message Digest) π―
Jab 64 operations complete ho jate hain, to buffers (A, B, C, D) final MD5 hash output dete hain:
Lowest bits β A
Highest bits β D
C++ Implementation (Using OpenSSL)
Agar MD5 hash generate karna hai, to C++ ka OpenSSL library use kar sakte hain:
#include <iostream>
#include <iomanip>
#include <openssl/md5.h>
#include <cstring>
int main() {
std::string inputstring = "This is a message sent by a computer user.";
// Hash generate karna
unsigned char digest[MD5_DIGEST_LENGTH];
MD5((unsigned char*)inputstring.c_str(), inputstring.length(), digest);
// Output print karna
std::cout << "Hash of the input string:" << std::endl;
for(int i = 0; i < MD5_DIGEST_LENGTH; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
}
std::cout << std::endl;
return 0;
}
β Output Example:
Hash of the input string:
922547e866c89b8f677312df0ccec8ee
π Applications of MD5 Algorithm
πΉ Data Integrity Check β Files ka checksum verify karne ke liye.
πΉ Password Storage (Lekin insecure hai!) β Pehle databases me passwords store karne ke liye use hota tha.
πΉ Version Control β Alag-alag file versions manage karne ke liye.
πΉ Digital Signatures & Certificates β Pehle use hota tha, ab SHA-256 prefer kiya jata hai.
β Advantages of MD5
β Fast aur simple β Jaldi hash generate karta hai.
β 16-byte (128-bit) strong output deta hai.
β Kam memory requirement hoti hai.
β Web development me passwords hashing ke liye use hota tha (lekin ab safe nahi).
β Disadvantages of MD5
β Hash Collision β Alag inputs ke liye same hash generate ho sakta hai.
β Poor security β SHA-256 aur SHA-3 MD5 se better hai.
β Insecure β Modern cryptographic use cases ke liye recommended nahi.
β Not Symmetric or Asymmetric β Ye sirf ek one-way hash function hai.
π Modern Cryptography me MD5 Alternatives
πΉ SHA-256 β 256-bit secure hash function, Blockchain, SSL, Digital Signatures me use hota hai.
πΉ SHA-3 β Naya aur secure algorithm (SHA-2 ka alternative).
πΉ RIPEMD-160 β MD5 se better security, lekin SHA-2 jitna popular nahi.
πΉ Whirlpool β 512-bit hash function, strong security ke liye use hota hai.