Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

158
LINES

< > BotCompany Repo | #374 // MD5 Algorithm (md5.calc)

Lua code

1  
-- An MD5 mplementation in Lua
2  
-- 10/02/2001 jcw@equi4.com
3  
4  
local bit=bit32
5  
6  
md5={ff=tonumber('ffffffff',16),consts={}}
7  
8  
string.gsub([[ d76aa478 e8c7b756 242070db c1bdceee
9  
    f57c0faf 4787c62a a8304613 fd469501
10  
    698098d8 8b44f7af ffff5bb1 895cd7be
11  
    6b901122 fd987193 a679438e 49b40821
12  
    f61e2562 c040b340 265e5a51 e9b6c7aa
13  
    d62f105d 02441453 d8a1e681 e7d3fbc8
14  
    21e1cde6 c33707d6 f4d50d87 455a14ed
15  
    a9e3e905 fcefa3f8 676f02d9 8d2a4c8a
16  
    fffa3942 8771f681 6d9d6122 fde5380c
17  
    a4beea44 4bdecfa9 f6bb4b60 bebfbc70
18  
    289b7ec6 eaa127fa d4ef3085 04881d05
19  
    d9d4d039 e6db99e5 1fa27cf8 c4ac5665
20  
    f4292244 432aff97 ab9423a7 fc93a039
21  
    655b59c3 8f0ccc92 ffeff47d 85845dd1
22  
    6fa87e4f fe2ce6e0 a3014314 4e0811a1
23  
    f7537e82 bd3af235 2ad7d2bb eb86d391
24  
    67452301 efcdab89 98badcfe 10325476 ]],"(%w+)", function (s) table.insert(md5.consts, tonumber(s,16)) end)
25  
    --67452301 efcdab89 98badcfe 10325476 ]],"(%w+)", function (s) tinsert(md5.consts,tonumber(s,16)) end)
26  
27  
function md5.transform(A,B,C,D,X)
28  
  local f=function (x,y,z) return bit.bor(bit.band(x,y),bit.band(-x-1,z)) end
29  
  local g=function (x,y,z) return bit.bor(bit.band(x,z),bit.band(y,-z-1)) end
30  
  local h=function (x,y,z) return bit.bxor(x,bit.bxor(y,z)) end
31  
  local i=function (x,y,z) return bit.bxor(y,bit.bor(x,-z-1)) end
32  
  local z=function (f,a,b,c,d,x,s,ac)
33  
        a=bit.band(a+f(b,c,d)+x+ac,md5.ff)
34  
        -- be *very* careful that left shift does not cause rounding!
35  
        return bit.bor(bit.lshift(bit.band(a,bit.rshift(md5.ff,s)),s),bit.rshift(a,32-s))+b
36  
      end
37  
  local a,b,c,d=A,B,C,D
38  
  local t=md5.consts
39  
40  
  a=z(f,a,b,c,d,X[ 0], 7,t[ 1])
41  
  d=z(f,d,a,b,c,X[ 1],12,t[ 2])
42  
  c=z(f,c,d,a,b,X[ 2],17,t[ 3])
43  
  b=z(f,b,c,d,a,X[ 3],22,t[ 4])
44  
  a=z(f,a,b,c,d,X[ 4], 7,t[ 5])
45  
  d=z(f,d,a,b,c,X[ 5],12,t[ 6])
46  
  c=z(f,c,d,a,b,X[ 6],17,t[ 7])
47  
  b=z(f,b,c,d,a,X[ 7],22,t[ 8])
48  
  a=z(f,a,b,c,d,X[ 8], 7,t[ 9])
49  
  d=z(f,d,a,b,c,X[ 9],12,t[10])
50  
  c=z(f,c,d,a,b,X[10],17,t[11])
51  
  b=z(f,b,c,d,a,X[11],22,t[12])
52  
  a=z(f,a,b,c,d,X[12], 7,t[13])
53  
  d=z(f,d,a,b,c,X[13],12,t[14])
54  
  c=z(f,c,d,a,b,X[14],17,t[15])
55  
  b=z(f,b,c,d,a,X[15],22,t[16])
56  
57  
  a=z(g,a,b,c,d,X[ 1], 5,t[17])
58  
  d=z(g,d,a,b,c,X[ 6], 9,t[18])
59  
  c=z(g,c,d,a,b,X[11],14,t[19])
60  
  b=z(g,b,c,d,a,X[ 0],20,t[20])
61  
  a=z(g,a,b,c,d,X[ 5], 5,t[21])
62  
  d=z(g,d,a,b,c,X[10], 9,t[22])
63  
  c=z(g,c,d,a,b,X[15],14,t[23])
64  
  b=z(g,b,c,d,a,X[ 4],20,t[24])
65  
  a=z(g,a,b,c,d,X[ 9], 5,t[25])
66  
  d=z(g,d,a,b,c,X[14], 9,t[26])
67  
  c=z(g,c,d,a,b,X[ 3],14,t[27])
68  
  b=z(g,b,c,d,a,X[ 8],20,t[28])
69  
  a=z(g,a,b,c,d,X[13], 5,t[29])
70  
  d=z(g,d,a,b,c,X[ 2], 9,t[30])
