What Is a Torrent Info Hash and How to Calculate It
An info hash is the unique identifier used across the BitTorrent
protocol to distinguish specific torrents and locate peers sharing
identical data. This article explains what an info hash is, its role in
peer-to-peer networking, and the exact step-by-step process of
calculating it from the info dictionary inside a
.torrent metainfo file.
What Is a Torrent Info Hash?
In the BitTorrent network, an info hash serves as a digital fingerprint for a specific download. When you add a torrent file or magnet link to a BitTorrent client, the client uses the info hash to query trackers and the Distributed Hash Table (DHT) to find peers who are uploading (seeding) or downloading (leeching) the exact same files.
Because it represents the underlying data structure rather than metadata like tracker URLs or creation dates, two users creating a torrent with the exact same files, chunk sizes, and layout will generate the identical info hash.
The Structure of a Metainfo File
A standard .torrent file contains data encoded in
Bencode, a lightweight serialization format used by
BitTorrent. At its root, a .torrent file is a Bencoded
dictionary containing several keys, such as:
announce: The URL of the primary tracker.creation date: The timestamp when the torrent was generated.comment: Optional notes added by the creator.info: A nested dictionary containing the essential payload structure.
The info dictionary holds the critical details of the
actual files being shared, including file names, total sizes, path
structures, the piece length (chunk size), and a concatenated string of
SHA-1 hashes for every individual chunk of the data.
How the Info Hash Is Calculated
The info hash is not calculated over the entire .torrent
file. Instead, it is calculated exclusively from the binary
representation of the info dictionary.
The calculation follows these steps:
1. Locate and Extract
the info Dictionary
The parser reads the Bencoded root dictionary of the
.torrent file and extracts the exact raw bytes
corresponding to the info key and its value.
2. Preserve Raw Bencoded Bytes
The extracted info section must remain in its original
Bencoded format. Decoding the info dictionary into an
in-memory object (such as a JSON object or a Python dictionary) and then
re-encoding it can alter key ordering or spacing, which changes the
resulting hash. The hash must be computed against the canonical, raw
Bencoded byte sequence.
3. Compute the Cryptographic Hash
- BitTorrent v1: Apply the SHA-1
cryptographic hash algorithm to the raw Bencoded
infobytes. The output is a 20-byte (160-bit) binary value. - BitTorrent v2: Apply the SHA-256
hash algorithm to the Bencoded
infodictionary. The output is a 32-byte (256-bit) binary value.
4. Format the Hash Representation
While clients use the raw binary hash internally when communicating
with trackers and DHT nodes, the hash is formatted for human readability
and magnet links into: * Hexadecimal: A 40-character
hexadecimal string (e.g., 4a5b6c...). *
Base32: A 32-character string, commonly used inside
magnet URIs (e.g., magnet:?xt=urn:btih:...).
By isolating the info dictionary from external metadata
like tracker lists, BitTorrent ensures that swarms can merge and peers
can connect regardless of which trackers they use to discover each
other.