关于 hash-calculator
hash-calculator 是用于计算文本和文件加密哈希值的 Python 工具。它支持批量计算以及校验和的生成与验证,可用于文件完整性检查。
主要功能
- 01支持多种哈希算法
- 02计算文本与文件哈希
- 03流式处理大型文件
- 04批量处理文件和目录
- 05生成及验证校验和文件
适用场景
- 01核对下载文件的完整性
- 02为发布文件生成校验和
- 03比较两个文件是否相同
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill hash-calculator安装后在 Claude Code 中输入/hash-calculator确认已加载。
附带文件scripts/hash_calc.pyscripts/requirements.txt
详细介绍
哈希计算器
计算文本字符串和文件的加密哈希值。支持多种算法、文件验证和批量处理。
快速开始
from scripts.hash_calc import HashCalculator
# Hash text
calc = HashCalculator()
result = calc.hash_text("Hello, World!")
print(result['sha256'])
# Hash file
result = calc.hash_file("document.pdf")
print(result['md5'])
# Verify file integrity
is_valid = calc.verify_file("file.zip", "expected_hash", algorithm="sha256")
功能
- 多种算法:MD5、SHA1、SHA256、SHA384、SHA512、BLAKE2
- 文本哈希:直接对字符串计算哈希值
- 文件哈希:通过高效的流式处理大型文件
- 验证:与预期哈希值进行比较
- 批量处理:对多个文件计算哈希值
- 校验和文件:生成/验证校验和文件
API 参考
文本哈希
calc = HashCalculator()
# Single algorithm
md5 = calc.hash_text("Hello", algorithm="md5")
# All algorithms
results = calc.hash_text("Hello")
# {'md5': '...', 'sha1': '...', 'sha256': '...', ...}
# Specific algorithms
results = calc.hash_text("Hello", algorithms=["md5", "sha256"])
文件哈希
# Single file
result = calc.hash_file("document.pdf")
print(result['sha256'])
# Specific algorithm
sha256 = calc.hash_file("file.zip", algorithm="sha256")
验证
# Verify file against expected hash
is_valid = calc.verify_file(
"download.iso",
"a1b2c3d4e5...",
algorithm="sha256"
)
# Verify text
is_valid = calc.verify_text("password", "5f4dcc3b...", algorithm="md5")
批量处理
# Hash all files in directory
results = calc.hash_directory("./files", algorithm="sha256")
# {'file1.txt': 'abc...', 'file2.pdf': 'def...'}
# With recursive option
results = calc.hash_directory("./files", recursive=True)
校验和文件
# Generate checksum file
calc.generate_checksums("./release", "checksums.sha256", algorithm="sha256")
# Verify against checksum file
results = calc.verify_checksums("checksums.sha256")
# {'file1.zip': True, 'file2.zip': True, 'file3.zip': False}
CLI 用法
# Hash text
python hash_calc.py --text "Hello, World!"
# Hash file
python hash_calc.py --file document.pdf
# Specific algorithm
python hash_calc.py --file document.pdf --algorithm sha256
# Verify file
python hash_calc.py --file download.iso --verify "expected_hash"
# Hash directory
python hash_calc.py --directory ./files --output checksums.txt
# Verify checksums file
python hash_calc.py --verify-checksums checksums.sha256
CLI 参数
| 参数 | 描述 | 默认值 |
|---|---|---|
--text | 要计算哈希值的文本 | - |
--file | 要计算哈希值的文件 | - |
--directory | 要计算哈希值的目录 | - |
--algorithm | 哈希算法 | all |
--verify | 要验证的预期哈希值 | - |
--output | 输出文件 | - |
--recursive | 递归处理目录 | False |
--verify-checksums | 验证校验和文件 | - |
支持的算法
| 算法 | 输出长度 | 用途 |
|---|---|---|
md5 | 128 位(32 个十六进制字符) | 旧版用途、校验和(不安全) |
sha1 | 160 位(40 个十六进制字符) | 旧版用途(不安全) |
sha256 | 256 位(64 个十六进制字符) | 通用、安全 |
sha384 | 384 位(96 个十六进制字符) | 高安全性 |
sha512 | 512 位(128 个十六进制字符) | 最高安全性 |
blake2b | 512 位 | 现代、高速 |
blake2s | 256 位 | 现代、高速、小巧 |
示例
验证下载完整性
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!")
生成发行版校验和
calc = HashCalculator()
# Generate checksums for release files
calc.generate_checksums(
"./release",
"SHA256SUMS",
algorithm="sha256"
)
# Output: SHA256SUMS
# e3b0c44298fc1c14... release-v1.0.zip
# a1b2c3d4e5f6g7h8... release-v1.0.tar.gz
密码存储(示例)
calc = HashCalculator()
import os
# Note: In production, use bcrypt/argon2 instead
salt = os.urandom(16).hex()
password = "user_password"
hashed = calc.hash_text(salt + password, algorithm="sha256")
# Store: salt + ":" + hashed
stored = f"{salt}:{hashed}"
比较两个文件
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")
输出格式
文本输出
MD5: 5d41402abc4b2a76b9719d911017c592
SHA1: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
SHA256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
JSON 输出
{
"input": "hello",
"md5": "5d41402abc4b2a76b9719d911017c592",
"sha256": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
依赖项
(No external dependencies - uses Python standard library)
安全注意事项
- MD5 和 SHA1 不具备抗碰撞性(请勿用于安全场景)
- 安全应用请使用 SHA256 或更高强度的算法
- 密码哈希请使用专用库(bcrypt、argon2)
- 文件哈希采用流式处理,可高效处理大型文件
评论
支持 Markdown