Torrent File Structure and Bencoding
Torrent File
A torrent file is a small file that contains metadata about the file(s) that you want to download.
Key components of torrent file:
File Metadata: Information about the files included in torrent such as names, sizes and number of files in the package.
Tracker Information: URLs of the trackers that help in the transfer between the peers.
Piece Info: Details about the how the file is split into chunks, including hash for each piece.
Torrent File Structure
Torrent file hold following information:
Announce: The URL of the tracker that helps in finding the peers.
Info: A dictionary containing the metadata about the files. This is the most critical part of the torrent file and includes:
Name: The name of the file or folder.
Piece Length: Size of each piece in bytes.
Pieces: A string that contains the concatenated SHA-1 hashes of all the pieces.
Files: A list (if multiple files) that includes names and sizes.
Creation Date: The timestamp when the torrent file was created.
Comment: Any additional comment about the author or the content.
Benconding: The Encoding Method
Torrent file uses custom encoding called as Bencoding(pronounced Bee-encoding) to encode the information present in the torrent files. It supports four data types:
Strings: Represented by their length followed by a colon and the string itself (e.g.,
4:abcd
).Integers: Represented by an ‘i’, followed by the integer, and ending with an ‘e’ (e.g.,
i42e
).Lists: Represented by an ‘l’ at the beginning and ‘e’ at the end, containing Bencoded elements (e.g.,
l4:abcd4:efghe
).Dictionaries: Represented by a ‘d’ at the beginning and ‘e’ at the end, containing key-value pairs (e.g.,
d4:key1i1e4:key2i2ee
).
References
Arpit Bhayani’s BitTorrent Internal Series
https://www.bittorrent.org/beps/bep_0003.html