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:
- Hash Function Identifier: The Multihash header for
SHA-256 is
0x12. - Digest Length: The length of a SHA-256 output is 32
bytes (
0x20in hex). - 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
- BitTorrent v1 (SHA-1): Magnet links
(
urn:btih:) historically supported either 40-character Hexadecimal strings or 32-character Base32 (RFC 4648) strings for 20-byte hashes. - BitTorrent v2 (SHA-256): Standardized directly on
Hexadecimal encoding for the
urn:btmh:multihash scheme. While some client implementations allow standard Base32 parsing for backward compatibility in hybrid environments, standard BitTorrent v2 magnet links strictly use the hexadecimal Multihash format.