Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Erlang和JavaScript MD5摘要匹配_Javascript_Erlang_Mnesia_Yaws - Fatal编程技术网

Erlang和JavaScript MD5摘要匹配

Erlang和JavaScript MD5摘要匹配,javascript,erlang,mnesia,yaws,Javascript,Erlang,Mnesia,Yaws,在此处测试MD5的Javascript实现:提供以下输出: MD5("muzaaya") = "00e081abefbbbf72b2d5258196a9b6d0" MD5(“muzaaya”)=“00E081abefbbf72b2d5258196a9b6d0” 转到我的erlang shell,计算相同值的MD5,我得到以下结果: Eshell V5.8.4 (abort with ^G) 1> erlang:md5("muzaaya"). <<0,224,129,171,2

在此处测试MD5的Javascript实现:提供以下输出:

MD5("muzaaya") = "00e081abefbbbf72b2d5258196a9b6d0" MD5(“muzaaya”)=“00E081abefbbf72b2d5258196a9b6d0” 转到我的erlang shell,计算相同值的MD5,我得到以下结果:

Eshell V5.8.4 (abort with ^G) 1> erlang:md5("muzaaya"). <<0,224,129,171,239,187,191,114,178,213,37,129,150,169, 182,208>> 2> Eshell V5.8.4(使用^G中止) 1> erlang:md5(“muzaaya”)。 2>
我如何比较这两者?如果JavaScript前端应用程序的MD5结果出现在我的Erlang后端,我希望能够比较这两个摘要。如何将Javascript MD5摘要与Erlang的摘要相匹配?

如果需要一行代码,它可以是这样的:

1> B = erlang:md5("muzaaya").
<<0,224,129,171,239,187,191,114,178,213,37,129,150,169,
  182,208>>
2> lists:flatten([io_lib:format("~2.16.0b", [C]) || <<C>> <= B]).
"00e081abefbbbf72b2d5258196a9b6d0"
1>B=erlang:md5(“muzaaya”)。

2> 列表:展平([io_lib:format(“~2.16.0b)”,[C])|如果您想在JavaScript端执行此操作,可以使用

function md5HexToArray ( hexStr ) {  
  var i, arr = [], arraylength = hexStr.length/2;

  for( i = 0; i < arraylength ; i++ ) {
     arr[i] = parseInt( hexStr.substr(i*2,2), 16) ;
  }

  return arr;
};
函数md5HexToArray(hextstr){
变量i,arr=[],arraylength=hexStr.length/2;
对于(i=0;i

但是@Wrikken的评论看起来也应该可以正常工作。

MD5哈希本质上是一个128位的数字

您在Erlang中以16字节(16*8=128位)的二进制形式接收MD5值。该二进制中的每个字节都必须转换为十六进制表示形式,才能与JavaScript的MD5输出(每字节两个字符的十六进制字符串)相比较:


(除了
io\u lib:format/2
之外,还有
http\u util:integer\u to\u hexlist/1
,尽管我不知道它是否更快)

这是位字符串理解版本,可能是最快、内存效率最高的版本:

hstr(B) when is_binary(B) ->
    T = {$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$a,$b,$c,$d,$e,$f},
    << <<(element(X bsr 4 + 1, T)), (element(X band 16#0F + 1, T))>>
    || <<X:8>> <= B >>.
hstr(B)何时为二进制(B)->
T={$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$a,$b,$c,$d,$e,$f},
.
3> M:hstr(erlang:md5(“muzaaya”))


4> 另一个更快的版本:

hstr(B) when is_binary(B) ->
  << <<(hex(A)), (hex(B))>> || <<A:4,B:4>> <= B >>.

-compile({inline, [hex/1]}).

hex(0)  -> $0;
hex(1)  -> $1;
hex(2)  -> $2;
hex(3)  -> $3;
hex(4)  -> $4;
hex(5)  -> $5;
hex(6)  -> $6;
hex(7)  -> $7;
hex(8)  -> $8;
hex(9)  -> $9;
hex(10) -> $a;
hex(11) -> $b;
hex(12) -> $c;
hex(13) -> $d;
hex(14) -> $e;
hex(15) -> $f.
EDIT2:另一种方法:

md5_hex(L) ->
    <<X:128>> = erlang:md5(L),
    B = integer_to_binary(X,16),
    list_to_binary([lists:duplicate(32-byte_size(B),$0)|B]).
md5_十六进制(L)->
=erlang:md5(L),
B=整数到二进制(X,16),
列表到二进制([列表:重复(32字节大小(B),$0)| B])。

非常感谢!,@WrikkenHm,正如您所知,可能更正确。。。
hstr(B) when is_binary(B) ->
  << <<(hex(A)), (hex(B))>> || <<A:4,B:4>> <= B >>.

-compile({inline, [hex/1]}).

hex(0)  -> $0;
hex(1)  -> $1;
hex(2)  -> $2;
hex(3)  -> $3;
hex(4)  -> $4;
hex(5)  -> $5;
hex(6)  -> $6;
hex(7)  -> $7;
hex(8)  -> $8;
hex(9)  -> $9;
hex(10) -> $a;
hex(11) -> $b;
hex(12) -> $c;
hex(13) -> $d;
hex(14) -> $e;
hex(15) -> $f.
md5_hex(L) ->
  << A1:4, A2:4,  A3:4,  A4:4,  A5:4,  A6:4,  A7:4,  A8:4,
    A9:4,  A10:4, A11:4, A12:4, A13:4, A14:4, A15:4, A16:4,
    A17:4, A18:4, A19:4, A20:4, A21:4, A22:4, A23:4, A24:4,
    A25:4, A26:4, A27:4, A28:4, A29:4, A30:4, A31:4, A32:4
    >> = erlang:md5(L),
  << (hex(A1)), (hex(A2)),  (hex(A3)),  (hex(A4)),
    (hex(A5)),  (hex(A6)),  (hex(A7)),  (hex(A8)),
    (hex(A9)),  (hex(A10)), (hex(A11)), (hex(A12)),
    (hex(A13)), (hex(A14)), (hex(A15)), (hex(A16)),
    (hex(A17)), (hex(A18)), (hex(A19)), (hex(A20)),
    (hex(A21)), (hex(A22)), (hex(A23)), (hex(A24)),
    (hex(A25)), (hex(A26)), (hex(A27)), (hex(A28)),
    (hex(A29)), (hex(A30)), (hex(A31)), (hex(A32)) >>.
hex(X) ->
  element(X+1, {$0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $a, $b, $c, $d, $e, $f}).
md5_hex(L) ->
    <<X:128>> = erlang:md5(L),
    B = integer_to_binary(X,16),
    list_to_binary([lists:duplicate(32-byte_size(B),$0)|B]).