简体   繁体   English

解析torrent文件 - 哈希信息。 (二郎)

[英]Parsing a torrent file - hash info. (Erlang)

I'm trying to come up with the correct url-encoded info hash to send to the tracker in order to get the peers list. 我正在尝试提供正确的url编码信息哈希发送到跟踪器以获取对等列表。

For testing, I tried parsing the torrent in this url . 为了测试,我尝试在这个URL中解析torrent。

After opening the file, manually cut the info dictionary piece and SHA1-hash it's value I get this binary value: 打开文件后,手动剪切信息字典片段和SHA1-hash它的值我得到这个二进制值:

<<84,124,15,255,155,171,156,168,91,46,204,24,249,116,110, 139,202,167,163,54>> << 84,124,15,255,155,171,156,168,91,46,204,24,249,116,110,139,202,167,163,54 >>

The ASCII string retrieved from the latter binary value is 788f590f28a799cc1009a9b780b649fd6f0a2e91, and it's the same value mentioned in the site. 从后一个二进制值检索的ASCII字符串是788f590f28a799cc1009a9b780b649fd6f0a2e91,它与站点中提到的值相同。

So let's assume everything is correct until now (isn't it?). 所以我们假设到现在为止一切都是正确的(不是吗?)。

After encoding the binary value using the url-encoding function below I get T%7c%0f%ff%9b%ab%9c%a8%5b.%cc%18%f9tn%8b%ca%a7%a36 , which is not even close to the correct urlencoded value that I should send to the tracker. 在使用下面的url-encoding函数对二进制值进行编码后,得到T%7c%0f%ff%9b%ab%9c%a8%5b。%cc%18​​%f9tn%8b%ca%a7%a36,这不是甚至接近我应发送给跟踪器的正确urlencoded值。 (I get a not-found error message when I send this to the tracker, plus, it's not matched to the value I see using wireshark which is x%8fY%0f%28%a7%99%cc%10%09%a9%b7%80%b6I%fdo%0a.%91 ). (当我将此信息发送给跟踪器时,我收到一条未找到的错误消息,此外,它与我使用wireshark看到的值不匹配,即x%8fY%0f%28%a7%99%cc%10%09%a9 %b7%80%b6I%fdo%0a。%91)。

The URL Encoding function I'm using: 我正在使用的URL编码功能:

encode(<<Bin:20/binary-unit:8>>)->
    %io:format("~p~n", [binary_to_list(Bin)]),
    encode(binary_to_list(Bin));
encode(List) -> do_encode(List).

do_encode([])-> [];
do_encode([H|T]) when H>=$a, H=<$z ->
    [H| encode(T)];
do_encode([H|T]) when H>=$A, H=<$Z ->
    [H| encode(T)];
do_encode([H|T]) when H>=$0, H=<$9 ->
    [H| encode(T)];
do_encode([H|T]) when H==$- ->
    [H| encode(T)];
do_encode([H|T]) when H==$. ->
    [H|do_encode(T)];
do_encode([H|T]) when H==$* ->
    [H|do_encode(T)];
do_encode([H|T]) ->
     to_hex(H) ++ encode(T).

hex(N) when N < 10 ->
    $0+N;
hex(N) when N >= 10, N < 16 ->
    $a+(N-10).
to_hex(N) when N < 256 ->
    [$%, hex(N div 16), hex(N rem 16)].

Is the function above wrong? 以上功能是错误的吗? I'm a kind of a newbie when it comes to raw-data handling. 在原始数据处理方面,我是一个新手。 so help/ideas are much appreciated! 所以非常感谢帮助/想法! Thanks! 谢谢!

Note that URL-encoding is already available in erlang (albeit well hidden). 请注意,URL编码已经在erlang中可用(尽管隐藏得很好)。

1> B = <<84,124,15,255,155,171,156,168,91,46,204,24,249,116,110, 139,202,167,163,54>>.
<<84,124,15,255,155,171,156,168,91,46,204,24,249,116,110,
2> L = erlang:binary_to_list(B).
[84,124,15,255,155,171,156,168,91,46,204,24,249,116,110,139,
 202,167,163,54]
3> edoc_lib:escape_uri(L).
"T%7c%f%c3%bf%c2%9b%c2%ab%c2%9c%c2%a8%5b.%c3%8c%18%c3%b9tn%c2%8b%c3%8a%c2%a7%c2%a36"

It yields the same result as yours. 它会产生与您相同的结果。

Your problem is not with your encoder but with your initial guess on the data. 您的编码器不是您的问题,而是您对数据的初步猜测。 The String we have is "788f590f28a799cc1009a9b780b649fd6f0a2e91", so we write a little bit of Erlang code to convert this to its binary representation as a list: 我们的字符串是“788f590f28a799cc1009a9b780b649fd6f0a2e91”,因此我们编写一些Erlang代码将其转换为二进制表示形式作为列表:

part([]) ->  [];
part([U,L | R]) ->
    [{list_to_integer([U], 16),
      list_to_integer([L], 16)} | part(R)].

Now, asking in a prompt gives: 现在,在提示中询问:

(etorrent@127.0.0.1)16> etorrent_utils:build_encoded_form_rfc1738([U*16+L || {U,L} <- foo:part("788f590f28a799cc1009a9b780b649fd6f0a2e91")]).
"x%8FY%0F%28%A7%99%CC%10%09%A9%B7%80%B6I%FDo%0A.%91"

Matching the expected. 匹配预期。 You should check that your manual picking of the infohash and its SHA1 calculation works as you expect it to work. 您应该检查您的手动选择infohash及其SHA1计算是否按预期工作。 Because your SHA1 binary does not match it. 因为您的SHA1二进制文件与它不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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