2013-12-13 73 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