Advertisement
julialy

MD5 in pure lua (no ffi no bit32)

May 11th, 2016
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. --Licensed under CC0, enjoy :-)
  2. local md5=function(L)local p,P,f,l,r,d,c,i,q,J,m,Q,V,y,F,w,Y,a,h,U,b,M,g,e,C,o,s,n,G=4503599627370496,2271681097355226,4294967296,4023233417,3135045272,1159922023,844396048,{},-257,-128,64,16,4,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,""local x=function(e,o)e=e+.5
  3. e=e/o
  4. e=e+1.5
  5. e=e+p
  6. e=e-p
  7. return e-2
  8. end
  9. local u=function(e,o)e=e-o
  10. e=e/p
  11. e=e+1
  12. e=e/2
  13. e=e+p
  14. return e-p
  15. end
  16. local W=function(e,o)e=e-o
  17. e=e*m
  18. return math.exp(-e*e)end
  19. local k=function(e,o)e=e*x(o,e)return o-e
  20. end
  21. local t=function(e)return i[math.floor(e)]or 0
  22. end
  23. local i=function(o,e)i[math.floor(o)]=e
  24. end
  25. local B=function(o)local r={}local h=1023
  26. local i=2047
  27. local c=nil
  28. local a=11
  29. local n=0
  30. local t=52
  31. if o<0 then
  32. c=1
  33. else
  34. c=0
  35. end
  36. o=math.abs(o)local e=math.floor(math.log(o)/math.log(2))local l=math.pow(2,-e)if o*l<1 then
  37. e=e-1
  38. l=l*2
  39. end
  40. if o*l>=2 then
  41. e=e+1
  42. l=l/2
  43. end
  44. if e+h>=i then
  45. e=i
  46. elseif e+h>=1 then
  47. n=(o*l-1)*math.pow(2,t)e=e+h
  48. else
  49. n=o*math.pow(2,h-1)*math.pow(2,t)e=0
  50. end
  51. while t>=8 do
  52. table.insert(r,math.floor(n%256))n=n/256
  53. t=t-8
  54. end
  55. e=math.floor(e*math.pow(2,t)+n)a=a+t
  56. while a>0 do
  57. table.insert(r,math.floor(e%256))e=e/256
  58. a=a-8
  59. end
  60. r[8]=r[8]+(c*128)return r
  61. end
  62. local K=function()if t(4)==0 then
  63. return true
  64. end
  65. local l=false
  66. local o=4
  67. while asssd~=0 do
  68. local e=t(o)if e==0 or e==10 then
  69. break
  70. end
  71. for t,e in pairs(B(e))do
  72. if e==0 or e==10 then
  73. l=true
  74. break
  75. end
  76. G=G..string.char(e)end
  77. o=o+1
  78. end
  79. return false
  80. end
  81. local B=0
  82. while K()do
  83. B=B+1
  84. o=string.byte(string.sub(L,B,B))or-1
  85. n=1-W(o,-1)o=o+J
  86. o=o*n
  87. o=o-J
  88. s=s+(o*w)w=w*256
  89. U=U+(n*8)s=s-U
  90. o=k(64,m+8)F=F*u(o*o,J)e=u(g+F,0)s=s*F
  91. s=s+U
  92. J=J*n
  93. o=d
  94. d=d-c
  95. d=d*e
  96. d=d+c
  97. c=c-r
  98. c=c*e
  99. c=c+r
  100. r=r-l
  101. r=r*e
  102. r=r+l
  103. l=l-o
  104. l=l*e
  105. l=l+o
  106. Y=Y*u(g,0)b=math.cos(h*2+2)b=math.sqrt((1-b)/2)*f-.5
  107. b=b+p
  108. r=r+r
  109. c=c+c
  110. d=d+d
  111. M=x(r,f)a=x(c,f)C=x(d,f)r=r+(M*(1-f))c=c+(a*(1-f))d=d+(C*(1-f))e=x(h,16)a=a-(C*u(math.cos(e*3),0))a=a*u(M+e,0)a=a+(C*W(e,0))a=a*a
  112. a=a-((1+C*(M-1))*W(e,3))a=a-M
  113. a=a*(1-C*W(e,1))a=a+(M*(1-W(e,2)))a=a*a
  114. Y=Y+(Y+a)n=k(4,h)g=g+y
  115. g=g-F
  116. o=((-4*e+16)*e-12)*e
  117. o=o*n+((22*e-90)*e+74)*e
  118. o=o*n-(((38*e-158)*e+138)*e-60)o=o*n+(((4*e-6)*e-22)*e+84)n=(((2*e-9)*e+11)*e+1)n=n*h-((2*e-7.5)*e+4.5)*e
  119. e=W(g,32)b=b-p
  120. b=b*e
  121. n=k(16,n)n=t(Q+n)n=n+Y
  122. n=n*e
  123. l=l+n
  124. l=l+b
  125. l=k(f,l)o=o*e
  126. o=math.pow(2,(o/12))l=l*o
  127. o=(l+.5*o)/f+1.5
  128. o=o+p
  129. o=o-p
  130. o=o-2
  131. l=l-(o*f)l=l+(o+r*e)l=k(f,l)g=g*(1-e)h=h+e
  132. o=u(64,h)h=h*o
  133. o=1-o
  134. d=d+(t(0)*o)l=l+(t(1)*o)r=r+(t(2)*o)c=c+(t(3)*o)d=k(f,d)l=k(f,l)r=k(f,r)c=k(f,c)Q=Q+(16*o)e=u(m,1)e=1-u(e,0)i(0,t(0)-d)i(1,t(1)-l)i(2,t(2)-r)i(3,t(3)-c)i(4,t(4)+e*46727004911171048e-169)i(5,t(5)+e*16937441874215991e174)o=math.exp(-y*(o+F)*m*m)i(0,t(0)*o)i(1,t(1)*o)i(2,t(2)*o)i(3,t(3)*o)i(4,t(4)*o)i(5,t(5)*o)e=math.exp(-e*m*m)e=e*u(m,399990)i(0,t(0)+d)i(1,t(1)+l)i(2,t(2)+r)i(3,t(3)+c)i(4,t(4)+e*4522871578970112e202)i(5,t(5)+e*790055321525613e101)y=y*u(m/4+1,Q)e=u(h+y,0)o=math.pow(2,q-304-f*e)i(V+4,(p+p/85+P)*o)i(m/4,s)m=m+F
  135. V=V+(e-1)n=u(w*y,f-1)s=s*(1-n)w=w+(n*(1-f)-1)w=w*e
  136. P=P*e
  137. q=q*e
  138. e=x(h,2)e=1-h+e*4
  139. e=x(t(V),math.pow(16,e))o=x(e,16)e=e-(o*16)e=e+(u(e,9)*39)o=x(e,16)o=o*W(h,6)e=e-(o*16)q=q+o
  140. w=w+1
  141. P=P+(e*w*u(7,h))q=q+(e*16*u(h,6))h=h+(1-y)h=h*u(m*y+8,h)end
  142. return G
  143. end
  144. print(md5("Hello world!"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement