Python Hash Generator

Generate MD5, SHA-1, SHA-256, SHA-384, SHA-512 hashes online — with Python hashlib examples and tips.

🔒 100% private — runs entirely in your browser
or try sample data

Hashing in Python with hashlib

Python's hashlib module provides a common interface for many secure hash algorithms including MD5, SHA-1, SHA-256, SHA-384, and SHA-512. It's part of the standard library — no pip install required.

  • hashlib.md5() — fast 128-bit hash, not collision-resistant. Use only for checksums, not security.
  • hashlib.sha256() — 256-bit hash, the standard choice for integrity checks and cryptographic applications.
  • hashlib.sha512() — 512-bit hash for maximum security requirements.
  • hmac module — for keyed-hash message authentication codes (HMAC) using any hashlib algorithm.
  • File hashing — read in chunks with update() to hash files of any size without loading them into memory.

Python Hashing Code Examples

MD5 Hash of a String

import hashlib text = "Hello, World!"
hash_md5 = hashlib.md5(text.encode("utf-8")).hexdigest()
print(hash_md5) # 65a8e27d8879283831b664bd8b7f0ad4

SHA-256 Hash of a String

import hashlib text = "Hello, World!"
hash_sha256 = hashlib.sha256(text.encode("utf-8")).hexdigest()
print(hash_sha256) # dffd6021bb2bd5b0af676290809ec3a5...

Hash a File in Chunks

import hashlib def hash_file(filepath, algorithm="sha256"): h = hashlib.new(algorithm) with open(filepath, "rb") as f: for chunk in iter(lambda: f.read(8192), b""): h.update(chunk) return h.hexdigest() print(hash_file("myfile.zip"))

HMAC Authentication

import hmac
import hashlib secret = b"my-secret-key"
message = b"important data" signature = hmac.new(secret, message, hashlib.sha256).hexdigest()
print(signature) # keyed hash for message authentication

Python Hashing Gotchas

hashlib requires bytes, not strings

Just like Base64, hashlib functions expect bytes. Always call .encode('utf-8') on your string: hashlib.sha256(text.encode('utf-8')).

MD5 is not secure for passwords

MD5 is cryptographically broken — collisions can be generated in seconds. Use bcrypt, argon2, or hashlib.pbkdf2_hmac() for password hashing. MD5 is still fine for checksums and non-security use cases.

hexdigest() vs digest()

.hexdigest() returns a hex string (e.g., '65a8e27d...'). .digest() returns raw bytes. Use hexdigest() for display and comparison; use digest() when feeding into another cryptographic operation.

Hash comparison must be constant-time

When verifying hashes, use hmac.compare_digest(a, b) instead of ==. The == operator is vulnerable to timing attacks that can leak the expected hash value byte by byte.

Should You Land Here First?

You should land here first if your task already names this workflow. The examples, defaults, and answers on Python Hash Generator assume that context.

If your task is more general or you have not narrowed down the workflow yet, start in the Developer Utility Tools and let it route you to the right page.

Frequently Asked Questions

How do I generate an MD5 hash in Python?

Use hashlib.md5(string.encode('utf-8')).hexdigest(). This encodes the string to bytes, computes the MD5 hash, and returns the 32-character hexadecimal digest.

What is the difference between MD5 and SHA-256?

MD5 produces a 128-bit (32 hex chars) hash and is fast but cryptographically broken — collisions exist. SHA-256 produces a 256-bit (64 hex chars) hash and remains secure for cryptographic use. Use SHA-256 for security; MD5 is fine for checksums.

How do I hash a file in Python?

Open the file in binary mode and read in chunks: create a hasher with hashlib.sha256(), loop with f.read(8192), call h.update(chunk), then h.hexdigest(). This handles files of any size without loading them fully into memory.

How do I hash passwords in Python?

Don't use MD5 or SHA-256 directly for passwords. Use bcrypt, argon2-cffi, or hashlib.pbkdf2_hmac('sha256', password, salt, iterations) which adds salt and key stretching to resist brute-force attacks.

Is this tool safe for hashing sensitive data?

Yes. All hashing happens entirely in your browser using client-side JavaScript and the Web Crypto API. Your data is never sent to any server.