解析洪流文件 - 哈希信息。 (Erlang)
-
29-09-2019 - |
题
我正在尝试提出正确的URL编码信息哈希,以发送到跟踪器以获取同行列表。
为了进行测试,我尝试解析洪流 这个网址.
打开文件后,手动剪切信息字典件和sha1-hash的值,我得到了这个二进制值:
<<84,124,15,255,155,171,156,168,91,46,204,24,249,116,110, 139,202,167,163,54>>
从后一个二进制值中检索的ASCII字符串为788F590F28A799CC1009B780B780B649FD6F0A2E91,并且网站中提到的值相同。
因此,让我们假设到目前为止一切都正确(不是吗?)。
使用URL编码函数编码二进制值之后,我获得T%7C%0f%FF%9B%AB%9C%A8%5B。%CC%CC%18%F9TN%8B%CA%CA%A7%A36,不是甚至接近我应该发送到跟踪器的正确urlencoded值。 (当我将其发送给跟踪器时,我会收到一条未建立的错误消息,此外,它与我使用Wireshark所看到的值不匹配,该值是x%8FY%0f%28%A7%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)].
功能上方是错误的吗?在Raw Data处理方面,我是一种新手。因此,帮助/想法非常感谢!谢谢!
解决方案
请注意,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"
它的结果与您的结果相同。
其他提示
您的问题不是与编码器有关,而是您对数据的最初猜测。我们拥有的字符串是“ 788F590F28A799CC1009A9B780B780B649FD6F0A2E91”,因此我们编写了一些Erlang代码将其转换为其二进制表示为列表:
part([]) -> [];
part([U,L | R]) ->
[{list_to_integer([U], 16),
list_to_integer([L], 16)} | part(R)].
现在,在提示下询问:
(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"
匹配预期。您应该检查一下Infohash的手动选择及其SHA1计算是否有效。因为您的SHA1二进制不匹配它。