gpt4 book ai didi

python - 从 torrent 文件中提取 SHA1 哈希

转载 作者:IT老高 更新时间:2023-10-28 20:31:40 30 4
gpt4 key购买 nike

我已经四处寻找这个问题的答案,但我似乎只能找到可以为您解决问题的软件。有人知道如何在 python 中执行此操作吗?

最佳答案

我编写了一段 Python 代码,用于根据 .torrent 文件中的内容验证下载文件的哈希值。假设您想检查下载是否损坏,您可能会发现这很有用。

您需要 bencode package使用这个。 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 字节的散列(每个部分一个)。然后将这些哈希值与磁盘上文件的哈希值进行比较。

此代码唯一复杂的部分是处理多文件种子,因为单个种子片段可以跨越多个文件(在内部,BitTorrent 将多文件下载视为单个连续文件)。我正在使用生成器函数 pieces_generator() 将其抽象出来。

您可能想阅读 BitTorrent spec更详细地了解这一点。

完整代码如下:

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()

关于python - 从 torrent 文件中提取 SHA1 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2572521/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com