# Torrent 文件

Torrent 文件（元信息文件）是一个小型的 bencode 编码字典，描述一组用于通过 [BitTorrent](https://www.edgepedia.org/zh/bittorrent) 点对点协议分发的文件：它列出文件名、给出追踪器 URL，并为内容的每一个分块携带一个哈希值，以便任何下载的数据都能与原始数据校验。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> 文件本身不包含内容，只包含客户端寻找对等节点和校验其数据所需的元数据。<sup>[2](https://dl.acm.org/doi/pdf/10.1145/1269899.1254919)</sup>

| 键 | 详情 |
|---|---|
| 格式 | Bencode 编码字典，包含 `announce`（追踪器 URL）和 `info`（文件描述）等键<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> |
| 分块哈希 | `pieces` 键存放 20 字节的 SHA-1 哈希，每块一个<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> |
| 标识符 | infohash 是 bencode 编码的 `info` 值的 20 字节 SHA-1 哈希；v2 使用 SHA-256，仅在需要 20 字节标识符的用途中截断为 20 字节<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup><sup> • </sup><sup>[3](https://www.bittorrent.org/beps/bep_0052.html)</sup> |
| 常见分块大小 | 几乎总是 2 的幂，最常见的是 2<sup>18</sup> = 256 KiB<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> |
| v2 变更 | 为每个文件分别构建的、基于 16 KiB 块的 SHA-256 Merkle 树，当文件超过分块长度时使用 `piece layers` 字典<sup>[3](https://www.bittorrent.org/beps/bep_0052.html)</sup> |
| 磁力链接 | v1 SHA-1 infohash 使用 `urn:btih:`，v2 SHA-256 infohash 使用 `urn:btmh:`<sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup> |
| 私有标志 | 将 `private` 设为 1 会将对等节点发现限制在元信息文件中列出的追踪器<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup> |

## 结构与编码

.torrent 文件以 bencode 编码书写（BitTorrent 用于元数据的紧凑数据编码格式）。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> 顶层字典包含 `announce` 键，即追踪器的 URL，以及 `info` 键，即描述共享文件的嵌套字典。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup>

`info` 字典有两种形式，一种用于无目录结构的单文件 torrent，一种用于多文件 torrent。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup> 两者都包含 `piece length`（每个分块的字节大小）和 `pieces`（一个长度是 20 的倍数的字符串，细分为 20 字节的 SHA-1 哈希，对应索引处的分块各一个）。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> 分块数为 ceil(总长度 / 分块大小)。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup>

**多文件布局。** 在多文件 torrent 中，分块边界通过将文件数据视为一条连续长流来计算，即按 `files` 列表中列出的顺序拼接每个文件。因此分块可能跨越文件边界，客户端通过将已验证的分块按记录的偏移量切回原文件来重组文件。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup>

## 完整性与 infohash

内容的每一个分块都被哈希，客户端在将每个收到的分块标记为完成之前，会将其与元信息文件中对应的哈希进行校验。<sup>[2](https://dl.acm.org/doi/pdf/10.1145/1269899.1254919)</sup> Torrent 的标识符——*infohash*——是 `info` 值的 bencode 编码形式（作为元信息文件的子串取出）的 20 字节 SHA-1 哈希。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> Infohash 充当 torrent 的标识符：追踪器和分布式哈希表（DHT）以它索引 swarm，携带它的磁力链接可在对等节点发现机制可用时用于定位 swarm。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup><sup> • </sup><sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup>

在 BitTorrent v2 中，同样的构造对 info 字典使用 SHA-256，对于需要 20 字节标识符的用途，摘要被截断为 20 字节，因为追踪器和 DHT 协议是围绕 20 字节哈希构建的，没有空间容纳 32 字节。<sup>[3](https://www.bittorrent.org/beps/bep_0052.html)</sup><sup> • </sup><sup>[6](https://dim145.github.io/opentracker/guide/bittorrent-v2.html)</sup> 验证 infohash 的实现必须要么拒绝无效的元信息文件，要么直接提取 info 字典子串；不允许对无效数据做解码-编码往返，因为这会改变被哈希的字节。<sup>[3](https://www.bittorrent.org/beps/bep_0052.html)</sup>

## 分块大小的权衡

分块大小是真正的工程权衡，因为它决定了 swarm 中的并行度。<sup>[7](https://arxiv.org/pdf/0802.1015)</sup>

- **较小的分块**让对等节点更快获得并开始上传完整分块，减少检测到损坏时丢弃的数据，并将请求分散到更多对等节点。<sup>[8](https://imdl.io/book/bittorrent/piece-length-selection.html)</sup>
- **较大的分块**减少请求大量分块带来的协议开销，缩小元信息文件，并降低分块数量。<sup>[8](https://imdl.io/book/bittorrent/piece-length-selection.html)</sup>

元数据成本是具体的：16 kB 分块产生的元信息文件大于 120 kB，而 256 kB 分块则小于 10 kB，因为每个分块存储一个 20 字节哈希。<sup>[7](https://arxiv.org/pdf/0802.1015)</sup><sup> • </sup><sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup> 在内容大小为 1 到 100 MB、分块大小为 16 kB 到 2048 kB 的测试床实验中，对于 100 MB 文件，约 256 kB 的大小表现最佳，且最优分块大小随内容大小增加。<sup>[7](https://arxiv.org/pdf/0802.1015)</sup> 真实 swarm 相应扩展：实测分块大小从 16 KB 到 8092 KB 不等，高达 1 GB 的资源通常使用 512 到 1024 KB 分块，1 到 2 GB 使用 2048 KB，2 GB 以上通常为 4096 KB。<sup>[9](https://arxiv.org/pdf/1110.6265)</sup>

过大的代价是重新下载：大分块导致哈希失败，每次失败都必须重新获取整个分块，客户端文档建议除非理解后果，否则将分块大小设置保持默认。<sup>[10](https://documentation.help/uTorrent/AppendixA_04_03.html)</sup> 为兼容 v2，分块长度选为至少 16 KiB 的 2 的幂；libtorrent 将超过 128 MiB 的大小视为不合理且无效，Intermodal 工具将其自动选择上限设为 16 MiB，因为更大的长度曾导致客户端问题。<sup>[11](https://www.libtorrent.org/reference-Create_Torrents.html)</sup><sup> • </sup><sup>[8](https://imdl.io/book/bittorrent/piece-length-selection.html)</sup>

## BitTorrent v2 与混合 torrent

BitTorrent v2（BEP 52）将分块哈希函数改为 SHA-256，产生 32 字节哈希而非 20 字节。<sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup> 每个非空文件携带一个 `pieces root`：分支因子为 2 的 Merkle 树的根哈希，由文件的 16 KiB 块构建。<sup>[3](https://www.bittorrent.org/beps/bep_0052.html)</sup> 这些哈希树的叶子始终为 16 KiB，与分块大小无关，torrent 中存储的分块哈希是完整树的各分块子树的根。<sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup> `piece layers` 字典将每个文件的 Merkle 根映射到树中某一层哈希的拼接，该层一个哈希覆盖分块长度；如果缺少所需的分块层条目、哈希与 Merkle 根不匹配或来自错误的层，则 torrent 无效。<sup>[3](https://www.bittorrent.org/beps/bep_0052.html)</sup>

实际收益是块级验证：下载的数据可在 16 KiB 块级别检查，因此来自对等节点的损坏数据会立即被发现，只需重新下载 16 KiB，且能确定性地识别违规对等节点。<sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup> v2 文件根也仅取决于文件内容，与分块长度、私有标志及 torrent 中其他所有文件无关，因此同一发布在不同追踪器上的两次上传会得出相同的值。<sup>[6](https://dim145.github.io/opentracker/guide/bittorrent-v2.html)</sup>

**混合 torrent** 同时携带 v1 SHA-1 infohash 和 v2 SHA-256 infohash，形成两个 swarm 或一个隔离 swarm。<sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup> 支持两种格式的客户端可通过计算新旧 infohash 并下载到同一存储来加入两个 swarm。<sup>[3](https://www.bittorrent.org/beps/bep_0052.html)</sup>

## 磁力链接与元数据交换

磁力链接通过 infohash 而非文件来命名 torrent。v1 前缀为 `urn:btih:`，用于 SHA-1 infohash；v2 增加了 `urn:btmh:`，用于多哈希格式的完整 SHA-256 infohash，两字节前缀为 0x12 0x20。混合 torrent 的磁力链接同时携带 btih 和 btmh 哈希。<sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup> 由于 v2 通过将分块哈希移入 Merkle 树来保持 info 字典较小，磁力链接发起的下载在开始前需下载的字节更少，从而降低启动延迟。<sup>[4](https://blog.libtorrent.org/2020/09/bittorrent-v2/)</sup>

## 私有 torrent 与元数据字段

可选的 `private` 字段是一个整数；设为 1 时，客户端必须仅通过元信息文件中明确描述的追踪器公布其存在并获取对等节点，禁用对等节点交换和 DHT。该标志指示合规客户端遵守这些限制；它本身不强制追踪器成员资格。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup>

其他可选顶层键包括 `announce-list`（多个追踪器）、`creation date`（UNIX 纪元秒）、`comment`、`created by` 和 `encoding`。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup>

## 数据一览

- 分块长度几乎总是 2 的幂，最常见的是 2<sup>18</sup> = 256 KiB；BitTorrent 3.2 版之前使用 2<sup>20</sup> = 1 MiB 作为默认值。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup>
- 约 98% 的受分析 torrent 描述的资源在 0 GB 到 10 GB 之间，平均资源大小约 2 GB；最大的资源约 28 个，为 28.5 GB 或更大，通常是软件。<sup>[9](https://arxiv.org/pdf/1110.6265)</sup>
- 一次大规模爬取在公共生态系统中识别出 460 万个 torrent 和 38,996 个追踪器，其中仅 728 个追踪器活跃；[海盗湾](https://www.edgepedia.org/zh/the-pirate-bay)追踪了 90% 的活跃 torrent，82% 的 torrent 少于 10 个对等节点。<sup>[12](https://www.academia.edu/61218705/Unraveling_the_BitTorrent_Ecosystem)</sup>
- 历史上，分块大小的选择使 .torrent 文件保持在约 50 到 75 kB。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup>

关于大型发布的推荐分块大小，指南有所不同。社区规范建议对于约 8 到 10 GB 的 torrent 将分块大小保持在 512 kB 或以下，接受更大的 .torrent 文件。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup> 测量研究发现实际部署的 swarm 使用大得多的分块，1 到 2 GB 资源为 2048 KB，2 GB 以上为 4096 KB。<sup>[9](https://arxiv.org/pdf/1110.6265)</sup>

## 近期发展

客户端对 v2 的支持已在 libtorrent 2.x 世代中整合。libtorrent 2.0 在新的 `info_hash_t` 类型中表示每个 torrent 的两个 infohash（一个 SHA-1，一个 SHA-256）；混合 torrent 按每个 info-hash 通告一次，追踪器结果按 BitTorrent 版本报告。其 `create_torrent` 类默认创建混合 torrent。<sup>[13](https://www.libtorrent.org/upgrade_to_2.0-ref.html)</sup> qBittorrent 在 PR #13487 中增加了创建 v2 torrent 的支持，仅在针对 libtorrent 2.0 或更高版本构建时可用；评审讨论指出，在设置默认值之前，应针对 V2、V1 和混合 torrent 测试追踪器上传脚本。<sup>[14](https://github.com/qbittorrent/qBittorrent/pull/13487)</sup>

2023 年后 libtorrent 的一项变更增加了 `settings_pack::hybrid_only_validates_v2`，允许混合 torrent 仅验证 v2 SHA-256 Merkle 分块哈希并跳过 v1 SHA-1 验证，减少哈希 CPU 和磁盘回读开销。<sup>[15](https://github.com/arvidn/libtorrent/pull/8217)</sup> 格式工作超越 v2 继续进行：Tixati 客户端的协议版本 3 规范定义了哈希算法说明符，如 SHA3-256-18 和 SHA2-256-23，其中最终输出哈希必须以指定数量的零位开头。<sup>[16](https://tixati.com/specs/bittorrent/v3)</sup>

## 创建与实践

创建 torrent 意味着对内容进行哈希并构建元信息文件。libtorrent 要求分块大小为 2 的幂且最小 16 KiB，若给定 0 则自动设置大小，并将超过 128 MiB 的大小视为不合理而拒绝。<sup>[11](https://www.libtorrent.org/reference-Create_Torrents.html)</sup>

下载时，客户端使用元信息文件的分块哈希验证收到的数据<sup>[2](https://dl.acm.org/doi/pdf/10.1145/1269899.1254919)</sup>，并从 `announce` 中指定的追踪器获取对等节点。<sup>[1](https://www.bittorrent.org/beps/bep_0003.html)</sup> 获得对等节点列表后，新加入的对等节点联系约 20 到 40 个对等节点形成其邻居集，根据需要重新连接以维持至少 20 个邻居。<sup>[17](https://repository.hkust.edu.hk/ir/bitstream/1783.1-6529/1/SurveyofBitTorrent.pdf)</sup> 已验证的分块随后按其偏移量写入，在多文件情况下，沿上述连续流切回各个文件。<sup>[5](https://wiki.theory.org/BitTorrentSpecification)</sup>

## References

1. [BEP 3: The BitTorrent Protocol Specification, bittorrent.org](https://www.bittorrent.org/beps/bep_0003.html)
2. [Clustering and Sharing Incentives in BitTorrent Systems, SIGMETRICS/Performance](https://dl.acm.org/doi/pdf/10.1145/1269899.1254919)
3. [BEP 52: The BitTorrent Protocol Specification v2, bittorrent.org](https://www.bittorrent.org/beps/bep_0052.html)
4. [BitTorrent v2, libtorrent blog (Arvid Norberg, 2020)](https://blog.libtorrent.org/2020/09/bittorrent-v2/)
5. [BitTorrentSpecification, wiki.theory.org (community specification)](https://wiki.theory.org/BitTorrentSpecification)
6. [BitTorrent v2 (BEP 52), Trackarr guide](https://dim145.github.io/opentracker/guide/bittorrent-v2.html)
7. [Understanding the BitTorrent Piece Size Trade-off (arXiv)](https://arxiv.org/pdf/0802.1015)
8. [Piece Length Selection, Intermodal (imdl) book](https://imdl.io/book/bittorrent/piece-length-selection.html)
9. [Understanding BitTorrent Through Real Measurements (arXiv)](https://arxiv.org/pdf/1110.6265)
10. [Create New Torrent, µTorrent User Manual](https://documentation.help/uTorrent/AppendixA_04_03.html)
11. [libtorrent reference: Create Torrents](https://www.libtorrent.org/reference-Create_Torrents.html)
12. [Unraveling the BitTorrent Ecosystem](https://www.academia.edu/61218705/Unraveling_the_BitTorrent_Ecosystem)
13. [Upgrade to 2.0, libtorrent reference documentation](https://www.libtorrent.org/upgrade_to_2.0-ref.html)
14. [Add support for creating v2 torrents, qBittorrent PR #13487](https://github.com/qbittorrent/qBittorrent/pull/13487)
15. [Add feature to only validate the v2 hashes for hybrid torrents, libtorrent PR #8217](https://github.com/arvidn/libtorrent/pull/8217)
16. [Tixati BitTorrent Protocol Version 3 specification](https://tixati.com/specs/bittorrent/v3)
17. [A Survey of BitTorrent Systems, HKUST](https://repository.hkust.edu.hk/ir/bitstream/1783.1-6529/1/SurveyofBitTorrent.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization › Text, encodings, and archives

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/torrent-file

English original: https://www.edgepedia.org/torrent-file
