好吧,我已经搜索了所有相关问题和可用的源代码,只是卡住了。 我正在使用相关问题中的代码: How to calculate the hash value of a torrent using Java
构建 SHA-1 哈希。
然后我这样做是为了对其进行编码,以包含在跟踪器请求中:
URLCodec c = new 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%BAJt%C0%C3%B4%12%93%D4h%3B%9A%2CF
有什么想法为什么这行不通吗?
最佳答案
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
关于java - 为 torrent 计算的 info_hash 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20569905/