4

How does one calculate the info_hash parameter? Aka the hash corresponding to the info dictionar??

From official specs:

info_hash The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. Note that this is a substring of the metainfo file. This value will almost certainly have to be escaped.

Does this mean simply get the substring from the meta-info file and do a sha-1 hash on the reprezentative bytes??

.... because this is how i tried 12 times but without succes meaning I have compared the resulting hash with the one i should end up with..and they differ ..that + tracker response is FAILURE, unknown torrent ...or something

So how do you calculate the info_hash?

pulancheck1988
  • 2,024
  • 3
  • 28
  • 46

2 Answers2

4

The metafile is already bencoded so I don't understand why you encode it again?

I finally got this working in Java code, here is my code:

byte metaData[];  //the raw .torrent file

int infoIdx = ?;  //index of 'd' right after the "4:info" string

info_hash = SHAsum(Arrays.copyOfRange(metaData, infoIdx, metaData.length-1));

This assumes the 'info' block is the last block in the torrent file (wrong?)

Don't sort or anything like that, just use a substring of the raw torrent file.

Works for me.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Peter Quiring
  • 1,648
  • 1
  • 16
  • 21
  • Found this as a solution on multiple sites.. this assumes that .torrent was built by the rules (having all dicts sorted), which should mainly always be true, except if made by my silly app..but anyways i ended up in using another bencoding library & now all works well (reading & creating).. thanks for the answer – pulancheck1988 May 25 '12 at 15:13
  • @pulancheck1988 but you have to sort the dict keys in the info structure, or the infohash computation will not be correct. So if you have to sort the keys in the info structure, you might as well sort them everywhere. – b0fh Feb 25 '13 at 11:15
  • @b0fh It's in fact wrong to sort an unsorted info dict when you calculate the info_hash. [See my comment here.](http://stackoverflow.com/questions/10191480/the-torrent-info-hash-parameter/10203508#comment48969520_10203508) – Encombe May 25 '15 at 18:21
  • There you have an example where the "info" block is not the last one: https://webtorrent.io/torrents/sintel.torrent It has a "url-list" key following. – Victor Basso Sep 16 '17 at 21:50
2

bdecode the metafile. Then it's simply sha1(bencode(metadata['info'])) (i.e. bencode only the info dict again, then hash that).

nz_21
  • 6,140
  • 7
  • 34
  • 80
pyroscope
  • 4,120
  • 1
  • 18
  • 13
  • 1
    this is exactly what i do.. i use this library: bencode.codeplex.com ..the BDict has a method string Encode(), which generated the coresponding string for a dict.. and i use that for the info dict + get the bytes (tried with booth encoding UTF-8, ASCII) .. and do hash on those bytes..still no getting the correct hash – pulancheck1988 Apr 18 '12 at 08:47
  • do you thing sorting the dict would help... in current implementation encode doesnt sort? and how would one sort by string accordingly to torrent specs.. – pulancheck1988 Apr 18 '12 at 08:48
  • if that is exactly what you do, there is a bug in your code somewhere, and you'd have to post the code if you want anyone to find it for you. – Arvid Apr 18 '12 at 16:59
  • 1
    BEP-3 *requires* sorting the keys, and makes very clear the encoding is always UTF-8, sorting is by comparing encoded bytes, no collating of any sort. If the above doesn't work and delivers wrong hashes, your lib has bugs and needs some unit tests. – pyroscope Apr 19 '12 at 20:06
  • this is the answer i needed .. i will investigate if sorting by keys gets me to a correct hash .. thanks – pulancheck1988 Apr 20 '12 at 14:15
  • 2
    [BEP-3](http://bittorrent.org/beps/bep_0003.html) has been updated and now says: "info_hash - The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. Note that this is a substring of the metainfo file. **The info-hash must be the hash of the encoded form as found in the .torrent file, regardless of it being invalid.** " [more info](http://stackoverflow.com/questions/19749085/calculating-the-info-hash-of-a-torrent-file/19800109#comment44825963_19800109) – Encombe May 25 '15 at 18:02