Problem creating info hash of torrent file

时间秒杀一切 提交于 2019-12-23 16:21:13

问题


I'm creating a BitTorrent site.

If a user uploads a .torrent file I need to get the info hash to get more info from the tracker.

However I cannot seem to get the correct info hash from the file.

I've download the a .torrent ( http://www.mininova.org/get/2886852 ) from mininova.

According the mininova the info hash should be: 6a7eb42ab3b9781eba2d9ff3545d9758f27ec239 ( http://www.mininova.org/det/2886852 ). However when I try to create the info hash of the file I get the following: 3d05f149e604b1efaa0ed554a31e693755de7cb0

I don't have any clue as to why I cannot get the correct info hash.

If I understood correctly I have to create the hash from the info section of the torrent data.

The relevant code:

$bencode = new BencodeModel();
$data = $bencode->decode_file($form->fields['filename']->saved_file);
$hash = $torrentmanager->create_hash($data['info']);

The BencodeModel (too long to post here): http://pastebin.com/Zc5i94DQ

The create hash function:

function create_hash($info)
{
    $bencode = new BencodeModel();
    return urlencode(sha1($bencode->encode($info)));
}

I'm totally in the dark where I go wrong. Any help is much appreciated!

If you need more info just tell me and I'll update with relevant info.

EDIT

As requested the data for sha1:

var_dump($bencode->encode($info));

http://pastebin.com/HiQgRX6M

EDIT

This is getting more strange.

I've deployed the site to the live server (which runs on Linux) and the hashing works there.

However on my dev machine (Windows) it still doesn't work.

I've already tried replaced linebreaks/carriage returns.

Any ideas?


回答1:


I was able to get the code to run on both Windows XP and 7 with PHP 5.3.x and get the correct hash. I'm guessing that the .torrent you're loading on Windows is different to the one you've loaded on Linux (possibly encoding issues).

Try running this code and see if you get the SHA1 hash 148251317dae971fcd5a5dcc5d4bde3d85130c8f echoed:

echo sha1(file_get_contents('your.torrent'));

which I'll assume would be:

echo sha1(file_get_contents($form->fields['filename']->saved_file));

If you get a different hash, then the file you're loading is not right.




回答2:


The hash in the torrent file cannot be the hash of the file. Think about it.... The hash is based on its contents, and you can't know what the hash is in advance. So calculating the hash of the file, then embedding it in the file changes the hash of the file, invalidating the hash you just embedded.

The hash in a .torrent file is based on the contents of the file, but not the entire contents.

From the BT spec:

info_hash
    The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. Note that this is a substring of the metainfo file. This value will almost certainly have to be escaped.


来源:https://stackoverflow.com/questions/6527357/problem-creating-info-hash-of-torrent-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!