Python

Generate Hashes in Python

Create MD5, SHA-256, bcrypt hashes in Python using hashlib and secrets modules.

Try Hash Generator →

Code Example

import hashlib
import hmac

# SHA-256
sha256_hash = hashlib.sha256(b'Hello World').hexdigest()
print(sha256_hash)

# MD5
md5_hash = hashlib.md5(b'Hello World').hexdigest()
print(md5_hash)

# SHA-1
sha1_hash = hashlib.sha1(b'Hello World').hexdigest()
print(sha1_hash)

# SHA-512
sha512_hash = hashlib.sha512(b'Hello World').hexdigest()
print(sha512_hash)

# Hash string (encode first)
text = 'Hello World'
hash_result = hashlib.sha256(text.encode('utf-8')).hexdigest()

# HMAC
hmac_hash = hmac.new(
    key=b'secret-key',
    msg=b'Hello World',
    digestmod=hashlib.sha256
).hexdigest()

# Hash file
def hash_file(filepath):
    sha256 = hashlib.sha256()
    with open(filepath, 'rb') as f:
        for chunk in iter(lambda: f.read(4096), b''):
            sha256.update(chunk)
    return sha256.hexdigest()

# bcrypt (for passwords - install: pip install bcrypt)
import bcrypt
password = b'secret_password'
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
# Verify
bcrypt.checkpw(password, hashed)  # True
Key Points: hashlib provides common algorithms. For passwords, use bcrypt with salting. Always hash in chunks for large files.

Related Examples

More Python Examples