计算torrent文件的信息哈希

编程入门 行业动态 更新时间:2024-10-24 11:14:50
本文介绍了计算torrent文件的信息哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用C ++来解析一个torrent文件的信息哈希,我得到一个正确的哈希值比较这个网站:

i-tools/torrent

我已经构建了一个非常简单的玩具示例,以确保我有正确的基础。

我在sublime打开了一个.torrent文件,对于信息字典,所以我有一个像这样的文件:

d6:lengthi729067520e4:name31:ubuntu-12.04.1 -desktop-i386.iso12:piece lengthi524288e6:pieces27820:¶ØËš3í..............(更难以阅读的东西.....)......... 。

我读取此文件并使用以下代码解析:

#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); //获取文件长度 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<<缓冲液< 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

有人知道我在这里做错了什么吗?问题是文件结束的不可读性吗?我需要将它解析为十六进制的第一个或什么?

解决方案

确保您没有换行符该文件,您可能还需要确保它以一个'e'结尾。

torrent文件的信息哈希是信息的SHA-1哈希分区(以编码形式).torrent文件。本质上,你需要解码文件(它是bencoded),并记住字节偏移量与info键相关联的值的内容开始和结束。

例如,如果这是torrent文件:

d4:infod6:pieces20:.................... 4:name4:test12:piece lengthi1024ee8:announce27:http://

d6:pieces20:.................... 4:name4:test12:piece lengthi1024ee

有关bencoding的详细信息,请参阅 BEP3 。

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:

i-tools/torrent

I have constructed a very simple toy example just to make sure I have the basics right.

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!\n"; 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; }

Compile it like so:

g++ -o hashes info_hasher.cpp -lssl -lcrypto

And I am met with this output:

4d0ca7e1599fbb658d886bddf3436e6543f58a8b

When I am expecting this output:

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?

解决方案

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'.

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.

For example, if this is the torrent file:

d4:infod6:pieces20:....................4:name4:test12:piece lengthi1024ee8:announce27:tracker/announcee

You wan to just hash this section:

d6:pieces20:....................4:name4:test12:piece lengthi1024ee

For more information on bencoding, see BEP3.

更多推荐

计算torrent文件的信息哈希

本文发布于:2023-11-03 12:33:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   信息   torrent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!