71  
  c=z(g,c,d,a,b,X[ 7],14,t[31])
72  
  b=z(g,b,c,d,a,X[12],20,t[32])
73  
74  
  a=z(h,a,b,c,d,X[ 5], 4,t[33])
75  
  d=z(h,d,a,b,c,X[ 8],11,t[34])
76  
  c=z(h,c,d,a,b,X[11],16,t[35])
77  
  b=z(h,b,c,d,a,X[14],23,t[36])
78  
  a=z(h,a,b,c,d,X[ 1], 4,t[37])
79  
  d=z(h,d,a,b,c,X[ 4],11,t[38])
80  
  c=z(h,c,d,a,b,X[ 7],16,t[39])
81  
  b=z(h,b,c,d,a,X[10],23,t[40])
82  
  a=z(h,a,b,c,d,X[13], 4,t[41])
83  
  d=z(h,d,a,b,c,X[ 0],11,t[42])
84  
  c=z(h,c,d,a,b,X[ 3],16,t[43])
85  
  b=z(h,b,c,d,a,X[ 6],23,t[44])
86  
  a=z(h,a,b,c,d,X[ 9], 4,t[45])
87  
  d=z(h,d,a,b,c,X[12],11,t[46])
88  
  c=z(h,c,d,a,b,X[15],16,t[47])
89  
  b=z(h,b,c,d,a,X[ 2],23,t[48])
90  
91  
  a=z(i,a,b,c,d,X[ 0], 6,t[49])
92  
  d=z(i,d,a,b,c,X[ 7],10,t[50])
93  
  c=z(i,c,d,a,b,X[14],15,t[51])
94  
  b=z(i,b,c,d,a,X[ 5],21,t[52])
95  
  a=z(i,a,b,c,d,X[12], 6,t[53])
96  
  d=z(i,d,a,b,c,X[ 3],10,t[54])
97  
  c=z(i,c,d,a,b,X[10],15,t[55])
98  
  b=z(i,b,c,d,a,X[ 1],21,t[56])
99  
  a=z(i,a,b,c,d,X[ 8], 6,t[57])
100  
  d=z(i,d,a,b,c,X[15],10,t[58])
101  
  c=z(i,c,d,a,b,X[ 6],15,t[59])
102  
  b=z(i,b,c,d,a,X[13],21,t[60])
103  
  a=z(i,a,b,c,d,X[ 4], 6,t[61])
104  
  d=z(i,d,a,b,c,X[11],10,t[62])
105  
  c=z(i,c,d,a,b,X[ 2],15,t[63])
106  
  b=z(i,b,c,d,a,X[ 9],21,t[64])
107  
108  
  return A+a,B+b,C+c,D+d
109  
end
110  
111  
-- convert little-endian 32-bit int to a 4-char string
112  
local function leIstr(i)
113  
  local f=function (s) return string.char(bit.band(bit.rshift(i,s),255)) end
114  
  return f(0)..f(8)..f(16)..f(24)
115  
end
116  
117  
  -- convert raw string to big-endian int
118  
  local function beInt(s)
119  
    local v=0
120  
    for i=1,string.len(s) do v=v*256+string.byte(s,i) end
121  
    return v
122  
  end
123  
  -- convert raw string to little-endian int
124  
  local function leInt(s)
125  
    local v=0
126  
    for i=string.len(s),1,-1 do v=v*256+string.byte(s,i) end
127  
    return v
128  
  end
129  
  -- cut up a string in little-endian ints of given size
130  
  local function leStrCuts(s,...)
131  
    local arg = table.pack(...) -- Lua 5.2 fix
132  
    local o,r=1,{}
133  
    for i=1,#arg do
134  
      table.insert(r,leInt(string.sub(s,o,o+arg[i]-1)))
135  
      o=o+arg[i]
136  
    end
137  
    return r
138  
  end
139  
140  
function md5.calc(s)
141  
  local msgLen=string.len(s)
142  
  local padLen=56- msgLen % 64
143  
  if msgLen % 64 > 56 then padLen=padLen+64 end
144  
  if padLen==0 then padLen=64 end
145  
  s=s..string.char(128)..string.rep(string.char(0),padLen-1)
146  
  s=s..leIstr(8*msgLen)..leIstr(0)
147  
  assert(string.len(s) % 64 ==0)
148  
  local t=md5.consts
149  
  local a,b,c,d=t[65],t[66],t[67],t[68]
150  
  for i=1,string.len(s),64 do
151  
    local X=leStrCuts(string.sub(s,i,i+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
152  
    assert(#X==16)
153  
    X[0]=table.remove(X,1) -- zero based!
154  
    a,b,c,d=md5.transform(a,b,c,d,X)
155  
  end
156  
  local swap=function (w) return beInt(leIstr(w)) end
157  
  return string.format("%08x%08x%08x%08x",swap(a),swap(b),swap(c),swap(d))
158  
end

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #374
Snippet name: MD5 Algorithm (md5.calc)
Eternal ID of this version: #374/1
Text MD5: 4bd573da481527f1894752a8bcb2142c
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-01 21:16:42
Source code size: 5342 bytes / 158 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 836 / 186
Referenced in: [show references]