vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 -- An MD5 mplementation in Lua, requires bitlib
2 -- Written by Jean-Claude Wippler
3 -- 10/02/2001 jcw@equi4.com
4 -- Original source available at http://www.equi4.com/md5/md5calc.lua
5  
6 -- Transformed for use in GroupCalendar by John Stephen
7  
8 gGroupCalendarMD5 =
9 {
10 };
11  
12 function GroupCalendarMD5_Initialize()
13 gGroupCalendarMD5.Mask32 = tonumber("ffffffff", 16);
14 gGroupCalendarMD5.Consts = {};
15  
16 local vConstants =
17 {
18 "d76aa478", "e8c7b756", "242070db", "c1bdceee",
19 "f57c0faf", "4787c62a", "a8304613", "fd469501",
20 "698098d8", "8b44f7af", "ffff5bb1", "895cd7be",
21 "6b901122", "fd987193", "a679438e", "49b40821",
22  
23 "f61e2562", "c040b340", "265e5a51", "e9b6c7aa",
24 "d62f105d", "02441453", "d8a1e681", "e7d3fbc8",
25 "21e1cde6", "c33707d6", "f4d50d87", "455a14ed",
26 "a9e3e905", "fcefa3f8", "676f02d9", "8d2a4c8a",
27  
28 "fffa3942", "8771f681", "6d9d6122", "fde5380c",
29 "a4beea44", "4bdecfa9", "f6bb4b60", "bebfbc70",
30 "289b7ec6", "eaa127fa", "d4ef3085", "04881d05",
31 "d9d4d039", "e6db99e5", "1fa27cf8", "c4ac5665",
32  
33 "f4292244", "432aff97", "ab9423a7", "fc93a039",
34 "655b59c3", "8f0ccc92", "ffeff47d", "85845dd1",
35 "6fa87e4f", "fe2ce6e0", "a3014314", "4e0811a1",
36 "f7537e82", "bd3af235", "2ad7d2bb", "eb86d391",
37  
38 "67452301", "efcdab89", "98badcfe", "10325476",
39 };
40  
41 for vIndex, vConstStr in vConstants do
42 gGroupCalendarMD5.Consts[vIndex] = tonumber(vConstStr, 16);
43 end
44 end
45  
46 function GroupCalendarMD5_f(x, y, z)
47 return bit.bor(bit.band(x, y), bit.band(-x - 1, z));
48 end
49  
50 function GroupCalendarMD5_g(x, y, z)
51 return bit.bor(bit.band(x, z), bit.band(y, -z - 1));
52 end
53  
54 function GroupCalendarMD5_h(x, y, z)
55 return bit.bxor(x, bit.bxor(y, z));
56 end
57  
58 function GroupCalendarMD5_i(x, y, z)
59 return bit.bxor(y, bit.bor(x, -z - 1));
60 end
61  
62 function GroupCalendarMD5_z(f,a,b,c,d,x,s,ac)
63 a = bit.band(a + f(b, c, d) + x + ac, gGroupCalendarMD5.Mask32);
64  
65 -- be *very* careful that left shift does not cause rounding!
66  
67 return bit.bor(bit.lshift(bit.band(a, bit.rshift(gGroupCalendarMD5.Mask32, s)), s), bit.rshift(a, 32 - s)) + b;
68 end
69  
70 function GroupCalendarMD5_Transform(A,B,C,D,X)
71 local a, b, c, d = A, B, C, D;
72 local t = gGroupCalendarMD5.Consts;
73  
74 -- Round 1
75  
76 a=GroupCalendarMD5_z(GroupCalendarMD5_f,a,b,c,d,X[ 1], 7,t[ 1])
77 d=GroupCalendarMD5_z(GroupCalendarMD5_f,d,a,b,c,X[ 2],12,t[ 2])
78 c=GroupCalendarMD5_z(GroupCalendarMD5_f,c,d,a,b,X[ 3],17,t[ 3])
79 b=GroupCalendarMD5_z(GroupCalendarMD5_f,b,c,d,a,X[ 4],22,t[ 4])
80 a=GroupCalendarMD5_z(GroupCalendarMD5_f,a,b,c,d,X[ 5], 7,t[ 5])
81 d=GroupCalendarMD5_z(GroupCalendarMD5_f,d,a,b,c,X[ 6],12,t[ 6])
82 c=GroupCalendarMD5_z(GroupCalendarMD5_f,c,d,a,b,X[ 7],17,t[ 7])
83 b=GroupCalendarMD5_z(GroupCalendarMD5_f,b,c,d,a,X[ 8],22,t[ 8])
84 a=GroupCalendarMD5_z(GroupCalendarMD5_f,a,b,c,d,X[ 9], 7,t[ 9])
85 d=GroupCalendarMD5_z(GroupCalendarMD5_f,d,a,b,c,X[10],12,t[10])
86 c=GroupCalendarMD5_z(GroupCalendarMD5_f,c,d,a,b,X[11],17,t[11])
87 b=GroupCalendarMD5_z(GroupCalendarMD5_f,b,c,d,a,X[12],22,t[12])
88 a=GroupCalendarMD5_z(GroupCalendarMD5_f,a,b,c,d,X[13], 7,t[13])
89 d=GroupCalendarMD5_z(GroupCalendarMD5_f,d,a,b,c,X[14],12,t[14])
90 c=GroupCalendarMD5_z(GroupCalendarMD5_f,c,d,a,b,X[15],17,t[15])
91 b=GroupCalendarMD5_z(GroupCalendarMD5_f,b,c,d,a,X[16],22,t[16])
92  
93 -- Round 2
94  
95 a=GroupCalendarMD5_z(GroupCalendarMD5_g,a,b,c,d,X[ 2], 5,t[17])
96 d=GroupCalendarMD5_z(GroupCalendarMD5_g,d,a,b,c,X[ 7], 9,t[18])
97 c=GroupCalendarMD5_z(GroupCalendarMD5_g,c,d,a,b,X[12],14,t[19])
98 b=GroupCalendarMD5_z(GroupCalendarMD5_g,b,c,d,a,X[ 1],20,t[20])
99 a=GroupCalendarMD5_z(GroupCalendarMD5_g,a,b,c,d,X[ 6], 5,t[21])
100 d=GroupCalendarMD5_z(GroupCalendarMD5_g,d,a,b,c,X[11], 9,t[22])
101 c=GroupCalendarMD5_z(GroupCalendarMD5_g,c,d,a,b,X[16],14,t[23])
102 b=GroupCalendarMD5_z(GroupCalendarMD5_g,b,c,d,a,X[ 5],20,t[24])
103 a=GroupCalendarMD5_z(GroupCalendarMD5_g,a,b,c,d,X[10], 5,t[25])
104 d=GroupCalendarMD5_z(GroupCalendarMD5_g,d,a,b,c,X[15], 9,t[26])
105 c=GroupCalendarMD5_z(GroupCalendarMD5_g,c,d,a,b,X[ 4],14,t[27])
106 b=GroupCalendarMD5_z(GroupCalendarMD5_g,b,c,d,a,X[ 9],20,t[28])
107 a=GroupCalendarMD5_z(GroupCalendarMD5_g,a,b,c,d,X[14], 5,t[29])
108 d=GroupCalendarMD5_z(GroupCalendarMD5_g,d,a,b,c,X[ 3], 9,t[30])
109 c=GroupCalendarMD5_z(GroupCalendarMD5_g,c,d,a,b,X[ 8],14,t[31])
110 b=GroupCalendarMD5_z(GroupCalendarMD5_g,b,c,d,a,X[13],20,t[32])
111  
112 -- Round 3
113  
114 a=GroupCalendarMD5_z(GroupCalendarMD5_h,a,b,c,d,X[ 6], 4,t[33])
115 d=GroupCalendarMD5_z(GroupCalendarMD5_h,d,a,b,c,X[ 9],11,t[34])
116 c=GroupCalendarMD5_z(GroupCalendarMD5_h,c,d,a,b,X[12],16,t[35])
117 b=GroupCalendarMD5_z(GroupCalendarMD5_h,b,c,d,a,X[15],23,t[36])
118 a=GroupCalendarMD5_z(GroupCalendarMD5_h,a,b,c,d,X[ 2], 4,t[37])
119 d=GroupCalendarMD5_z(GroupCalendarMD5_h,d,a,b,c,X[ 5],11,t[38])
120 c=GroupCalendarMD5_z(GroupCalendarMD5_h,c,d,a,b,X[ 8],16,t[39])
121 b=GroupCalendarMD5_z(GroupCalendarMD5_h,b,c,d,a,X[11],23,t[40])
122 a=GroupCalendarMD5_z(GroupCalendarMD5_h,a,b,c,d,X[14], 4,t[41])
123 d=GroupCalendarMD5_z(GroupCalendarMD5_h,d,a,b,c,X[ 1],11,t[42])
124 c=GroupCalendarMD5_z(GroupCalendarMD5_h,c,d,a,b,X[ 4],16,t[43])
125 b=GroupCalendarMD5_z(GroupCalendarMD5_h,b,c,d,a,X[ 7],23,t[44])
126 a=GroupCalendarMD5_z(GroupCalendarMD5_h,a,b,c,d,X[10], 4,t[45])
127 d=GroupCalendarMD5_z(GroupCalendarMD5_h,d,a,b,c,X[13],11,t[46])
128 c=GroupCalendarMD5_z(GroupCalendarMD5_h,c,d,a,b,X[16],16,t[47])
129 b=GroupCalendarMD5_z(GroupCalendarMD5_h,b,c,d,a,X[ 3],23,t[48])
130  
131 -- Round 4
132  
133 a=GroupCalendarMD5_z(GroupCalendarMD5_i,a,b,c,d,X[ 1], 6,t[49])
134 d=GroupCalendarMD5_z(GroupCalendarMD5_i,d,a,b,c,X[ 8],10,t[50])
135 c=GroupCalendarMD5_z(GroupCalendarMD5_i,c,d,a,b,X[15],15,t[51])
136 b=GroupCalendarMD5_z(GroupCalendarMD5_i,b,c,d,a,X[ 6],21,t[52])
137 a=GroupCalendarMD5_z(GroupCalendarMD5_i,a,b,c,d,X[13], 6,t[53])
138 d=GroupCalendarMD5_z(GroupCalendarMD5_i,d,a,b,c,X[ 4],10,t[54])
139 c=GroupCalendarMD5_z(GroupCalendarMD5_i,c,d,a,b,X[11],15,t[55])
140 b=GroupCalendarMD5_z(GroupCalendarMD5_i,b,c,d,a,X[ 2],21,t[56])
141 a=GroupCalendarMD5_z(GroupCalendarMD5_i,a,b,c,d,X[ 9], 6,t[57])
142 d=GroupCalendarMD5_z(GroupCalendarMD5_i,d,a,b,c,X[16],10,t[58])
143 c=GroupCalendarMD5_z(GroupCalendarMD5_i,c,d,a,b,X[ 7],15,t[59])
144 b=GroupCalendarMD5_z(GroupCalendarMD5_i,b,c,d,a,X[14],21,t[60])
145 a=GroupCalendarMD5_z(GroupCalendarMD5_i,a,b,c,d,X[ 5], 6,t[61])
146 d=GroupCalendarMD5_z(GroupCalendarMD5_i,d,a,b,c,X[12],10,t[62])
147 c=GroupCalendarMD5_z(GroupCalendarMD5_i,c,d,a,b,X[ 3],15,t[63])
148 b=GroupCalendarMD5_z(GroupCalendarMD5_i,b,c,d,a,X[10],21,t[64])
149  
150 return A+a,B+b,C+c,D+d
151 end
152  
153 function GroupCalendarMD5_Calc(s)
154 local msgLen=strlen(s)
155 local padLen=56-bit.mod(msgLen,64)
156  
157 if bit.mod(msgLen,64)>56 then padLen=padLen+64 end
158  
159 if padLen==0 then padLen=64 end
160  
161 s = s..strchar(128)..strrep(strchar(0),padLen-1)
162 s = s..GroupCalendar_LEToString(8*msgLen)..GroupCalendar_LEToString(0)
163  
164 local a, b, c, d = gGroupCalendarMD5.Consts[65], gGroupCalendarMD5.Consts[66], gGroupCalendarMD5.Consts[67], gGroupCalendarMD5.Consts[68];
165  
166 for i = 1, strlen(s), 64 do
167 local X = GroupCalendar_SliceStringToLEs(strsub(s, i, i + 63));
168 a, b, c, d = GroupCalendarMD5_Transform(a, b, c, d, X);
169 end
170  
171 local swap = function (w) return GroupCalendar_StringToBE4(GroupCalendar_LEToString(w)) end
172  
173 return format("%08x%08x%08x%08x", swap(a), swap(b), swap(c), swap(d));
174 end
175  
176 -- convert little-endian 32-bit int to a 4-char string
177  
178 function GroupCalendar_LEToString(i)
179 i = math.floor(i);
180  
181 local f = function (s) return strchar(bit.band(bit.rshift(i,s),255)) end
182  
183 return f(0)..f(8)..f(16)..f(24)
184 end
185  
186 -- convert raw string to big-endian int
187  
188 function GroupCalendar_StringToBE4(str)
189 return 256 * (256 * (256 * strbyte(str, 1) + strbyte(str, 2)) + strbyte(str, 3)) + strbyte(str, 4);
190 end
191  
192 -- cut up a string in little-endian ints of given size
193  
194 function GroupCalendar_SliceStringToLEs(s)
195 local r = {};
196 local o = 1;
197  
198 for i = 1, 16 do
199 local str = strsub(s, o, o + 3);
200 r[i] = 256 * (256 * (256 * strbyte(str, 4) + strbyte(str, 3)) + strbyte(str, 2)) + strbyte(str, 1);
201 o = o + 4;
202 end
203  
204 return r;
205 end
206  
207 function GroupCalendarMD5_Verify()
208 s0 = 'message digest'
209 s1 = 'abcdefghijklmnopqrstuvwxyz'
210 s2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
211 s3 = '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
212  
213 if GroupCalendarMD5_Calc('') ~= 'd41d8cd98f00b204e9800998ecf8427e' then
214 Calendar_ErrorMessage("GroupCalendarMD5: Test 1 failed");
215 return;
216 end
217  
218 if GroupCalendarMD5_Calc('a') ~= '0cc175b9c0f1b6a831c399e269772661' then
219 Calendar_ErrorMessage("GroupCalendarMD5: Test 2 failed");
220 return;
221 end
222  
223 if GroupCalendarMD5_Calc('abc') ~= '900150983cd24fb0d6963f7d28e17f72' then
224 Calendar_ErrorMessage("GroupCalendarMD5: Test 3 failed");
225 return;
226 end
227  
228 if GroupCalendarMD5_Calc(s0) ~= 'f96b697d7cb7938d525a2f31aaf161d0' then
229 Calendar_ErrorMessage("GroupCalendarMD5: Test 4 failed");
230 return;
231 end
232  
233 if GroupCalendarMD5_Calc(s1) ~= 'c3fcd3d76192e4007dfb496cca67e13b' then
234 Calendar_ErrorMessage("GroupCalendarMD5: Test 5 failed");
235 return;
236 end
237  
238 if GroupCalendarMD5_Calc(s2) ~= 'd174ab98d277d9f5a5611c2c9f419d9f' then
239 Calendar_ErrorMessage("GroupCalendarMD5: Test 6 failed");
240 return;
241 end
242  
243 if GroupCalendarMD5_Calc(s3) ~= '57edf4a22be3c955ac49da2e2107b67a' then
244 Calendar_ErrorMessage("GroupCalendarMD5: Test 7 failed");
245 return;
246 end
247  
248 Calendar_DebugMessage("GroupCalendarMD5: All tests passed");
249 end
250  
251 function GroupCalendarMD5_CheckPerformance()
252 if 1 then
253 sizes={10,50,100,500,1000,5000,10000}
254  
255 for i=1,getn(sizes) do
256 local s=strrep(' ',sizes[i])
257 debugprofilestart();
258  
259 for j=1,10 do
260 GroupCalendarMD5_Calc(s)
261 end
262  
263 local elapsed = math.floor(debugprofilestop() / 10);
264  
265 Calendar_DebugMessage(format('%6d bytes: %4d ms', sizes[i], elapsed))
266 end
267 end
268 end