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

标签 python hash extract sha1 bittorrent

我已经四处寻找这个问题的答案,但我似乎只能找到可以为您解决问题的软件。有人知道如何在 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/

相关文章:

python - 突出显示 GNOME 窗口列表中的一个窗口

Javascript HashTable 使用对象键

linux - 如何从 Linux 服务器上的大型 (30Gb+) zip 文件中提取文件

python - 如何将图像的 Blob 转移到白色背景?

python - 提取在 pandas 中不 float 的 DataFrame 的索引值

python - 静态 css 文件未在 Django 管理中加载

python - 测试Python代码的初学者,需要帮助!

python - 使用 Pip 和 Anaconda 安装包 (Windows)

ruby - 检查零值的散列

perl - 如何访问嵌套 Perl 散列的值?