Last active
March 29, 2022 07:51
-
-
Save dkw72n/f9e72976ce2c5df22533f1f67969f7f2 to your computer and use it in GitHub Desktop.
codegolf of md5 in lua5.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local md5=function(E) local z,A,B,C,D,p=0xffffffff,0x67452301,0xefcdab89,0x98badcfe,0x10325476,string local uk,R,Q,q,v,P=p.unpack,function(a,b,c,d,x,s,ac) a=a+((b&c)|((~b)&d))+x+ac a=(((a&z)<<s)|((a&z)>>32-s))+b return a&z end,function(a,b,c,d,x,s,ac) a=a+((b&d)|(c&(~d)))+x+ac a=(((a&z)<<s)|((a&z)>>32-s))+b return a&z end,function(a,b,c,d,x,s,ac) a=a+(b~c~d)+x+ac a=(((a&z)<<s)|((a&z)>>32-s))+b return a&z end,function(a,b,c,d,x,s,ac) a=a+(c~(b|(~d)))+x+ac a=(((a&z)<<s)|((a&z)>>32-s))+b return a&z end,function(s) local N=s:len() local Z=N*8 % 512 local fS=(448-Z)//8 if Z>448 then fS=(960-Z)//8 end local K={} local bI=1 for i=1,N do local X=(i-1)//4+1 K[X]=K[X] or 0 K[X]=K[X]|(s:byte(i)<<(bI-1)*8) bI=bI+1 if bI==5 then bI=1 end end local Y=false local U=#K if bI~=1 then K[U]=K[U]|0x80<<(bI-1)*8 Y=true end for i=1,fS//4 do if not Y and i==1 then K[U+i]=0x80 else K[U+i]=0x0 end end local O=N*8 U=#K K[U+1]=O&z K[U+2]=O>>32 return K end return(function(s) local w=P(s) local V={A,B,C,D} local x=function(f,g) return table.pack(uk(p.rep(f,64),g)) end local a,b,c=x("i1","\0\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\1\6\11\0\5\10\15\4\9\14\3\8\13\2\7\12\5\8\11\14\1\4\7\10\13\0\3\6\9\12\15\2\0\7\14\5\12\3\10\1\8\15\6\13\4\11\2\9"),x("i1","\7\12\17\22\7\12\17\22\7\12\17\22\7\12\17\22\5\9\14\20\5\9\14\20\5\9\14\20\5\9\14\20\4\11\16\23\4\11\16\23\4\11\16\23\4\11\16\23\6\10\15\21\6\10\15\21\6\10\15\21\6\10\15\21"),x("I4","\120\164\106\215\86\183\199\232\219\112\32\36\238\206\189\193\175\15\124\245\42\198\135\71\19\70\48\168\1\149\70\253\216\152\128\105\175\247\68\139\177\91\255\255\190\215\92\137\34\17\144\107\147\113\152\253\142\67\121\166\33\8\180\73\98\37\30\246\64\179\64\192\81\90\94\38\170\199\182\233\93\16\47\214\83\20\68\2\129\230\161\216\200\251\211\231\230\205\225\33\214\7\55\195\135\13\213\244\237\20\90\69\5\233\227\169\248\163\239\252\217\2\111\103\138\76\42\141\66\57\250\255\129\246\113\135\34\97\157\109\12\56\229\253\68\234\190\164\169\207\222\75\96\75\187\246\112\188\191\190\198\126\155\40\250\39\161\234\133\48\239\212\5\29\136\4\57\208\212\217\229\153\219\230\248\124\162\31\101\86\172\196\68\34\41\244\151\255\42\67\167\35\148\171\57\160\147\252\195\89\91\101\146\204\12\143\125\244\239\255\209\93\132\133\79\126\168\111\224\230\44\254\20\67\1\163\161\17\8\78\130\126\83\247\53\242\58\189\187\210\215\42\145\211\134\235") for i=1,#w//16 do local L,F,t,T=(i-1)*16+1,{R,Q,q,v},{2,1,4,3},{V[1],V[2],V[3],V[4]} for i=1,64 do T[t[i%4+1]]=F[(i-1)//16+1](T[t[i%4+1]],T[t[(i+3)%4+1]],T[t[(i+2)%4+1]],T[t[(i+1)%4+1]],w[L+a[i]],b[i],c[i]) end for i=1,4 do V[i]=(V[i]+T[i])&z end end local J='' for i=1,4 do for _=1,4 do J=J..p.format("%02x",V[i]&0xFF) V[i]=V[i]>>8 end end return p.lower(J) end)(E) end | |
print(md5("hello world")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment