M. *_*Who 3 python hash python-3.x torrent
下面的代码(对丑陋表示歉意),我运行它来计算 torrent 的哈希值,但它给出的答案与我直接在 Transmission 中打开该 torrent 时不同:
我正在此页面上的 r_000 上进行测试:http://gen.lib.rus.ec/repository_torrent/
传输给我:63a04291a8b266d968aa7ab8a276543fa63a9e84
我的代码给了我:1882ff6534ee4aa660e2fbf225c1796638bea4c0
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)
Run Code Online (Sandbox Code Playgroud)
知道我搞砸了什么吗?谢谢!
我也犯了同样的错误。搜索发现了这个问题,这帮助我解决了它。但为了让其他通过搜索如何从 python3+ 做到这一点的人更清楚,这是明确的修复:
改变:
hashed_info = hashlib.sha1(info[b'pieces']).hexdigest()
Run Code Online (Sandbox Code Playgroud)
到:
hashed_info = hashlib.sha1(bencoding.bencode(info)).hexdigest()
Run Code Online (Sandbox Code Playgroud)
感谢Encombe澄清了此处的信息哈希:https ://stackoverflow.com/questions/28140766/28162042#28162042
Torrent 客户端中的哈希值或您在 Magnet-URI 中找到的哈希值是 Torrent 文件的原始编码信息字典部分的 SHA1 哈希值。
一个完整但简约的例子是:
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)
Run Code Online (Sandbox Code Playgroud)
结果:
$ python3 example.py
63a04291a8b266d968aa7ab8a276543fa63a9e84
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3202 次 |
| 最近记录: |