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));
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?
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: