Calculating the info-hash of a torrent file(计算种子文件的信息哈希)
问题描述
我正在使用 C++ 来解析 Torrent 文件的信息哈希值,但与此站点相比,我无法获得正确"的哈希值:
I'm using C++ to parse the info hash of a torrent file, and I am having trouble getting a "correct" hash value in comparison to this site:
http://i-tools.org/torrent
我构建了一个非常简单的玩具示例,以确保我掌握了正确的基础知识.
I have constructed a very simple toy example just to make sure I have the basics right.
我在 sublime 中打开了一个 .torrent 文件并剥离了除信息字典之外的所有内容,所以我有一个如下所示的文件:
I opened a .torrent file in sublime and stripped off everything except for the info dictionary, so I have a file that looks like this:
d6:lengthi729067520e4:name31:ubuntu-12.04.1-desktop-i386.iso12:piece lengthi524288e6:pieces27820:¡´E¶ˆØËš3í ..............(more unreadable stuff.....)..........
我读入这个文件并用这个代码解析它:
I read this file in and parse it with this code:
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <openssl/sha.h>
void printHexRep(const unsigned char * test_sha) {
std::cout << "CALLED HEX REP...PREPPING TO PRINT!
";
std::ostringstream os;
os.fill('0');
os << std::hex;
for (const unsigned char * ptr = test_sha; ptr < test_sha + 20; ptr++) {
os << std::setw(2) << (unsigned int) *ptr;
}
std::cout << os.str() << std::endl << std::endl;
}
int main() {
using namespace std;
ifstream myFile ("INFO_HASH__ubuntu-12.04.1-desktop-i386.torrent", ifstream::binary);
//Get file length
myFile.seekg(0, myFile.end);
int fileLength = myFile.tellg();
myFile.seekg(0, myFile.beg);
char buffer[fileLength];
myFile.read(buffer, fileLength);
cout << "File length == " << fileLength << endl;
cout << buffer << endl << endl;
unsigned char datSha[20];
SHA1((unsigned char *) buffer, fileLength, datSha);
printHexRep(datSha);
myFile.close();
return 0;
}
像这样编译:
g++ -o hashes info_hasher.cpp -lssl -lcrypto
我遇到了这个输出:
4d0ca7e1599fbb658d886bddf3436e6543f58a8b
当我期待这个输出时:
14FFE5DD23188FD5CB53A1D47F1289DB70ABF31E
有人知道我在这里做错了什么吗?问题可能出在文件末尾的不可读性上吗?我需要先将其解析为十六进制还是什么?
Does anybody know what I might be doing wrong here? Could the problem lie with the un-readability of the end of the file? Do I need to parse this as hex first or something?
推荐答案
确保文件末尾没有换行符,您可能还想确保它以e"结尾.
Make sure you don't have a newline at the end of the file, you may also want to make sure it ends with an 'e'.
>
torrent 文件的信息哈希是 .torrent 文件中信息部分(以编码形式)的 SHA-1 哈希.本质上,您需要解码文件(它是经过编码的)并记住与info"键相关联的值的内容开始和结束的字节偏移量.这是您需要散列的字节范围.
The info-hash of a torrent file is the SHA-1 hash of the info-section (in bencoded form) from the .torrent file. Essentially you need to decode the file (it's bencoded) and remember the byte offsets where the content of the value associated with the "info" key begins and end. That's the range of bytes you need to hash.
例如,如果这是 torrent 文件:
For example, if this is the torrent file:
d4:infod6:pieces20:....................4:name4:test12:piece lengthi1024ee8:announce27:http://tracker.com/announcee
您只想散列此部分:
d6:pieces20:....................4:name4:test12:piece lengthi1024ee
有关 bencoding 的更多信息,请参阅 BEP3.
For more information on bencoding, see BEP3.
这篇关于计算种子文件的信息哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:计算种子文件的信息哈希
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01