0

I understand that you must calculate the SHA1 of the info dict in the torrent file, I am struggling Parsing the info dict, Do i Need to decode the bencoded info dict before calculating the sha1 hash??

How do I do it?

Poontang
  • 1
  • 1

1 Answers1

6

The info hash is the SHA-1 hash of the info dictionary in bencoded format.

The torrent file contains a bencoded representation of the meta info dictionary which in turn contains the info dictionary.

You have to get the info dictionary part of the file in order to compute its hash. Assuming you have access to a bencode encoder and decoder you could:

  • decode the whole file
  • take the info dictionary part of it
  • re-encode it for hashing.

In pseudo code:

metainfo = b_decode(contents_of("file.torrent"))
info = metainfo.getValue("info")
encoded_info = b_encode(info)
info_hash = sha1(encoded_info)
Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
  • That would be wrong in some cases, because it would sort a unsorted info-dict and the agreed convention when a info-dict for some reason is unsorted, is to hash the info-dict raw as it is (unsorted). A more safe metod is described by Arvid here: http://stackoverflow.com/questions/19749085/calculating-the-info-hash-of-a-torrent-file/19800109#19800109 – Encombe Jan 30 '15 at 09:48