如何使用 JS 或节点 JS 从文件中提取片段信息

How to extract pieces info from a file using JS or node JS

作为我自学过程的一部分,我正在尝试编写一个从文件创建 torrent 的程序。 所以我打开了一个种子文件。我从中得到了这个信息。我想知道什么是“碎片”。以及如何从文件中提取这些“片段”信息。

{
      "infoHash": "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
      "name": "nothing.epub",
      "encoding": "UTF-8",
      "created": 1375363666,
      "createdBy": "uTorrent/3300",
      "comment": "Torrent downloaded from torrent cache at http://itorrents.org",
      "announce": [
        "http://tracker.example.com/announce"
      ],
      "urlList": [],
      "files": [
        {
          "path": "nothing.epub",
          "length": 362017
        }
      ],
      "pieceLength": 16384,
      "pieces": [
        "1f9c3f59beec079715ec53324bde8569e4a0b4eb",
        "ec42307d4ce5557b5d3964c5ef55d354cf4a6ecc",
        "7bf1bcaf79d11fa5e0be06593c8faafc0c2ba2cf",
        "76d71c5b01526b23007f9e9929beafc5151e6511",
        "0931a1b44c21bf1e68b9138f90495e690dbc55f5",
        "72e4c2944cbacf26e6b3ae8a7229d88aafa05f61",
        "eaae6abf3f07cb6db9677cc6aded4dd3985e4586",
        "27567fa7639f065f71b18954304aca6366729e0b",
        "4773d77ae80caa96a524804dfe4b9bd3deaef999",
        "c9dd51027467519d5eb2561ae2cc01467de5f643",
        "0a60bcba24797692efa8770d23df0a830d91cb35",
        "b3407a88baa0590dc8c9aa6a120f274367dcd867",
        "e88e8338c572a06e3c801b29f519df532b3e76f6",
        "70cf6aee53107f3d39378483f69cf80fa568b1ea",
        "c53b506159e988d8bc16922d125d77d803d652c3",
        "ca3070c16eed9172ab506d20e522ea3f1ab674b3",
        "f923d76fe8f44ff32e372c3b376564c6fb5f0dbe",
        "52164f03629fd1322636babb2c014b7dae582da4",
        "1363965261e6ce12b43701f0a8c9ed1520a70eba",
        "004400a267765f6d3dd5c7beb5bd3c75f3df2a54",
        "560a61801147fa4ec7cf568e703acb04e5610a4d",
        "56dcc242d03293e9446cf5e457d8eb3d9588fd90",
        "c698de9b0dad92980906c026d8c1408fa08fe4ec"
      ]

来自BEP-3

piece length maps to the number of bytes in each piece the file is split into. For the purposes of transfer, files are split into fixed-size pieces which are all the same length except for possibly the last one which may be truncated. piece length is almost always a power of two, most commonly 2 18 = 256 K (BitTorrent prior to version 3.2 uses 2 20 = 1 M as default).

pieces maps to a string whose length is a multiple of 20. It is to be subdivided into strings of length 20, each of which is the SHA1 hash of the piece at the corresponding index.

要计算文件的片段哈希值,您需要打开文件,读入“片段长度”字节并计算该片段的 SHA1 哈希值。继续这样做,直到 运行 进入文件末尾。

如果文件不是片段长度的精确倍数,则允许最终片段小于片段大小。

哦,对于多文件 torrent,您需要将所有文件视为一个连续文件,以便分段。这意味着,例如,如果您有两个文件,一个 100 字节,一个 500000 字节,则第一部分将是第一个文件的 100 字节与第二个文件的 (piece length - 100) 字节连接。

了解这一点,您可以使用类似这样的代码从文件中获取第一部分:

var fs = require('fs');
var crypto = require('crypto');

var pieceLength = 16384;
var filename = 'nothing.epub';
const stream = fs.createReadStream(filename, { start: 0, end: pieceLength - 1 });
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
stream.on('end', function() {
    hash.end();
    console.log(hash.read());
})
stream.pipe(hash);