BitTorrent v2 Info Hash Encoding in Magnet Links

This article explains the encoding schemes used to represent 32-byte SHA-256 info hashes in BitTorrent v2 magnet links. It details the transition from the legacy SHA-1 algorithm to SHA-256 under the BitTorrent v2 specification (BEP 52), the role of the Multihash standard, and the exact representation formats used within modern magnet URIs.

BitTorrent v2 replaces the legacy 20-byte SHA-1 hash algorithm with the more secure 32-byte SHA-256 hash algorithm to identify torrents and verify data integrity.

In BitTorrent v2 magnet links, the SHA-256 info hash is encoded using Hexadecimal (Base16) representation formatted according to the Multihash specification.

The Multihash Format and Hexadecimal Encoding

Under BitTorrent Enhancement Proposal 52 (BEP 52), v2 magnet links use the exact topic parameter urn:btmh: (BitTorrent Multihash) rather than the legacy urn:btih: (BitTorrent Info Hash).

The value following urn:btmh: is a hexadecimal string constructed as follows:

  1. Hash Function Identifier: The Multihash header for SHA-256 is 0x12.
  2. Digest Length: The length of a SHA-256 output is 32 bytes (0x20 in hex).
  3. Digest Output: The 32-byte SHA-256 info hash itself (64 hexadecimal characters).

When serialized into the magnet link, the entire multihash (the 2-byte header plus the 32-byte hash) is represented as a 68-character hexadecimal string (case-insensitive, typically lowercase). It begins with the prefix 1220:

xt=urn:btmh:1220<64-character-sha256-hex-hash>

Base32 vs. Hexadecimal Across Versions