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:
- ITEM: length(size) and path (path with filename)
- Name: The name to search for
- Piece length: The length(size) of a single piece
- Pieces: SHA1 Hash of EVERY piece of this torrent
- 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.
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.
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 hash
you 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
bencode
ing 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
Stoi and Std::To_String on Mingw 4.7.1
Image Retrieval System by Colour from the Web Using C++ with Openframeworks
Why Are Std::Atomic Objects Not Copyable
Exception Error C0000005 in Vc++
Referencing Memory Operands in .Intel_Syntax Gnu C Inline Assembly
Want to Efficiently Overcome Mismatch Between Key Types in a Map in Boost.Interprocess Shared Memory
Undefined Reference to a Static Member of the Class
Vscode G++ It Isn't Finding .Cpp Definition Files
Ordering of Using Namespace Std; and Includes
Uniform Initialization Fails to Copy When Object Has No Data Members
How to Include Data Object Files (Images, etc.) in Program and Access the Symbols
How to Parse CSV Using Boost::Spirit
Dependent Name Resolution & Namespace Std/Standard Library