简体   繁体   中英

info_hash calculated for torrent incorrect

OK I've searched all related questions, and source code where available, just stuck. I'm using this code from a related question: How to calculate the hash value of a torrent using Java

to build the SHA-1 hash.

Then I'm doing this to encode it, to be included in the tracker request:

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

But the tracker replies with no peers. By tracing uTorrent, I can see the correct hash for my torrent file is: T%7f%bc%a6%92%bb%8a%8b%2aL%b9%a3%0f%a59%f3%98%e6%0c%eb

But my code outputs: %E4%AF%3C%96%9E%D2%BAJt%C0%C3%B4%12%93%D4h%3B%9A%2CF

Any ideas why this won't work?

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;
}

I use C# , you can read my blog for more detail http://hello.xiaoyezi.xyz/urlencode-the-info_hash.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM