hash-calculator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHash Calculator
哈希计算器
Calculate cryptographic hash values for text strings and files. Supports multiple algorithms, file verification, and batch processing.
为文本字符串和文件计算加密哈希值,支持多种算法、文件验证和批量处理。
Quick Start
快速开始
python
from scripts.hash_calc import HashCalculatorpython
from scripts.hash_calc import HashCalculatorHash text
计算文本哈希值
calc = HashCalculator()
result = calc.hash_text("Hello, World!")
print(result['sha256'])
calc = HashCalculator()
result = calc.hash_text("Hello, World!")
print(result['sha256'])
Hash file
计算文件哈希值
result = calc.hash_file("document.pdf")
print(result['md5'])
result = calc.hash_file("document.pdf")
print(result['md5'])
Verify file integrity
验证文件完整性
is_valid = calc.verify_file("file.zip", "expected_hash", algorithm="sha256")
undefinedis_valid = calc.verify_file("file.zip", "expected_hash", algorithm="sha256")
undefinedFeatures
功能特性
- Multiple Algorithms: MD5, SHA1, SHA256, SHA384, SHA512, BLAKE2
- Text Hashing: Hash strings directly
- File Hashing: Efficient streaming for large files
- Verification: Compare against expected hash
- Batch Processing: Hash multiple files
- Checksum Files: Generate/verify checksum files
- 多算法支持:MD5、SHA1、SHA256、SHA384、SHA512、BLAKE2
- 文本哈希:直接计算字符串的哈希值
- 文件哈希:高效流式处理大文件
- 完整性验证:与预期哈希值对比验证
- 批量处理:计算多个文件的哈希值
- 校验和文件:生成/验证校验和文件
API Reference
API 参考
Text Hashing
文本哈希
python
calc = HashCalculator()python
calc = HashCalculator()Single algorithm
单一算法
md5 = calc.hash_text("Hello", algorithm="md5")
md5 = calc.hash_text("Hello", algorithm="md5")
All algorithms
所有算法
results = calc.hash_text("Hello")
results = calc.hash_text("Hello")
{'md5': '...', 'sha1': '...', 'sha256': '...', ...}
{'md5': '...', 'sha1': '...', 'sha256': '...', ...}
Specific algorithms
指定算法
results = calc.hash_text("Hello", algorithms=["md5", "sha256"])
undefinedresults = calc.hash_text("Hello", algorithms=["md5", "sha256"])
undefinedFile Hashing
文件哈希
python
undefinedpython
undefinedSingle file
单个文件
result = calc.hash_file("document.pdf")
print(result['sha256'])
result = calc.hash_file("document.pdf")
print(result['sha256'])
Specific algorithm
指定算法
sha256 = calc.hash_file("file.zip", algorithm="sha256")
undefinedsha256 = calc.hash_file("file.zip", algorithm="sha256")
undefinedVerification
完整性验证
python
undefinedpython
undefinedVerify file against expected hash
验证文件与预期哈希值是否匹配
is_valid = calc.verify_file(
"download.iso",
"a1b2c3d4e5...",
algorithm="sha256"
)
is_valid = calc.verify_file(
"download.iso",
"a1b2c3d4e5...",
algorithm="sha256"
)
Verify text
验证文本
is_valid = calc.verify_text("password", "5f4dcc3b...", algorithm="md5")
undefinedis_valid = calc.verify_text("password", "5f4dcc3b...", algorithm="md5")
undefinedBatch Processing
批量处理
python
undefinedpython
undefinedHash all files in directory
计算目录下所有文件的哈希值
results = calc.hash_directory("./files", algorithm="sha256")
results = calc.hash_directory("./files", algorithm="sha256")
{'file1.txt': 'abc...', 'file2.pdf': 'def...'}
{'file1.txt': 'abc...', 'file2.pdf': 'def...'}
With recursive option
启用递归选项
results = calc.hash_directory("./files", recursive=True)
undefinedresults = calc.hash_directory("./files", recursive=True)
undefinedChecksum Files
校验和文件
python
undefinedpython
undefinedGenerate checksum file
生成校验和文件
calc.generate_checksums("./release", "checksums.sha256", algorithm="sha256")
calc.generate_checksums("./release", "checksums.sha256", algorithm="sha256")
Verify against checksum file
验证校验和文件
results = calc.verify_checksums("checksums.sha256")
results = calc.verify_checksums("checksums.sha256")
{'file1.zip': True, 'file2.zip': True, 'file3.zip': False}
{'file1.zip': True, 'file2.zip': True, 'file3.zip': False}
undefinedundefinedCLI Usage
CLI 使用方法
bash
undefinedbash
undefinedHash text
计算文本哈希值
python hash_calc.py --text "Hello, World!"
python hash_calc.py --text "Hello, World!"
Hash file
计算文件哈希值
python hash_calc.py --file document.pdf
python hash_calc.py --file document.pdf
Specific algorithm
指定算法
python hash_calc.py --file document.pdf --algorithm sha256
python hash_calc.py --file document.pdf --algorithm sha256
Verify file
验证文件
python hash_calc.py --file download.iso --verify "expected_hash"
python hash_calc.py --file download.iso --verify "expected_hash"
Hash directory
计算目录哈希值
python hash_calc.py --directory ./files --output checksums.txt
python hash_calc.py --directory ./files --output checksums.txt
Verify checksums file
验证校验和文件
python hash_calc.py --verify-checksums checksums.sha256
undefinedpython hash_calc.py --verify-checksums checksums.sha256
undefinedCLI Arguments
CLI 参数
| Argument | Description | Default |
|---|---|---|
| Text to hash | - |
| File to hash | - |
| Directory to hash | - |
| Hash algorithm | all |
| Expected hash to verify | - |
| Output file | - |
| Recursive directory | False |
| Verify checksum file | - |
| 参数 | 描述 | 默认值 |
|---|---|---|
| 需计算哈希的文本 | - |
| 需计算哈希的文件 | - |
| 需计算哈希的目录 | - |
| 哈希算法 | 全部 |
| 用于验证的预期哈希值 | - |
| 输出文件 | - |
| 递归处理目录 | False |
| 验证校验和文件 | - |
Supported Algorithms
支持的算法
| Algorithm | Output Length | Use Case |
|---|---|---|
| 128 bits (32 hex) | Legacy, checksums (not secure) |
| 160 bits (40 hex) | Legacy (not secure) |
| 256 bits (64 hex) | General purpose, secure |
| 384 bits (96 hex) | High security |
| 512 bits (128 hex) | Maximum security |
| 512 bits | Modern, fast |
| 256 bits | Modern, fast, small |
| 算法 | 输出长度 | 适用场景 |
|---|---|---|
| 128位(32位十六进制) | 遗留系统、校验和(不具备安全性) |
| 160位(40位十六进制) | 遗留系统(不具备安全性) |
| 256位(64位十六进制) | 通用场景、安全可靠 |
| 384位(96位十六进制) | 高安全性需求场景 |
| 512位(128位十六进制) | 最高安全性需求场景 |
| 512位 | 现代算法、计算快速 |
| 256位 | 现代算法、计算快速、轻量 |
Examples
示例
Verify Download Integrity
验证下载文件完整性
python
calc = HashCalculator()python
calc = HashCalculator()Downloaded file and expected hash from website
下载的文件与官网提供的预期哈希值
expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
is_valid = calc.verify_file("ubuntu.iso", expected, algorithm="sha256")
if is_valid:
print("Download verified successfully!")
else:
print("WARNING: Hash mismatch - file may be corrupted!")
undefinedexpected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
is_valid = calc.verify_file("ubuntu.iso", expected, algorithm="sha256")
if is_valid:
print("下载文件验证成功!")
else:
print("警告:哈希值不匹配 - 文件可能已损坏!")
undefinedGenerate Release Checksums
生成发布版本校验和
python
calc = HashCalculator()python
calc = HashCalculator()Generate checksums for release files
为发布版本文件生成校验和
calc.generate_checksums(
"./release",
"SHA256SUMS",
algorithm="sha256"
)
calc.generate_checksums(
"./release",
"SHA256SUMS",
algorithm="sha256"
)
Output: SHA256SUMS
输出:SHA256SUMS
e3b0c44298fc1c14... release-v1.0.zip
e3b0c44298fc1c14... release-v1.0.zip
a1b2c3d4e5f6g7h8... release-v1.0.tar.gz
a1b2c3d4e5f6g7h8... release-v1.0.tar.gz
undefinedundefinedPassword Storage (Example)
密码存储(示例)
python
calc = HashCalculator()
import ospython
calc = HashCalculator()
import osNote: In production, use bcrypt/argon2 instead
注意:生产环境中请使用 bcrypt/argon2 等专用库
salt = os.urandom(16).hex()
password = "user_password"
hashed = calc.hash_text(salt + password, algorithm="sha256")
salt = os.urandom(16).hex()
password = "user_password"
hashed = calc.hash_text(salt + password, algorithm="sha256")
Store: salt + ":" + hashed
存储格式:salt + ":" + hashed
stored = f"{salt}:{hashed}"
undefinedstored = f"{salt}:{hashed}"
undefinedCompare Two Files
对比两个文件
python
calc = HashCalculator()
hash1 = calc.hash_file("file1.txt", algorithm="sha256")
hash2 = calc.hash_file("file2.txt", algorithm="sha256")
if hash1 == hash2:
print("Files are identical")
else:
print("Files are different")python
calc = HashCalculator()
hash1 = calc.hash_file("file1.txt", algorithm="sha256")
hash2 = calc.hash_file("file2.txt", algorithm="sha256")
if hash1 == hash2:
print("文件内容完全一致")
else:
print("文件内容存在差异")Output Formats
输出格式
Text Output
文本输出
MD5: 5d41402abc4b2a76b9719d911017c592
SHA1: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
SHA256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824MD5: 5d41402abc4b2a76b9719d911017c592
SHA1: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
SHA256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824JSON Output
JSON 输出
json
{
"input": "hello",
"md5": "5d41402abc4b2a76b9719d911017c592",
"sha256": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}json
{
"input": "hello",
"md5": "5d41402abc4b2a76b9719d911017c592",
"sha256": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}Dependencies
依赖项
(No external dependencies - uses Python standard library)(无外部依赖 - 仅使用 Python 标准库)Security Notes
安全注意事项
- MD5 and SHA1 are not collision-resistant (don't use for security)
- Use SHA256 or higher for security applications
- For password hashing, use dedicated libraries (bcrypt, argon2)
- File hashing uses streaming to handle large files efficiently
- MD5 和 SHA1 不具备抗碰撞性(请勿用于安全场景)
- 安全相关应用请使用 SHA256 或更高版本的算法
- 密码哈希请使用专用库(如 bcrypt、argon2)
- 文件哈希采用流式处理,可高效处理大文件