下面的代码(对丑表示歉意),我运行它是为了计算洪流的散列,但是它给了我一个不同的答案,当我在传输中直接打开那个洪流时:
我正在r_000上测试这个页面:torrent/
传输给我:63a04291a8b266d93.aa7ab8a276543fa63a9e84
我的代码是:1882ff6534ee4a660e2fbf225c1796638bea4c0
import bencoding
from io import BytesIO
import binascii
import hashlib
with open("cache/r_000.torrent", "rb") as f:
data = bencoding.bdecode(f.read())
info = data[b'info']
hashed_info = hashlib.sha1(info[b'pieces']).hexdigest()
print(hashed_info)知道我搞砸了什么吗?谢谢!
发布于 2017-09-18 02:18:04
我也犯了同样的错误。搜索发现了这个问题,这帮我解决了这个问题。但是,为了让其他人更清楚地了解如何从python3+进行这种方式的搜索,这是一个显式的解决方法:
更改:
hashed_info = hashlib.sha1(info[b'pieces']).hexdigest()至:
hashed_info = hashlib.sha1(bencoding.bencode(info)).hexdigest()感谢Encombe澄清了这里的信息哈希:https://stackoverflow.com/questions/28140766/28162042#28162042
torrent客户端中的散列或在magnet中找到的散列是原始本编码信息字典的SHA1 1-散列--洪流文件的一部分。
一个完整但极简的例子是:
import bencoding, hashlib
objTorrentFile = open("r_0000.torrent", "rb")
decodedDict = bencoding.bdecode(objTorrentFile.read())
info_hash = hashlib.sha1(bencoding.bencode(decodedDict[b"info"])).hexdigest()
print(info_hash)结果:
$ python3 example.py
63a04291a8b266d968aa7ab8a276543fa63a9e84https://stackoverflow.com/questions/46025771
复制相似问题