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

发布于 2024-10-01 15:44:20 字数 1591 浏览 4 评论 0原文

我正在尝试找出正确的网址编码信息哈希发送到跟踪器,以便获取对等列表。

为了进行测试,我尝试解析 此网址 中的 torrent 。

打开文件后,手动剪切信息字典片段和SHA1哈希值我得到这个二进制值:

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

从后一个二进制值检索到的 ASCII 字符串是 788f590f28a799cc1009a9b780b649fd6f0a2e91,与网站中提到的值相同。

因此,我们假设到目前为止一切都是正确的(不是吗?)。

使用下面的 url 编码函数对二进制值进行编码后,我得到 T%7c%0f%ff%9b%ab%9c%a8%5b.%cc%18%f9tn%8b%ca%a7%a36 ,它甚至不接近我应该发送给跟踪器的正确的 urlencoded 值。 (当我将其发送到跟踪器时,我收到一条未找到的错误消息,另外,它与我使用wireshark看到的值不匹配,即x%8fY%0f%28%a7%99%cc%10%09%a9 %b7%80%b6I%fdo%0a.%91 )。

我正在使用的 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)].

上面的函数是否错误?在原始数据处理方面,我是一个新手。所以非常感谢帮助/想法!谢谢!

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.

For testing, I tried parsing the torrent in this url.

After opening the file, manually cut the info dictionary piece and SHA1-hash it's value I get this binary value:

<<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.

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. (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 ).

The URL Encoding function I'm using:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阪姬 2024-10-08 15:44:20

请注意,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"

它产生与你相同的结果。

Note that URL-encoding is already available in erlang (albeit well hidden).

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.

琴流音 2024-10-08 15:44:20

您的问题不在于编码器,而在于您对数据的最初猜测。我们拥有的字符串是“788f590f28a799cc1009a9b780b649fd6f0a2e91”,因此我们编写了一些 Erlang 代码,将其转换为列表形式的二进制表示形式:

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

现在,在提示中询问会给出:

([email protected])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"

与预期匹配。您应该检查您手动选择的 infohash 及其 SHA1 计算是否按照您的预期工作。因为你的 SHA1 二进制文件与它不匹配。

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:

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

Now, asking in a prompt gives:

([email protected])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. Because your SHA1 binary does not match it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文