2013-12-13 46 views
0

OK我已经搜索了所有相关的问题,以及可用的源代码,只是卡住了。我使用的是相关问题的代码: How to calculate the hash value of a torrent using Javainfo_hash为torrent计算不正确

构建SHA-1散列。

然后我这样做是为了对其进行编码,要被包括在跟踪请求:

URLCodec C =新org.apache.commons.codec.net.URLCodec(); return new String(c.encode(info_hash));

但跟踪器回复没有同行。通过跟踪uTorrent,我可以看到我的torrent文件的正确散列是: T%7f%bc%a6%92%bb%8a%8b%2aL%b9%a3%0f%a59%f3%98%e6%0c %EB

但我的代码输出: %E4%AF%3C%96%9E%D2%奥吉通%C0%C3%B4%12%93%D4H%3B%9A%2CF

任何想法为什么这不起作用?

+0

基本上你需要来urlencode信息散列的二进制* *的形式。这是经常被问到的问题,实际上SO已经有这样的答案:http://stackoverflow.com/questions/4072234/bittorrent-tracker-request-format-of-info-hash –

回答

1
private string encodeInfoHash(string code) 
{ 
     //string code = "78820B24672757A60BEF15252E93F3D0C4DEB5C3"; 
     byte[] codeB = Encoding.Default.GetBytes(code); 
     string info_hash_encoded = null; 
     for (int i = 0; i < code.Length; i += 2) 
     { 
      string tmpCode = code[i].ToString() + code[i + 1].ToString(); 
      int j = Convert.ToInt32(tmpCode, 16); 
      char s1 = (char)j; 
      bool isSpecChar = false; 
      if (s1.ToString() == "." || s1.ToString() == "-" || s1.ToString() == "~") 
        isSpecChar = true; 
      if ((Char.IsLetter(s1) || Char.IsNumber(s1) || isSpecChar) && !Char.IsControl(s1) && j <= 127) 
      { 
        info_hash_encoded += s1.ToString(); 
      } 
      else 
      { 
        info_hash_encoded += "%" + tmpCode.ToLower(); 
      } 
     } 
     return info_hash_encoded; 
} 

我使用C#,你可以阅读我的博客更详细 http://hello.xiaoyezi.xyz/urlencode-the-info_hash.html

+0

新用户答案审查:感谢您抽出时间提供答案。如果你喜欢tipp,我会建议用一个文本开始一个答案,然后显示代码。你没有做错任何事,它只是看起来像一个“代码唯一”的答案,这是被折磨。在没有总结内容的情况下提供链接也是如此。 – eckes