Reputation: 1467
I am studying the file transfer using bittorrent protocol and is interested to know whether the piece index
associated with each piece has any significance in the rearrangement of the pieces. Howe is this piece index
assigned? Also is their some other way apart from using the hash value in .torrent
file to rearrange the pieces?
Upvotes: 1
Views: 1027
Reputation: 9225
The metadata of a torrent includes sufficient information to reassemble and verify the data the torrent describes regardless of the order the data arrives. The full definition can be found here, and for your question there are some important parts.
info This maps to a dictionary, with keys described below.
and
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.
and
For the purposes of the other keys, the multi-file case is treated as only having a single file by concatenating the files in the order they appear in the files list.
and finally
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).
Taken all together. There is an assumption that the underlying data that a .torrent describes is in order from piece 0 to piece NumPieces. All of the pieces will be exactly the same length, expect for possibly the last one and thus the piece_index X piece_size will give the offset in bytes from the beginning to where that piece fits in the data. Multiple files are treated as if they're all concatenated together. Each piece hash is located at piece_index X 20 bytes from the beginning of pieces. Now since pieces is located in the info section, if you rearrange the pieces by changing their index, this will change the SHA1 hash of the info section and thus the infoHash, which changes the torrent. This is by design so the authenticity of the data is can be validated. Practically speaking, it would be impossible to forge or accidentally reassemble pieces in the wrong order because the infoHash is the final check that pieces is correct and pieces is the check that the data is correct.
Upvotes: 1