hash-calculator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Hash 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 HashCalculator
python
from scripts.hash_calc import HashCalculator

Hash 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")
undefined
is_valid = calc.verify_file("file.zip", "expected_hash", algorithm="sha256")
undefined

Features

功能特性

  • 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"])
undefined
results = calc.hash_text("Hello", algorithms=["md5", "sha256"])
undefined

File Hashing

文件哈希

python
undefined
python
undefined

Single 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")
undefined
sha256 = calc.hash_file("file.zip", algorithm="sha256")
undefined

Verification

完整性验证

python
undefined
python
undefined

Verify 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")
undefined
is_valid = calc.verify_text("password", "5f4dcc3b...", algorithm="md5")
undefined

Batch Processing

批量处理

python
undefined
python
undefined

Hash 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)
undefined
results = calc.hash_directory("./files", recursive=True)
undefined

Checksum Files

校验和文件

python
undefined
python
undefined

Generate 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}

undefined
undefined

CLI Usage

CLI 使用方法

bash
undefined
bash
undefined

Hash 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
undefined
python hash_calc.py --verify-checksums checksums.sha256
undefined

CLI Arguments

CLI 参数

ArgumentDescriptionDefault
--text
Text to hash-
--file
File to hash-
--directory
Directory to hash-
--algorithm
Hash algorithmall
--verify
Expected hash to verify-
--output
Output file-
--recursive
Recursive directoryFalse
--verify-checksums
Verify checksum file-
参数描述默认值
--text
需计算哈希的文本-
--file
需计算哈希的文件-
--directory
需计算哈希的目录-
--algorithm
哈希算法全部
--verify
用于验证的预期哈希值-
--output
输出文件-
--recursive
递归处理目录False
--verify-checksums
验证校验和文件-

Supported Algorithms

支持的算法

AlgorithmOutput LengthUse Case
md5
128 bits (32 hex)Legacy, checksums (not secure)
sha1
160 bits (40 hex)Legacy (not secure)
sha256
256 bits (64 hex)General purpose, secure
sha384
384 bits (96 hex)High security
sha512
512 bits (128 hex)Maximum security
blake2b
512 bitsModern, fast
blake2s
256 bitsModern, fast, small
算法输出长度适用场景
md5
128位(32位十六进制)遗留系统、校验和(不具备安全性)
sha1
160位(40位十六进制)遗留系统(不具备安全性)
sha256
256位(64位十六进制)通用场景、安全可靠
sha384
384位(96位十六进制)高安全性需求场景
sha512
512位(128位十六进制)最高安全性需求场景
blake2b
512位现代算法、计算快速
blake2s
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!")
undefined
expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" is_valid = calc.verify_file("ubuntu.iso", expected, algorithm="sha256")
if is_valid: print("下载文件验证成功!") else: print("警告:哈希值不匹配 - 文件可能已损坏!")
undefined

Generate 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

undefined
undefined

Password Storage (Example)

密码存储(示例)

python
calc = HashCalculator()
import os
python
calc = HashCalculator()
import os

Note: 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}"
undefined
stored = f"{salt}:{hashed}"
undefined

Compare 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: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
MD5:    5d41402abc4b2a76b9719d911017c592
SHA1:   aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
SHA256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

JSON 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)
  • 文件哈希采用流式处理,可高效处理大文件