MD5 & SHA Hash Generator — Developer Code Samples

Cryptographic hash functions produce fixed-size digests from arbitrary input. Python's hashlib and the Node.js crypto module support MD5, SHA-1, SHA-256, and SHA-512 natively. Use hashes for data integrity checks, password verification, and caching.

Try the interactive version online: Open MD5 & SHA Hash Generator Tool →

Parameters

Parameter Type Required Description
text str Yes Input string or bytes to hash
algorithm str No Hash algorithm: md5, sha1, sha256, sha512 (default: sha256)
encoding str No String encoding to use (default: utf-8)

Returns: Hexadecimal hash digest string

Code Examples

import hashlib

# Hash a string with SHA-256 (most common)
text = "Hello, World!"
sha256 = hashlib.sha256(text.encode('utf-8')).hexdigest()
print(f"SHA-256: {sha256}")
# Output: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986d

# MD5 (not for security, use for checksums)
md5 = hashlib.md5(text.encode('utf-8')).hexdigest()
print(f"MD5:     {md5}")

# SHA-1
sha1 = hashlib.sha1(text.encode('utf-8')).hexdigest()
print(f"SHA-1:   {sha1}")

# SHA-512
sha512 = hashlib.sha512(text.encode('utf-8')).hexdigest()
print(f"SHA-512: {sha512}")

# Hash a file (streaming, handles large files)
def hash_file(filepath, algorithm='sha256'):
    h = hashlib.new(algorithm)
    with open(filepath, 'rb') as f:
        for chunk in iter(lambda: f.read(65536), b''):
            h.update(chunk)
    return h.hexdigest()

# HMAC for message authentication
import hmac
key = b'secret-key'
message = b'important message'
mac = hmac.new(key, message, hashlib.sha256).hexdigest()
print(f"HMAC-SHA256: {mac}")

# List all available algorithms
print(hashlib.algorithms_available)