How BitTorrent Verifies Magnet Link Metadata

Magnet links allow BitTorrent clients to initiate downloads without downloading a .torrent file first, relying instead on a cryptographic identifier known as an info hash. The BitTorrent protocol guarantees that metadata fetched from untrusted peers has not been tampered with by hashing the received metadata payload and comparing it against the trusted info hash embedded within the original magnet link. If the computed hash does not perfectly match the hash from the link, the client discards the data, preventing malicious or corrupted metadata from compromising the swarm.

A standard magnet link contains a Uniform Resource Name (URN) defined by the xt (exact topic) parameter. This parameter holds the cryptographic hash of the .torrent file’s info dictionary—the core metadata block defining the file structure, piece sizes, and individual piece hashes.

For BitTorrent v1, this identifier is a 40-character hexadecimal string representing a SHA-1 hash (BTIH, or BitTorrent Info Hash). In BitTorrent v2, it uses a SHA-256 hash. Because the magnet link is obtained from an external, trusted source (such as a trusted indexing website), the client treats this hash as the authoritative reference for the swarm.

Fetching the Metadata via Extension Protocols

Because a magnet link lacks the actual file list and piece hashes, the client must retrieve this information directly from peers in the swarm. This process is governed by two BitTorrent Enhancement Proposals (BEPs):

  1. BEP 10 (Extension Protocol): Allows clients to advertise custom capabilities to connected peers.
  2. BEP 9 (Extension for Transfer of Metadata Files): Defines how clients request and transfer the raw info dictionary (ut_metadata).

When a client connects to peers discovered via the Distributed Hash Table (DHT) or peer exchange (PEX), it requests the metadata in small, indexed slices.

The Cryptographic Verification Process

To ensure that untrusted peers have not modified file paths, piece hashes, or file sizes, the client performs a strict cryptographic validation process:

  1. Assembly: The client downloads all chunks of the info dictionary from one or multiple peers until the full metadata payload is assembled.
  2. Hashing: The client calculates the cryptographic hash (SHA-1 for v1 or SHA-256 for v2) of the exact, raw bencoded byte stream of the assembled info dictionary.
  3. Comparison: The client compares the calculated hash against the info hash specified in the magnet link.
  4. Validation or Rejection:
    • If the hashes match, the metadata is mathematically proven to be authentic and identical to the original creator’s specification. The client parses the metadata and begins requesting the actual content files.
    • If the hashes mismatch, the client immediately rejects the data, drops the faulty metadata, and attempts to re-fetch the metadata pieces from different peers.

BitTorrent v2 and Merkle Trees

In BitTorrent v2 (BEP 52), integrity verification is reinforced using SHA-256 and Merkle hash trees. Instead of only checking a flat list of hashes, the metadata contains the root hash of a Merkle tree for each file. This allows clients to verify individual 16 KiB blocks of data as they arrive, providing granular protection against corrupt or malicious data injections during both the metadata exchange and the subsequent file downloads.