Calculating the Info-Hash of a Torrent File

Calculating the info-hash of a torrent file

Make sure you don't have a newline at the end of the file, you may also want to make sure it ends with an 'e'.

The info-hash of a torrent file is the SHA-1 hash of the info-section (in bencoded form) from the .torrent file. Essentially you need to decode the file (it's bencoded) and remember the byte offsets where the content of the value associated with the "info" key begins and end. That's the range of bytes you need to hash.

For example, if this is the torrent file:

d4:infod6:pieces20:....................4:name4:test12:piece lengthi1024ee8:announce27:http://tracker.com/announcee

You wan to just hash this section:

d6:pieces20:....................4:name4:test12:piece lengthi1024ee

For more information on bencoding, see BEP3.

What exactly is the info_Hash in a torrent file

So I finally figured it out.

The “infohash” is the SHA1 Hash over the part of a torrent file that includes:

  1. ITEM: length(size) and path (path with filename)
  2. Name: The name to search for
  3. Piece length: The length(size) of a single piece
  4. Pieces: SHA1 Hash of EVERY piece of this torrent
  5. Private: flag for restricted access

To show this a little more I took a random torrent file and used the “BEncode Editor” from Ultima to make it more clearly to me.

BEncode Editor

As you can see the the red box marked the information part of the torrent file.
The torrent file includes not the Hash of the items, but the hashes of every piece.

  • For item1 with: 1069496548
  • and item2 with: 223
  • It is together: 1069496771
  • With a piece size of: 524288
  • There are 2040 pieces. (1069496771/524288=2039.9032 approximately)
  • The pieces section includes 40800 byte of data what are 81600 + 2 chars in the file.
  • the +2 because 0x marks that this is hexadecimal.
  • A SHA1 hash has 40 0x chars or 20 Byte of data what are 2040 SHA1 hashes.

Sample Image

I am sorry that this information is about a torrent that leads to a illegal movie, but i wanted to use a torrent that realy exists.

Hash calculation in torrent clients

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

To understand how that works you need to know two things:

  • How a torrent-file is built.

  • How bencodeing is done.

Both of these are explained in the offical bittorrent specification that you can find here: http://bittorrent.org/beps/bep_0003.html

However I recommed that you instead read the inoffical specification that you can find here: https://wiki.theory.org/BitTorrentSpecification

as it is much easier to understand.

The torrent info_hash parameter

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



Related Topics



Leave a reply



Submit