The torrent info_hash parameter

2019-04-09 14:23发布

How does one calculate the info_hash parameter? Aka the hash corresponding to the info dictionar??

From official specs:

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.

Does this mean simply get the substring from the meta-info file and do a sha-1 hash on the reprezentative bytes??

.... because this is how i tried 12 times but without succes meaning I have compared the resulting hash with the one i should end up with..and they differ ..that + tracker response is FAILURE, unknown torrent ...or something

So how do you calculate the info_hash?

3条回答
倾城 Initia
2楼-- · 2019-04-09 14:50

bdecode the metafile, then it's simply "sha1(bencode(metadata['info']))" (i.e. bencode only the info dict again, then hash that).

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-09 14:55

The metafile is already bencoded so I don't understand why you encode it again?

I finally got this working in Java code, here is my code:

byte metaData[];  //the raw .torrent file

int infoIdx = ?;  //index of 'd' right after the "4:info" string

info_hash = SHAsum(Arrays.copyOfRange(metaData, infoIdx, metaData.length-1));

This assumes the 'info' block is the last block in the torrent file (wrong?)

Don't sort or anything like that, just use a substring of the raw torrent file.

Works for me.

查看更多
等我变得足够好
4楼-- · 2019-04-09 15:16

Visit https://gist.github.com/shxsun/9085646 , this is a python code, explains how info_generate, good luck.

查看更多
登录 后发表回答