Open In App

MD5 hash in Python

Last Updated : 09 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MD5 is a cryptographic hash function that produces a 128-bit hash value, usually shown as a 32-character hexadecimal string. While it was commonly used for tasks like data integrity checks, MD5 is now considered insecure due to collision vulnerabilities. Despite this, it remains useful for non-sensitive tasks, such as file fingerprinting and data verification, where security is not important.

Key methods in hashlib for MD5

Here are the key functions in the hashlib module when working with MD5:

  • encode() converts a string to a byte sequence, which is required by the MD5 hash function.
  • digest() returns the raw binary representation of the MD5 hash. It’s mainly used for cryptographic purposes and is not human-readable.
  • hexdigest() returns the MD5 hash in a readable hexadecimal format, which is the most common representation.

How to Generate an MD5 Hash in Python

Python's built-in hashlib library makes it easy to generate MD5 hashes. You can hash both raw bytes and strings, depending on your use case. Here's how to do both:

1. MD5 Hash from Bytes: You can directly pass bytes to the MD5 function. For example:

Python
import hashlib

res = hashlib.md5(b'GeeksforGeeks')
print(res.digest())

Output
b'\xf1\xe0ix~\xcetS\x1d\x11%Y\x94\\hq'

Explanation:

  • input (b'GeeksforGeeks') is a byte sequence.
  • digest() outputs the MD5 hash in raw bytes format.

2. MD5 Hash from String: Since MD5 accepts only byte sequences, you must first encode strings into bytes using the encode() method.

Python
import hashlib

s = "GeeksforGeeks"
res = hashlib.md5(s.encode())
print(res.hexdigest())

Output
f1e069787ece74531d112559945c6871

Explanation:

  • string "GeeksforGeeks" is first encoded to bytes using encode().
  • hexdigest() outputs the MD5 hash as a human-readable hexadecimal string.

Common Use Cases of MD5

Though MD5 is no longer secure for cryptographic tasks, it’s still useful for:

  • Data Integrity verifying files or data during transfer by comparing hashes.
  • File Fingerprinting identifying duplicate files based on identical MD5 hashes.
  • Quick Checksums fast hash generation for detecting file changes.

Limitations of MD5

Although MD5 is still widely used in some applications, it is no longer considered secure for cryptographic purposes. Here's why:

  • Collision Vulnerability: Different inputs can produce the same hash.
  • Fast but Risky: Its speed makes it prone to brute-force attacks.

Alternatives to MD5

For secure applications, stronger hash functions are recommended:

  • SHA-256: A secure, widely-used hash function resistant to collisions.
  • bcrypt: Ideal for password hashing, with built-in salting and slow computation.
  • argon2: A modern, memory-hard algorithm designed to resist brute-force and GPU attacks.

Next Article
Article Tags :
Practice Tags :

Similar Reads