Python3 calculating torrent hash

3.2k views Asked by At

Code below (apologies for ugliness), I'm running this to calculate the hash for a torrent but it is giving me a different answer than when I open that torrent directly in Transmission:

I'm testing on r_000 on this page: http://gen.lib.rus.ec/repository_torrent/

Transmission gives me: 63a04291a8b266d968aa7ab8a276543fa63a9e84

My code gives me: 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)

Any idea what I've screwed up? Thanks!

1

There are 1 answers

2
CGar On BEST ANSWER

I made the same mistake. Searching found this question and that helped me fix it. But to make it clearer for others who come this way via searches on how to do it from python3+ this is the explicit fix:

Change:

hashed_info = hashlib.sha1(info[b'pieces']).hexdigest()

to:

hashed_info = hashlib.sha1(bencoding.bencode(info)).hexdigest()

Thanks to Encombe for clarifying the info hash here: https://stackoverflow.com/questions/28140766/28162042#28162042

The hash in a torrent client or the hash you find in a magnet-URI is the SHA1-hash of the raw bencoded info-dictionary-part of a torrent-file.


A full but minimalistic example is:

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)

Result:

$ python3 example.py
63a04291a8b266d968aa7ab8a276543fa63a9e84