创建 torrent 文件的信息哈希时出现问题
我正在创建一个 BitTorrent 网站。
如果用户上传 .torrent 文件,我需要获取信息哈希以从跟踪器获取更多信息。
但是我似乎无法从文件中获取正确的信息哈希。
我已经下载了 .torrent ( http://www.mininova.org/get/2886852 ) 来自 mininova。
根据 mininova,信息哈希应该是: 6a7eb42ab3b9781eba2d9ff3545d9758f27ec239 ( http://www.mininova.org/det/2886852< /a>)。但是,当我尝试创建文件的信息哈希时,我得到以下信息: 3d05f149e604b1efaa0ed554a31e693755de7cb0
我不知道为什么我无法获得正确的信息哈希。
如果我理解正确的话,我必须从 torrent 数据的信息部分创建哈希。
相关代码:
$bencode = new BencodeModel();
$data = $bencode->decode_file($form->fields['filename']->saved_file);
$hash = $torrentmanager->create_hash($data['info']);
BencodeModel(太长,无法在此处发布):http://pastebin.com/Zc5i94DQ
创建哈希函数:
function create_hash($info)
{
$bencode = new BencodeModel();
return urlencode(sha1($bencode->encode($info)));
}
我完全不知道哪里出错了。非常感谢任何帮助!
如果您需要更多信息,请告诉我,我将更新相关信息。
编辑
根据要求sha1的数据:
var_dump($bencode->encode($info));
< strong>编辑
这变得越来越奇怪了。
我已将该站点部署到实时服务器(在 Linux 上运行),并且散列在那里工作。
但是在我的开发机器(Windows)上它仍然无法工作。
我已经尝试过替换换行符/回车符。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够使用 PHP 5.3.x 使代码在 Windows XP 和 7 上运行并获得正确的哈希值。我猜测您在 Windows 上加载的 .torrent 与您在 Linux 上加载的 .torrent 不同(可能是编码问题)。
尝试运行此代码,看看您是否获得 SHA1 哈希
148251317dae971fcd5a5dcc5d4bde3d85130c8f
回显:我假设是:
如果您获得不同的哈希,则您加载的文件不正确。
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:which I'll assume would be:
If you get a different hash, then the file you're loading is not right.
torrent 文件中的哈希值不能是该文件的哈希值。想想看......哈希值是基于其内容的,你无法提前知道哈希值是什么。因此,计算文件的哈希值,然后将其嵌入到文件中会更改文件的哈希值,从而使刚刚嵌入的哈希值无效。
.torrent 文件中的哈希值基于文件的内容,而不是全部内容。
来自 BT 规范:
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: