この答えを探してみましたが、代わりに実行してくれるソフトウェアしか見つからないようです。これを Python で行う方法を誰か知っていますか?
ベストアンサー1
私はハッシュを検証するPythonコードを書きましたダウンロードしたファイルの内容に対して.torrent ファイルダウンロードの破損をチェックしたい場合は、これが役に立つかもしれません。
必要なのはbencode パッケージこれを使用するには、Bencode を使用します。Bencode は、.torrent ファイルで使用されるシリアル化形式です。JSON のように、リスト、辞書、文字列、数値を整理できます。
コードは文字列に含まれるハッシュを取得しますinfo['pieces']
。
torrent_file = open(sys.argv[1], "rb")
metainfo = bencode.bdecode(torrent_file.read())
info = metainfo['info']
pieces = StringIO.StringIO(info['pieces'])
この文字列には、20 バイトのハッシュ (各部分に 1 つずつ) が連続して含まれています。これらのハッシュは、ディスク上のファイルの部分のハッシュと比較されます。
このコードの唯一の複雑な部分は、複数のファイルのトレントを処理することです。単一のトレントではピース複数のファイルにまたがることが可能(BitTorrent は内部的に複数ファイルのダウンロードを 1 つの連続したファイルとして扱います)pieces_generator()
それを抽象化するためにジェネレータ関数を使用しています。
ぜひ読んでみてくださいBitTorrent 仕様これをさらに詳しく理解するため。
完全なコードは以下の通りです:
import sys, os, hashlib, StringIO, bencode
def pieces_generator(info):
"""Yield pieces from download file(s)."""
piece_length = info['piece length']
if 'files' in info: # yield pieces from a multi-file torrent
piece = ""
for file_info in info['files']:
path = os.sep.join([info['name']] + file_info['path'])
print path
sfile = open(path.decode('UTF-8'), "rb")
while True:
piece += sfile.read(piece_length-len(piece))
if len(piece) != piece_length:
sfile.close()
break
yield piece
piece = ""
if piece != "":
yield piece
else: # yield pieces from a single file torrent
path = info['name']
print path
sfile = open(path.decode('UTF-8'), "rb")
while True:
piece = sfile.read(piece_length)
if not piece:
sfile.close()
return
yield piece
def corruption_failure():
"""Display error message and exit"""
print("download corrupted")
exit(1)
def main():
# Open torrent file
torrent_file = open(sys.argv[1], "rb")
metainfo = bencode.bdecode(torrent_file.read())
info = metainfo['info']
pieces = StringIO.StringIO(info['pieces'])
# Iterate through pieces
for piece in pieces_generator(info):
# Compare piece hash with expected hash
piece_hash = hashlib.sha1(piece).digest()
if (piece_hash != pieces.read(20)):
corruption_failure()
# ensure we've read all pieces
if pieces.read():
corruption_failure()
if __name__ == "__main__":
main()