解析 torrent 文件 - 哈希信息。 (二郎)
我正在尝试找出正确的网址编码信息哈希发送到跟踪器,以便获取对等列表。
为了进行测试,我尝试解析 此网址 中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,URL 编码在 erlang 中已经可用(尽管隐藏得很好)。
它产生与你相同的结果。
Note that URL-encoding is already available in erlang (albeit well hidden).
It yields the same result as yours.
您的问题不在于编码器,而在于您对数据的最初猜测。我们拥有的字符串是“788f590f28a799cc1009a9b780b649fd6f0a2e91”,因此我们编写了一些 Erlang 代码,将其转换为列表形式的二进制表示形式:
现在,在提示中询问会给出:
与预期匹配。您应该检查您手动选择的 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:
Now, asking in a prompt gives:
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.