从torrent文件中提取SHA1哈希

时间:2010-04-03 20:10:22

标签: python hash extract sha1 bittorrent

我已经仔细研究了这个答案,但我似乎只能找到适合你的软件。有人知道如何在python中执行此操作吗?

3 个答案:

答案 0 :(得分:28)

我编写了一段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字节的哈希值(每件一个)。然后将这些哈希值与磁盘文件的哈希值进行比较。

此代码中唯一复杂的部分是处理多文件种子,因为单个torrent piece 可以跨越多个文件(内部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()

答案 1 :(得分:17)

这里我是如何从torrent文件中提取HASH值的:

#!/usr/bin/python

import sys, os, hashlib, StringIO
import bencode



def main():
    # Open torrent file
    torrent_file = open(sys.argv[1], "rb")
    metainfo = bencode.bdecode(torrent_file.read())
    info = metainfo['info']
    print hashlib.sha1(bencode.bencode(info)).hexdigest()    

if __name__ == "__main__":
    main()

与运行命令相同:

transmissioncli -i test.torrent 2>/dev/null | grep "^hash:" | awk '{print $2}'

希望,这有助于:)

答案 2 :(得分:-2)

根据this,您应该能够通过搜索看起来像这样的数据部分找到文件的md5sums:

d[...]6:md5sum32:[hash is here][...]e

(SHA不是规范的一部分)