无效信息\u散列(Java BitTorrent客户端)
根据规范: info_散列:Metainfo文件中info键值的URLCoded 20字节SHA1散列。请注意,根据上面info键的定义,该值将是一个bencoded字典无效信息\u散列(Java BitTorrent客户端),java,sha1,bittorrent,Java,Sha1,Bittorrent,根据规范: info_散列:Metainfo文件中info键值的URLCoded 20字节SHA1散列。请注意,根据上面info键的定义,该值将是一个bencoded字典 torrendmap是我的字典,我得到另一个字典的info键,我计算散列,然后我URLencode它 但是,当我试图将信息发送到跟踪器时,总会收到一条无效信息\u散列消息 这是我的代码: public String GetInfo_hash() { String info_hash = ""; Byt
torrendmap
是我的字典,我得到另一个字典的info
键,我计算散列,然后我URLencode
它
但是,当我试图将信息发送到跟踪器时,总会收到一条无效信息\u散列
消息
这是我的代码:
public String GetInfo_hash() {
String info_hash = "";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(torrentMap.get("info"));
byte[] bytes = bos.toByteArray(); //Map => byte[]
MessageDigest md = MessageDigest.getInstance("SHA1");
info_hash = urlencode(md.digest(bytes)); //Hashing and URLEncoding
out.close();
bos.close();
} catch (Exception ex) { }
return info_hash;
}
private String urlencode(byte[] bs) {
StringBuffer sb = new StringBuffer(bs.length * 3);
for (int i = 0; i < bs.length; i++) {
int c = bs[i] & 0xFF;
sb.append('%');
if (c < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(c));
}
return sb.toString();
}
公共字符串GetInfo\u hash(){
字符串信息_hash=“”;
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
ObjectOutput out=null;
试一试{
out=新对象输出流(bos);
out.writeObject(torrendmap.get(“info”);
byte[]bytes=bos.toByteArray();//映射=>byte[]
MessageDigest md=MessageDigest.getInstance(“SHA1”);
info_hash=urlencode(md.digest(bytes));//哈希和URLEncoding
out.close();
bos.close();
}捕获(例外情况除外){}
返回信息散列;
}
专用字符串urlencode(字节[]bs){
StringBuffer sb=新的StringBuffer(bs.length*3);
对于(int i=0;i
这几乎肯定是问题所在:
out = new ObjectOutputStream(bos);
out.writeObject(torrentMap.get("info"));
您将要散列的是torrendmap.get(“info”)
值的Java二进制序列化格式。我发现很难相信所有的BitTorrent程序都应该知道这一点
从规范中我还不清楚“info”键的值是什么,但您需要找到其他方法将其转换为字节数组。如果它是一个字符串,我希望使用一些指定良好的编码(例如UTF-8)。如果已经是二进制数据,则直接使用该字节数组
编辑:事实上,根据你的引用,这个值听起来像是一个“bencodeddictionary”,看起来像是一个字符串。在对字符串进行散列之前,您应该如何对该字符串进行编码(例如,听起来它可能包含非ASCII格式的值),这就需要掌握了。如果您的示例字符串都是ASCII,那么使用“ASCII”和“UTF-8”作为字符串的编码名称。getBytes(…)
无论如何都会给出相同的结果,当然…torrentMap.get(“info”)返回一个对象,它可以强制转换为HashMap。根据您所说的,我将该方法更改为:返回urlencode(torrentMap.get(“info”).toString().getBytes(Charset.forName(“UTF-8”));但它仍然给我一些错误:(@Simone:如果它真的是一个HashMap
,你需要将它以bencoding的形式放入字符串中,因为这就是将被哈希的内容。不清楚你是想从头开始创建自己的BT API,还是修改现有的BT API,但你需要bencoding支持。