栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从种子文件中提取SHA1哈希

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从种子文件中提取SHA1哈希

我写了一段python代码,根据 .torrent文件 中的内容验证 下载文件 的哈希值。假设您要检查下载是否损坏,则可能会发现此功能有用。 __

您需要benpre包才能使用它。Benpre是.torrent文件中使用的序列化格式。它可以封送列表,字典,字符串和数字,就像JSON。

该代码采用

info['pieces']
字符串中包含的哈希值:

torrent_file = open(sys.argv[1], "rb")metainfo = benpre.bdepre(torrent_file.read())info = metainfo['info']pieces = StringIO.StringIO(info['pieces'])

该字符串包含连续的20个字节的哈希值(每段一个)。然后,将这些哈希与磁盘文件碎片的哈希进行比较。

此代码的唯一复杂部分被处理多文件种子因为单个洪流 可以跨越多于一个文件 (内部BitTorrent的治疗多文件下载作为单个连续文件)
。我正在使用生成器函数

pieces_generator()
将其抽象化。

您可能需要阅读BitTorrent规范以更详细地了解这一点。

完整代码如下:

import sys, os, hashlib, StringIO, benpredef 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.depre('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.depre('UTF-8'), "rb")        while True: piece = sfile.read(piece_length) if not piece:     sfile.close()     return yield piecedef 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 = benpre.bdepre(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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号