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


Problem creating info hash of torrent file

我正在创建一个BitTorrent网站。

如果用户上传了一个.torrent文件,我需要获得信息哈希,才能从跟踪器中获得更多信息。

然而,我似乎无法从文件中获得正确的信息哈希。

我已经下载了a.torrent(http://www.mininova.org/get/2886852)。

根据mininova的说法,信息哈希应该是:6a7eb42ab3b9781eba2d9ff3545d9758f27ec239(http://www.mininova.org/det/2886852)。然而,当我尝试创建文件的信息哈希时,我得到了以下信息:3d05f149e604b1efaaoed554a31e693755de7cb0

我不知道为什么我不能得到正确的信息散列。

如果我理解正确,我必须从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));

http://pastebin.com/HiQgRX6M

编辑

这越来越奇怪了。

我已经将该站点部署到了实时服务器(在Linux上运行)上,并且在那里进行了哈希处理。

然而,在我的开发机器(Windows)上,它仍然不起作用。

我已经试过更换换行符/回车符。

有什么想法吗?

我能够使用PHP 5.3.x在Windows XP和7上运行代码,并获得正确的哈希。我猜你在Windows上加载的.trent与在Linux上加载的不同(可能是编码问题)。

试着运行这个代码,看看你是否得到了SHA1散列148251317dae971fcd5a5dcc5d4bde3d85130c8f回显:

echo sha1(file_get_contents('your.torrent'));

我想应该是:

echo sha1(file_get_contents($form->fields['filename']->saved_file));

如果你得到一个不同的散列,那么你加载的文件是不对的。

torrent文件中的哈希不能是该文件的哈希。想想看……哈希是基于其内容的,你无法提前知道哈希是什么。因此,计算文件的哈希,然后将其嵌入到文件中,会更改文件的哈希值,从而使刚才嵌入的哈希值无效。

.torrent文件中的哈希基于文件的内容,而不是整个内容。

来自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.