22 python hash bittorrent sha1 extract
我已经四处寻找答案,但我似乎只能找到适合你的软件.有人知道如何在python中这样做吗?
Ale*_*min 29
我写了一段python代码,它根据.torrent文件中的内容验证下载文件的哈希值.假设您要检查下载是否有损坏,您可能会觉得这很有用.
您需要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'])
Run Code Online (Sandbox Code Playgroud)
该字符串包含一系列20字节的哈希值(每个哈希值一个).然后将这些散列与磁盘文件的散列进行比较.
此代码中唯一复杂的部分是处理多文件种子,因为单个torrent 片段可以跨越多个文件(内部BitTorrent将多文件下载视为单个连续文件).我正在使用生成器函数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()
Run Code Online (Sandbox Code Playgroud)
gru*_*dic 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()
Run Code Online (Sandbox Code Playgroud)
它与运行命令相同:
transmissioncli -i test.torrent 2>/dev/null | grep "^hash:" | awk '{print $2}'
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你 :)