如何从40字节转换为ASCII编码的SHA1哈希(40字节十六进制字符串),以20字节?

问题描述 投票:1回答:1

我有一个torrent文件。它info_hash值87bebe2a6dfa25c8d8075893c4c6e05878cccb4a

我需要从UDP跟踪得到的同龄人,但在UDP协议跟踪,info_hash的大小应该是20。那么该如何改变呢?

我用BitComet的下载和Wireshark来捕获消息。

这是我得到的东西:

0000   80 f6 2e cd a7 81 8c ec 4b 6d 8f 0f 08 00 45 00   .ö.ͧ..ìKm....E.
0010   00 7e 41 a9 00 00 80 11 00 00 c0 a8 29 49 6d 4a   .~A©......À¨)ImJ
0020   46 bc 41 cd 4c 3e 00 6a 9e 73 64 31 3a 61 64 32   F¼AÍL>.j.sd1:ad2
0030   3a 69 64 32 30 3a f7 66 44 1b 73 41 09 0d 1b 10   :id20:÷fD.sA....
0040   84 1d a6 7f 47 e8 4c b7 63 86 36 3a 74 61 72 67   ..¦.GèL·c.6:targ
0050   65 74 32 30 3a 08 99 bb e4 8c be f6 f2 e4 ef 7b   et20:..»ä.¾öòäï{
0060   e2 59 80 b8 17 b3 48 9c 78 65 31 3a 71 39 3a 66   âY.¸.³H.xe1:q9:f
0070   69 6e 64 5f 6e 6f 64 65 31 3a 74 38 3a 0f ea 8a   ind_node1:t8:.ê.
0080   c2 86 ff 4c 37 31 3a 79 31 3a 71 65               Â.ÿL71:y1:qe

据UDP跟踪协议,该info_hash我捕捉7341090d1b1084 1da67f47e84cb76386363a7461。它是从实际值是87bebe2a6dfa25c8d8075893c4c6e05878cccb4a完全不同。我错了吗?我相信该消息的目的地是一个UDP跟踪器,因为它的长度是98。

我试图用Java转换。下面是代码:

public static String IHToString(String v) throws Exception{
    byte[] b=new byte[20];
    for(int i=0;i<b.length;i++) {
        String n=v.substring(i*2, i*2+2);
        int j=(byte)Integer.parseInt(n, 16) & 0xff;
        b[i]=(byte)j;
    }

    return new String(b,"ASCII");
}
String s1=IHToString("87bebe2a6dfa25c8d8075893c4c6e05878cccb4a");
byte[] b1=s1.getByte("ASCII");

显然是错误的。

其结果是既不7341090d1b1084 1da67f47e84cb76386363a7461也不87bebe2a6dfa25c8d8075893c4c6e05878cccb4a。我真的累了这个问题。请帮我。 -_-

java udp p2p bittorrent dht
1个回答
1
投票

您的文件(info_hash)的87bebe...已经有20个字节。一个字节可以通过两个十六进制数字来表示。 SHA-1散列160具有位(20字节)的长度。

这哈希值是从您从UDP协议跟踪拿到info_hash不同,因为它是计算方式不同。哈希不能颠倒。

有关详细信息,请参阅:http://www.bittorrent.org/beps/bep_0003.htmlWhat exactly is the info_Hash in a torrent file

© www.soinside.com 2019 - 2024. All rights reserved.