相关疑难解决方法(0)

计算torrent文件的info-hash

我正在使用C++来解析torrent文件的信息哈希,与此站点相比,我无法获得"正确"的哈希值:

http://i-tools.org/torrent

我构建了一个非常简单的玩具示例,以确保我有正确的基础知识.

我在sublime中打开了一个.torrent文件并删除了除信息字典之外的所有内容,所以我有一个如下所示的文件:

d6:lengthi729067520e4:name31:ubuntu-12.04.1-desktop-i386.iso12:piece lengthi524288e6:pieces27820:¡´E¶ˆØËš3í   ..............(more unreadable stuff.....)..........
Run Code Online (Sandbox Code Playgroud)

我读了这个文件并用这段代码解析它:

#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 …
Run Code Online (Sandbox Code Playgroud)

c++ hash bittorrent sha1 info-hash

4
推荐指数
1
解决办法
6441
查看次数

创建 torrent 哈希信息

如何在 torrent 文件上生成 torrent 哈希信息。

我一直在看这个例子:如何使用 Java 计算 torrent 的哈希值,并尝试将其转换为 C++。这是我到目前为止的代码:

void At::ReadTorrent::TorrentParser::create_hash(std::string torrentstub)
{
    std::string info;
    int counter = 0;

    while(info.find("4:info") == -1)
    {
        info.push_back(torrentstub[counter]);
        counter++;
    }

    unsigned char array[torrentstub.size()];
    int test = 0;

    for(int data; (data = torrentstub[counter]) > -1;)
    {
         array[test++] = data;
         counter++;
    }
    std::cout << array << std::endl;

    //SHA-1 some value here to generate the hash.
}
Run Code Online (Sandbox Code Playgroud)

参数torrentstub是以字符串表示的 torrent 文件。据我了解,我必须获得之后的信息4:info。我认为这工作正常,例如:

d6:lengthi2847431620e4:name8:filename12:piece lengthi1143252e6:pieces50264
Run Code Online (Sandbox Code Playgroud)

之后只有我无法读取的信息,我猜这是一些二进制数据?

所以我的问题实际上可以归结为:应该对 后面的所有内容进行哈希处理的信息4:info,以及我应该在哪里停止收集哈希数据?

c++ bittorrent info-hash

2
推荐指数
1
解决办法
2110
查看次数

标签 统计

bittorrent ×2

c++ ×2

info-hash ×2

hash ×1

sha1 ×1