hash-calculator について
hash-calculatorは、テキストやファイルの暗号学的ハッシュ値を計算するPythonツールです。バッチ処理に対応し、ファイルの整合性確認向けにチェックサムの生成と検証を行えます。
主な機能
- 01複数のハッシュアルゴリズムに対応
- 02テキストとファイルのハッシュ計算
- 03大容量ファイルのストリーム処理
- 04ファイルやディレクトリの一括処理
- 05チェックサムファイルの生成と検証
活用シーン
- 01ダウンロードファイルの整合性確認
- 02リリースファイルのチェックサム作成
- 032つのファイルが同一か比較
インストール
コマンドをコピーしてターミナルで実行します。
$
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 bits (32 hex) | レガシー、チェックサム(安全ではありません) |
sha1 | 160 bits (40 hex) | レガシー(安全ではありません) |
sha256 | 256 bits (64 hex) | 汎用、安全 |
sha384 | 384 bits (96 hex) | 高いセキュリティ |
sha512 | 512 bits (128 hex) | 最高レベルのセキュリティ |
blake2b | 512 bits | モダン、高速 |
blake2s | 256 bits | モダン、高速、小型 |
使用例
ダウンロードの完全性を検証する
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}"
2 つのファイルを比較する
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 対応