[英]Problem creating info hash of torrent file
我正在创建一个 BitTorrent 网站。
如果用户上传 a.torrent 文件,我需要获取信息 hash 以从跟踪器获取更多信息。
但是我似乎无法从文件中获得正确的信息 hash。
我已经从 mininova 下载了 a.torrent ( http://www.mininova.org/get/2886852 )。
根据 mininova 的信息 hash 应该是:6a7eb42ab3b9781eba2d9ff3545d9758f27ec239( http://www.mininova.org/det/2886852 )。 但是,当我尝试创建文件的信息 hash 时,我得到以下信息: 3d05f149e604b1efaa0ed554a31e693755de7cb0
我不知道为什么我无法获得正确的信息 hash。
如果我理解正确,我必须从 torrent 数据的信息部分创建 hash。
相关代码:
$bencode = new BencodeModel();
$data = $bencode->decode_file($form->fields['filename']->saved_file);
$hash = $torrentmanager->create_hash($data['info']);
BencodeModel(此处无法发布): http://pastebin.com/Zc5i94DQ
创建 hash function:
function create_hash($info)
{
$bencode = new BencodeModel();
return urlencode(sha1($bencode->encode($info)));
}
我完全不知道 go 错了。 任何帮助深表感谢!
如果您需要更多信息,请告诉我,我会更新相关信息。
编辑
根据要求提供 sha1 的数据:
var_dump($bencode->encode($info));
编辑
这越来越奇怪了。
我已经将该站点部署到实时服务器(在 Linux 上运行),并且在那里进行哈希处理。
但是在我的开发机器(Windows)上它仍然不起作用。
我已经尝试过替换换行符/回车。
有任何想法吗?
我能够使用 PHP 5.3.x 在 Windows XP 和 7 上运行代码,并获得正确的 hash。 我猜你在 Windows 上加载的 .torrent 与你在 Linux 上加载的不同(可能是编码问题)。
尝试运行此代码,看看是否得到 SHA1 hash 148251317dae971fcd5a5dcc5d4bde3d85130c8f
回显:
echo sha1(file_get_contents('your.torrent'));
我假设是:
echo sha1(file_get_contents($form->fields['filename']->saved_file));
如果您得到不同的 hash,那么您正在加载的文件不正确。
Torrent文件中的hash不能是文件的hash。 想一想.... hash 是根据它的内容,而你无法提前知道 hash 是什么。 所以计算文件的hash,然后嵌入到文件中会改变文件的hash,使你刚才嵌入的hash失效。
.torrent 文件中的 hash 是基于文件的内容,而不是全部内容。
从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.