简体   繁体   English

创建 torrent 文件的信息 hash 时出现问题

[英]Problem creating info hash of torrent file

I'm creating a BitTorrent site.我正在创建一个 BitTorrent 网站。

If a user uploads a.torrent file I need to get the info hash to get more info from the tracker.如果用户上传 a.torrent 文件,我需要获取信息 hash 以从跟踪器获取更多信息。

However I cannot seem to get the correct info hash from the file.但是我似乎无法从文件中获得正确的信息 hash。

I've download the a.torrent ( http://www.mininova.org/get/2886852 ) from mininova.我已经从 mininova 下载了 a.torrent ( http://www.mininova.org/get/2886852 )。

According the mininova the info hash should be: 6a7eb42ab3b9781eba2d9ff3545d9758f27ec239 ( http://www.mininova.org/det/2886852 ).根据 mininova 的信息 hash 应该是:6a7eb42ab3b9781eba2d9ff3545d9758f27ec239( http://www.mininova.org/det/2886852 )。 However when I try to create the info hash of the file I get the following: 3d05f149e604b1efaa0ed554a31e693755de7cb0但是,当我尝试创建文件的信息 hash 时,我得到以下信息: 3d05f149e604b1efaa0ed554a31e693755de7cb0

I don't have any clue as to why I cannot get the correct info hash.我不知道为什么我无法获得正确的信息 hash。

If I understood correctly I have to create the hash from the info section of the torrent data.如果我理解正确,我必须从 torrent 数据的信息部分创建 hash。

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 BencodeModel(此处无法发布): http://pastebin.com/Zc5i94DQ

The create hash function:创建 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.我完全不知道 go 错了。 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:根据要求提供 sha1 的数据:

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

http://pastebin.com/HiQgRX6M 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.我已经将该站点部署到实时服务器(在 Linux 上运行),并且在那里进行哈希处理。

However on my dev machine (Windows) it still doesn't work.但是在我的开发机器(Windows)上它仍然不起作用。

I've already tried replaced linebreaks/carriage returns.我已经尝试过替换换行符/回车。

Any ideas?有任何想法吗?

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.我能够使用 PHP 5.3.x 在 Windows XP 和 7 上运行代码,并获得正确的 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).我猜你在 Windows 上加载的 .torrent 与你在 Linux 上加载的不同(可能是编码问题)。

Try running this code and see if you get the SHA1 hash 148251317dae971fcd5a5dcc5d4bde3d85130c8f echoed:尝试运行此代码,看看是否得到 SHA1 hash 148251317dae971fcd5a5dcc5d4bde3d85130c8f回显:

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.如果您得到不同的 hash,那么您正在加载的文件不正确。

The hash in the torrent file cannot be the hash of the file. Torrent文件中的hash不能是文件的hash。 Think about it.... The hash is based on its contents, and you can't know what the hash is in advance.想一想.... hash 是根据它的内容,而你无法提前知道 hash 是什么。 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.所以计算文件的hash,然后嵌入到文件中会改变文件的hash,使你刚才嵌入的hash失效。

The hash in a.torrent file is based on the contents of the file, but not the entire contents. .torrent 文件中的 hash 是基于文件的内容,而不是全部内容。

From the BT spec :BT 规范

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM