3

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?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • It would be helpful to also tell us what you end up feeding into `sha1`. – Jon Jun 29 '11 at 21:21
  • @hakre: because a torrent can have only 1 info hash and it is used to `scrape` for the info. And when I manually try to get the info using that hash I get the info I need: http://tracker.mininova.org/scrape?info_hash=6a7eb42ab3b9781eba2d9ff3545d9758f27ec239 – PeeHaa Jun 29 '11 at 21:42
  • @hakre: http://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol . And thanks for trying to help me out thus far. – PeeHaa Jun 29 '11 at 22:04
  • Does `$form->fields['filename']->saved_file` really return the file contents? I don't remember uploading in PHP having that syntax. – fent Aug 08 '11 at 13:14
  • @DeadEnD: I use my own custom framework. `$form->fields['filename']->saved_file` returns the filename which I use as a param for `$bencode->decode_file()` which reads and decoded the contents of the file – PeeHaa Aug 08 '11 at 14:58
  • Could you provide a pastebin of $data['info']? and why do you urlencode() the hash? – fent Aug 10 '11 at 11:43
  • @DeaDEnD: http://www.2shared.com/document/FCnaIgAO/info.html I thought it has to be URL encoded? – PeeHaa Sep 24 '11 at 16:51
  • I can't figure out why it isn't working. Try looking at http://wiki.theory.org/BitTorrentSpecification#Info_Dictionary and see if you missed anything. And shah1 returns a hex string, `urlencode` will return the same string it was given. – fent Sep 25 '11 at 08:50

2 Answers2

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.

Philip Chen
  • 1,408
  • 1
  • 11
  • 7
0

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.
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • There is no hash in the torrent file. I'm trying to calculate the hash from the info within the torrent file. – PeeHaa Jun 29 '11 at 22:02
  • @PeeHaa: Carefully read the specs about what and in which encoding you need to create the hash of. Take a look into the bencode class if it really does what you expect. I'm pretty sure the error is rooted therein. – hakre Jun 29 '11 at 22:11