Using SHA-256, PBKDF2-HMAC, and BLAKE2b in Python's hashlib

SHA-256 Hash Generation

import hashlib

# Initialize and update with byte string
sha_hasher1 = hashlib.sha256()
sha_hasher1.update(b'input_data')
output1 = sha_hasher1.hexdigest()

# Initialize and update with encoded string
sha_hasher2 = hashlib.sha256()
sha_hasher2.update('input_data'.encode('utf-8'))
output2 = sha_hasher2.hexdigest()

# Incremental updates
sha_hasher3 = hashlib.sha256()
sha_hasher3.update(b'in')
sha_hasher3.update(b'put')
sha_hasher3.update(b'_data')
output3 = sha_hasher3.hexdigest()

# Direct computation
direct_output = hashlib.sha256(b'input_data').hexdigest()

# Using new() factory method
sha_hasher4 = hashlib.new('sha256')
sha_hasher4.update(b'input_data')
output4 = sha_hasher4.hexdigest()

print(f"Method 1: {output1}")
print(f"Method 2: {output2}")
print(f"Method 3: {output3}")
print(f"Direct: {direct_output}")
print(f"Factory: {output4}")

Password-Based Key Derivation with PBKDF2-HMAC

import hashlib
import binascii
import os

# Derive key with empty salt, single iteration
derived_key1 = hashlib.pbkdf2_hmac('sha256', b'password', b'', 1)
print(f"Iteration 1: {binascii.hexlify(derived_key1).decode()}")

# Repeat with same parameters
derived_key2 = hashlib.pbkdf2_hmac('sha256', b'password', b'', 1)
print(f"Repeat: {binascii.hexlify(derived_key2).decode()}")

# Increase iteration count
derived_key3 = hashlib.pbkdf2_hmac('sha256', b'password', b'', 100)
print(f"Iterations 100: {binascii.hexlify(derived_key3).decode()}")

# Change salt value
derived_key4 = hashlib.pbkdf2_hmac('sha256', b'password', b'unique_salt', 1)
print(f"Different salt: {binascii.hexlify(derived_key4).decode()}")

# Generate random salt
random_salt = os.urandom(16)
derived_key5 = hashlib.pbkdf2_hmac('sha256', b'password', random_salt, 1)
print(f"Random salt: {binascii.hexlify(derived_key5).decode()}")

BLAKE2b Hash Function

import hashlib
import hmac
from cryptography.fernet import Fernet

# Basic hashing operation
blake_hasher = hashlib.blake2b()
blake_hasher.update(b'data_input')
basic_digest = blake_hasher.hexdigest()
print(f"Basic digest: {basic_digest}")

# Verify consistency
verify_digest = hashlib.blake2b(b'data_input').hexdigest()
assert hmac.compare_digest(basic_digest, verify_digest)

# Configured hashing with parameters
config_hasher = hashlib.blake2b(digest_size=10, key=b'secret', salt=b'salt', person=b'person')
config_hasher.update(b'data_input')
config_digest = config_hasher.hexdigest()
print(f"Configured digest: {config_digest}")

# Symmetric encryption demonstration
fernet_key = Fernet.generate_key()
cipher_suite = Fernet(fernet_key)
encrypted_msg = cipher_suite.encrypt(b'data_input')
decrypted_msg = cipher_suite.decrypt(encrypted_msg)
print(f"Decrypted message: {decrypted_msg.decode()}")

# HMAC with BLAKE2b
blake_hmac = hmac.new(fernet_key, b'data_input', digestmod=hashlib.blake2b)
hmac_digest = blake_hmac.hexdigest()
print(f"HMAC digest: {hmac_digest}")

Tags: python hashlib sha256 pbkdf2-hmac blake2b

Posted on Fri, 21 Aug 2026 16:38:17 +0000 by brownka