为什么对信息字典进行散列会导致错误? [英] Why is hashing the info dict turning out wrong?

查看:131
本文介绍了为什么对信息字典进行散列会导致错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很久以来一直在尝试获取这种哈希值,以使BitTorrent可以在Java中工作,但是它总是会出错.

I have been trying for ages to get this hashing thing for BitTorrent to work in Java but it always becomes wrong.

我已将其范围缩小到几行代码,我99%的确定问题是:

I have narrowed it down to a few lines of code where I'm 99% sure the problem is:

Bencode bencode = new Bencode(Charset.forName("UTF-8"));
byte[] fileBytes = new byte[33237];
Map<String, Object> dict = bencode.decode(fileBytes, Type.DICTIONARY);
Map infoMap = (Map) object.get("info");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BencodeOutputStream bos = new BencodeOutputStream(baos);
bos.writeDictionary(infoMap);
byte[] hash = DigestUtils.sha1(baos.toByteArray());

我已经对数组的大小进行了硬编码,以确保问题不是由一堆零所引起的.

I have hardcoded the size of the array just to make sure the issue is not caused by a bunch of zeroes hanging around.

我已经尝试过同时使用UTF-8US-ASCII.

I have tried with both UTF-8 and US-ASCII.

我尝试使用两个不同的库进行bencoding,因此问题所在可能不在那里.

I have tried using two different libraries for the bencoding so it's probably not there where the problem's at.

编辑:从规范看来,应该将info dict编码为info_hash.因此,我尝试将字典写出为ByteArrayOutputStream,然后对ByteArrayOutPutStream所保存的byte[]进行sha1哈希.

From the spec it seems that the info dict should be urlencoded as the info_hash. So I tried writing out the dictionary into a ByteArrayOutputStream and then do the sha1 hashing on the byte[] that ByteArrayOutPutStream is holding.

DigestUtils.sha1方法是否将提供URL编码器?找不到任何相关信息.

Will the DigestUtils.sha1method provide a URL encoder? Can't find any information on that.

推荐答案

正如Encombe所指出的,问题在于编码.在Bencode规范中,它讨论了 byte字符串,这似乎表明它只是没有任何编码的数据流.

The problem, as Encombe pointed out, was with the encoding. In the Bencode specification it talks about byte strings and this seems to point to it just being a stream of data without any encoding.

我看过的两个库都将所有字节字符串转换为某种编码,所以我写了一个Bencode库,仅在明确要求时才进行转换.

Both of the libraries I looked at converted all byte strings to some encoding so I wrote a Bencode library that only did the conversion when specifically asked to.

上面的代码基本上是正确的,但这是我现在正在使用的客户端代码:

The code above is basically correct but here is the client code I am using now:

public void readManifest() throws IOException, Exception {
    byte[] fileBytes = FileUtils.readFileToByteArray(file);
    ByteArrayInputStream bis = new ByteArrayInputStream(fileBytes);
    BDecoder decoder = new BDecoder(bis, "UTF-8");
    BDict dict = decoder.decodeDict();
    Map<String, Object> valueMap = dict.getValue();
    infoMap = (Map<String, Object>) valueMap.get("info");
}

public String hash() throws Exception {
    if (hash == null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BEncoder encoder = new BEncoder(baos, "UTF-8");
        encoder.encodeDict(infoMap);
        hash = DigestUtils.sha1Hex(baos.toByteArray());
    }
    return hash;
}

这篇关于为什么对信息字典进行散列会导致错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