How to convert torrent info hash for scrape?

▼魔方 西西 提交于 2019-11-30 23:10:01

It's a raw hexadecimal representation. Use pack() with H to convert it. Then URL encode it.

Got this python snippet from a colleague,

r = ''
s = 'fda164e7af470f83ea699a529845a9353cc26576'
for n in range(0, len(s), 2):
    r += '%%%s' % s[n:n+2].upper()
print r

Output: %FD%A1%64%E7%AF%47%0F%83%EA%69%9A%52%98%45%A9%35%3C%C2%65%76

Works like a charm.

Edit: Works like a charm for getting the tracker to give back status 200 (ok) but still doesn't work for retrieving the torrent details...

In case someone is having trouble and comes across this thread in the future: the trick to this whole issue is to use the bool $raw_output argument of the PHP: sha1 function, setting it to "true".

The BDecode/DEncode classes can be found HERE. This project, called Trackon, also includes many other helpful classes to interact with torrent trackers and files.

So, in PHP, something like this will work to obtain the correct info hash for scraping the tracker for details:

include('./path/to/BDecode.php');
include('./path/to/BEncode.php');

function getHash($torFile){
    $tfile = BDecode(file_get_contents($torFile));
    $infohash = sha1(BEncode($tfile["info"]), TRUE);

    return urlencode($infohash);
}

Then merely call it like so:

$hash = getHash('./path/to/.torrent');

Hope this helps someone out there. I was still scratching my head after reading many posts about how to obtain the correct info hash. I understand why this wasn't mentioned anywhere now though, this argument was added in PHP 5. If you're not running PHP 5, you will have to convert the sha1 hash to raw binary after you calculate it.

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