DevToolsForYou

Hash Generator in Python — Code Examples

Hash Generator in PythonUse the online tool →

Cryptographic hash functions convert input data into a fixed-size digest. SHA-256 is the current standard for security-sensitive use cases; MD5 is broken for security but still used for checksums.

Python's hashlib module supports MD5, SHA-1, SHA-256, SHA-512, and more with a consistent API.

Python 3
import hashlib

text = "Hello, world!"

# SHA-256
sha256 = hashlib.sha256(text.encode("utf-8")).hexdigest()
print(sha256)
# 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

# SHA-512
sha512 = hashlib.sha512(text.encode("utf-8")).hexdigest()

# MD5 (checksums only)
md5 = hashlib.md5(text.encode("utf-8")).hexdigest()
print(md5)  # 6cd3556deb0da54bca060b4c39479839

# Hash a file in chunks (memory-efficient)
def hash_file(path, algorithm="sha256"):
    h = hashlib.new(algorithm)
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(65536), b""):
            h.update(chunk)
    return h.hexdigest()

# HMAC
import hmac as hmac_module
mac = hmac_module.new(b"secret-key", text.encode(), hashlib.sha256).hexdigest()
Notes & gotchas
  • Always encode strings to bytes before hashing — hashlib only accepts bytes.
  • Use hashlib.new('algorithm') to select the algorithm dynamically.
  • For password hashing use bcrypt or argon2-cffi, not hashlib.
Try it in your browser

Need to hash md5/sha without writing code? The Hash Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Hash MD5/SHA
Hash Generator in other languages