
Security News
TypeScript 6.0 Released: The Final JavaScript-Based Version
TypeScript 6.0 introduces new standard APIs, modern default settings, and deprecations as it prepares projects for the upcoming TypeScript 7.0 release.
hash-utils-py
Advanced tools
Hash Utils Py is a powerful and easy-to-use hashing library for Python. It provides a unified interface for various hash algorithms, file and directory operations, and secure hash verification.
pip install hash-utils-py
from hash_utils import calculate_hash, verify_hash, hash_file
# Hash a string
hash_value = calculate_hash("Hello, World!", algorithm="sha256")
print(f"SHA256: {hash_value}")
# Verify a hash
is_valid = verify_hash("Hello, World!", hash_value)
print(f"Password correct: {is_valid}")
# Hash a file
file_hash = hash_file("document.pdf")
print(f"File hash: {file_hash}")
from hash_utils import calculate_hash
# Hash a string (default SHA256)
hash_sha256 = calculate_hash("Hello World")
print(hash_sha256)
# Output: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
# Hash with specific algorithm
hash_md5 = calculate_hash("Hello World", algorithm="md5")
print(hash_md5)
# Output: b10a8db164e0754105b7a99be72e3fe5
hash_sha1 = calculate_hash("Hello World", algorithm="sha1")
print(hash_sha1)
# Output: 0a4d55a8d778e5022fab701977c5d840bbc486d0
# Hash bytes data
data = b"Binary data here"
hash_bytes = calculate_hash(data, algorithm="blake2b")
print(hash_bytes)
# Different encodings
text = "Hello World"
hash_utf8 = calculate_hash(text, encoding="utf-8")
hash_utf16 = calculate_hash(text, encoding="utf-16")
print(f"UTF-8: {hash_utf8}")
print(f"UTF-16: {hash_utf16}")
from hash_utils import verify_hash
# Store a hash
original_password = "my_secret_password"
stored_hash = calculate_hash(original_password)
# Verify password
user_input = "my_secret_password"
if verify_hash(user_input, stored_hash):
print("Access granted")
else:
print("Invalid password")
# Secure comparison protects against timing attacks
result = verify_hash("password", stored_hash)
from hash_utils import hash_file
import os
# Hash a small file
file_hash = hash_file("config.json")
print(f"Config hash: {file_hash}")
# Hash large files with custom buffer
large_file_hash = hash_file(
"video.mp4",
algorithm="sha512",
chunk_size=16384 # 16KB buffer
)
print(f"Video hash: {large_file_hash}")
# Error handling
try:
hash_value = hash_file("/path/to/nonexistent/file.txt")
except FileNotFoundError:
print("File not found")
except PermissionError:
print("Permission denied")
# Practical: verify file integrity
def verify_file_integrity(filepath, expected_hash):
"""Verify file integrity"""
current_hash = hash_file(filepath, algorithm="sha256")
return current_hash == expected_hash
# Usage
if verify_file_integrity("download.zip", "abc123..."):
print("File is intact")
from hash_utils import hash_directory
import json
# Hash all files in a directory
hashes = hash_directory("./my_project")
for filename, file_hash in hashes.items():
print(f"{filename}: {file_hash}")
# Save hashes to JSON
with open("file_hashes.json", "w") as f:
json.dump(hashes, f, indent=2)
# Hash with different algorithm
hashes_sha512 = hash_directory("./documents", algorithm="sha512")
# Filter results
hashes = hash_directory("./project")
only_py_files = {k: v for k, v in hashes.items() if k.endswith('.py')}
print(f"Found {len(only_py_files)} Python files")
from hash_utils import generate_salt, calculate_hash
# Generate random salt
salt = generate_salt() # 32 bytes (64 hex characters)
print(f"Salt: {salt}")
# Generate different salt lengths
salt_16 = generate_salt(16) # 16 bytes
salt_64 = generate_salt(64) # 64 bytes
# Practical: secure password hashing
def hash_password(password: str) -> str:
"""Hash password with salt"""
salt = generate_salt()
password_hash = calculate_hash(password + salt)
return f"{salt}${password_hash}"
def verify_password(password: str, stored_hash: str) -> bool:
"""Verify password"""
salt, hash_value = stored_hash.split('$')
return verify_hash(password + salt, hash_value)
# Usage
stored = hash_password("my_secure_password")
print(f"Stored in DB: {stored}")
# Verification
is_correct = verify_password("my_secure_password", stored)
print(f"Password correct: {is_correct}")
from hash_utils import get_hash_algorithm
# Detect algorithm by hash length
hash_md5 = "098f6bcd4621d373cade4e832627b4f6"
algorithm = get_hash_algorithm(hash_md5)
print(f"Algorithm: {algorithm}") # Output: md5
hash_sha256 = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
algorithm = get_hash_algorithm(hash_sha256)
print(f"Algorithm: {algorithm}") # Output: sha256
# Unknown algorithm
unknown = "abc123"
algorithm = get_hash_algorithm(unknown)
print(f"Algorithm: {algorithm}") # Output: None
from hash_utils import hash_file, verify_hash
import os
import json
class FileIntegrityChecker:
"""File integrity checker class"""
def __init__(self, manifest_file="integrity.json"):
self.manifest_file = manifest_file
self.hashes = {}
def generate_manifest(self, directory):
"""Generate hash manifest for all files"""
from hash_utils import hash_directory
self.hashes = hash_directory(directory)
self.save_manifest()
def save_manifest(self):
"""Save manifest to file"""
with open(self.manifest_file, 'w') as f:
json.dump(self.hashes, f, indent=2)
def verify_integrity(self):
"""Verify file integrity"""
with open(self.manifest_file, 'r') as f:
stored_hashes = json.load(f)
corrupted = []
for filepath, expected_hash in stored_hashes.items():
if os.path.exists(filepath):
current_hash = hash_file(filepath)
if not verify_hash(current_hash, expected_hash):
corrupted.append(filepath)
else:
corrupted.append(f"{filepath} (missing)")
return corrupted
# Usage
checker = FileIntegrityChecker()
checker.generate_manifest("/path/to/project")
corrupted_files = checker.verify_integrity()
if corrupted_files:
print("Corrupted files found:")
for file in corrupted_files:
print(f" - {file}")
else:
print("All files are intact")
from hash_utils import calculate_hash, generate_salt, verify_hash
class PasswordManager:
"""Password manager with salt"""
def __init__(self, algorithm="sha256"):
self.algorithm = algorithm
self.users_db = {}
def create_user(self, username, password):
"""Create a new user"""
salt = generate_salt()
password_hash = calculate_hash(password + salt, self.algorithm)
self.users_db[username] = {
'salt': salt,
'hash': password_hash
}
print(f"User {username} created")
def authenticate(self, username, password):
"""Authenticate user"""
if username not in self.users_db:
return False
user = self.users_db[username]
expected_hash = calculate_hash(password + user['salt'], self.algorithm)
if verify_hash(expected_hash, user['hash']):
print(f"User {username} successfully logged in")
return True
else:
print(f"Invalid password for {username}")
return False
# Usage
pm = PasswordManager()
pm.create_user("alice", "SecurePass123!")
pm.create_user("bob", "AnotherPass456!")
pm.authenticate("alice", "SecurePass123!") # Success
pm.authenticate("alice", "WrongPassword") # Failure
from hash_utils import hash_file
import os
from collections import defaultdict
def find_duplicates(directory):
"""Find duplicate files in directory"""
hashes = defaultdict(list)
for root, dirs, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
try:
file_hash = hash_file(filepath)
hashes[file_hash].append(filepath)
except (IOError, PermissionError):
continue
# Filter only duplicates
duplicates = {h: paths for h, paths in hashes.items() if len(paths) > 1}
return duplicates
# Usage
duplicates = find_duplicates("./downloads")
for hash_value, files in duplicates.items():
print(f"\nDuplicates (hash: {hash_value[:16]}...):")
for file in files:
size = os.path.getsize(file) / 1024 # KB
print(f" - {file} ({size:.1f} KB)")
from hash_utils import hash_directory, hash_file
import argparse
from datetime import datetime
def create_checksum_file(directory, output_file="checksums.txt", algorithm="sha256"):
"""Create checksum file for directory"""
hashes = hash_directory(directory, algorithm)
with open(output_file, 'w') as f:
f.write(f"# Checksums ({algorithm})\n")
f.write(f"# Created: {datetime.now()}\n\n")
for filename, file_hash in sorted(hashes.items()):
if not filename.endswith(('ERROR', 'PermissionError')):
f.write(f"{file_hash} {filename}\n")
print(f"Checksum file created: {output_file}")
print(f"Total files: {len(hashes)}")
def verify_checksum_file(checksum_file):
"""Verify checksum file"""
with open(checksum_file, 'r') as f:
lines = [line.strip() for line in f if line.strip() and not line.startswith('#')]
errors = []
for line in lines:
if ' ' in line:
expected_hash, filename = line.split(' ', 1)
try:
current_hash = hash_file(filename)
if current_hash != expected_hash:
errors.append(f"{filename}: {expected_hash} != {current_hash}")
except FileNotFoundError:
errors.append(f"{filename}: File not found")
if errors:
print("Errors found:")
for error in errors:
print(f" {error}")
else:
print("All files verified successfully!")
# Usage
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Checksum utility")
parser.add_argument("command", choices=["create", "verify"])
parser.add_argument("path")
parser.add_argument("--output", default="checksums.txt")
parser.add_argument("--algorithm", default="sha256")
args = parser.parse_args()
if args.command == "create":
create_checksum_file(args.path, args.output, args.algorithm)
else:
verify_checksum_file(args.path)
MIT License
Copyright (c) 2024 Your Name
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
FAQs
Comprehensive hashing utilities for Python
We found that hash-utils-py demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
TypeScript 6.0 introduces new standard APIs, modern default settings, and deprecations as it prepares projects for the upcoming TypeScript 7.0 release.

Security News
/Research
Newly published Trivy Docker images (0.69.4, 0.69.5, and 0.69.6) were found to contain infostealer IOCs and were pushed to Docker Hub without corresponding GitHub releases.

Research
/Security News
The worm-enabled campaign hit @emilgroup and @teale.io, then used an ICP canister to deliver follow-on payloads.