-rw-r--r-- | qmake/qtmd5.cpp | 304 |
1 files changed, 304 insertions, 0 deletions
diff --git a/qmake/qtmd5.cpp b/qmake/qtmd5.cpp new file mode 100644 index 0000000..03c60c3 --- a/dev/null +++ b/qmake/qtmd5.cpp | |||
@@ -0,0 +1,304 @@ | |||
1 | /* derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm */ | ||
2 | #include <qstring.h> | ||
3 | #include "qtmd5.h" | ||
4 | |||
5 | typedef unsigned char *POINTER; | ||
6 | typedef unsigned short int UINT2; | ||
7 | typedef unsigned long int UINT4; | ||
8 | |||
9 | typedef struct { | ||
10 | UINT4 state[4]; // state(ABCD) | ||
11 | UINT4 count[2]; // number of bits, modulo 2^64(lsb first) | ||
12 | unsigned char buffer[64]; // input buffer | ||
13 | } MD5_CTX; | ||
14 | |||
15 | static void MD5Init(MD5_CTX *); | ||
16 | static void MD5Update(MD5_CTX *, unsigned char *, unsigned int); | ||
17 | static void MD5Final(unsigned char [16], MD5_CTX *); | ||
18 | static void MD5Transform(UINT4[4], unsigned char[64]); | ||
19 | static void Encode(unsigned char *, UINT4 *, unsigned int); | ||
20 | static void Decode(UINT4 *, unsigned char *, unsigned int); | ||
21 | static void MD5_memset(POINTER, int, unsigned int); | ||
22 | static void MD5_memcpy(POINTER output,POINTER input,unsigned int len); | ||
23 | |||
24 | // Constants for MD5Transform routine. | ||
25 | enum { | ||
26 | S11 = 7, S12 = 12, S13 = 17, S14 = 22, S21 = 5, S22 = 9, S23 = 14, S24 = 20, | ||
27 | S31 = 4, S32 = 11, S33 = 16, S34 = 23, S41 = 6, S42 = 10, S43 = 15, S44 = 21 | ||
28 | }; | ||
29 | |||
30 | static unsigned char PADDING[64] = { | ||
31 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
32 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
33 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
34 | }; | ||
35 | |||
36 | // F, G, H and I are basic MD5 functions. | ||
37 | static inline UINT4 F(UINT4 x, UINT4 y, UINT4 z) | ||
38 | { | ||
39 | return(x & y) |((~x) & z); | ||
40 | } | ||
41 | |||
42 | static inline UINT4 G(UINT4 x, UINT4 y, UINT4 z) | ||
43 | { | ||
44 | return(x & z) |(y &(~z)); | ||
45 | } | ||
46 | |||
47 | static inline UINT4 H(UINT4 x, UINT4 y, UINT4 z) | ||
48 | { | ||
49 | return x ^ y ^ z; | ||
50 | } | ||
51 | |||
52 | static inline UINT4 I(UINT4 x, UINT4 y, UINT4 z) | ||
53 | { | ||
54 | return y ^(x |(~z)); | ||
55 | } | ||
56 | |||
57 | static inline UINT4 rotateLeft(UINT4 x, UINT4 n) | ||
58 | { | ||
59 | return(x << n) |(x >>(32-(n))); | ||
60 | } | ||
61 | |||
62 | // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. | ||
63 | // Rotation is separate from addition to prevent recomputation. | ||
64 | static inline void FF(UINT4 &a, UINT4 b, UINT4 c, UINT4 d, UINT4 x, UINT4 s, UINT4 ac) | ||
65 | { | ||
66 | a += F(b, c, d) + x + ac; | ||
67 | a = rotateLeft(a, s); | ||
68 | a += b; | ||
69 | } | ||
70 | |||
71 | static inline void GG(UINT4 &a, UINT4 b, UINT4 c, UINT4 d, UINT4 x, UINT4 s, UINT4 ac) | ||
72 | { | ||
73 | a += G(b, c, d) + x +(UINT4)(ac); | ||
74 | a = rotateLeft(a, s); | ||
75 | a += b; | ||
76 | } | ||
77 | |||
78 | static inline void HH(UINT4 &a, UINT4 b, UINT4 c, UINT4 d, UINT4 x, UINT4 s, UINT4 ac) | ||
79 | { | ||
80 | a += H(b, c, d) + x +(UINT4)(ac); | ||
81 | a = rotateLeft(a, s); | ||
82 | a += b; | ||
83 | } | ||
84 | |||
85 | static inline void II(UINT4 &a, UINT4 b, UINT4 c, UINT4 d, UINT4 x, UINT4 s, UINT4 ac) | ||
86 | { | ||
87 | a += I(b, c, d) + x +(UINT4)(ac); | ||
88 | a = rotateLeft(a, s); | ||
89 | a += b; | ||
90 | } | ||
91 | |||
92 | // MD5 initialization. Begins an MD5 operation, writing a new context. | ||
93 | static void MD5Init(MD5_CTX *context) | ||
94 | { | ||
95 | context->count[0] = context->count[1] = 0; | ||
96 | |||
97 | // Load magic initialization constants. | ||
98 | context->state[0] = 0x67452301; | ||
99 | context->state[1] = 0xefcdab89; | ||
100 | context->state[2] = 0x98badcfe; | ||
101 | context->state[3] = 0x10325476; | ||
102 | } | ||
103 | |||
104 | // MD5 block update operation. Continues an MD5 message-digest | ||
105 | // operation, processing another message block, and updating the | ||
106 | // context. | ||
107 | static void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen) | ||
108 | { | ||
109 | unsigned int i, index, partLen; | ||
110 | // Compute number of bytes mod 64 | ||
111 | index =(unsigned int)((context->count[0] >> 3) & 0x3F); | ||
112 | |||
113 | // Update number of bits | ||
114 | if ((context->count[0] +=((UINT4)inputLen << 3)) <((UINT4)inputLen << 3)) | ||
115 | context->count[1]++; | ||
116 | |||
117 | context->count[1] +=((UINT4)inputLen >> 29); | ||
118 | partLen = 64 - index; | ||
119 | |||
120 | // Transform as many times as possible. | ||
121 | if (inputLen >= partLen) { | ||
122 | MD5_memcpy((POINTER)&context->buffer[index],(POINTER)input, partLen); | ||
123 | MD5Transform(context->state, context->buffer); | ||
124 | for (i = partLen; i + 63 < inputLen; i += 64) | ||
125 | MD5Transform(context->state, &input[i]); | ||
126 | index = 0; | ||
127 | } else { | ||
128 | i = 0; | ||
129 | } | ||
130 | |||
131 | // Buffer remaining input | ||
132 | MD5_memcpy((POINTER)&context->buffer[index],(POINTER)&input[i], | ||
133 | inputLen-i); | ||
134 | } | ||
135 | |||
136 | // MD5 finalization. Ends an MD5 message-digest operation, writing the | ||
137 | // the message digest and zeroizing the context. | ||
138 | static void MD5Final(unsigned char digest[16], MD5_CTX *context) | ||
139 | { | ||
140 | unsigned char bits[8]; | ||
141 | unsigned int index, padLen; | ||
142 | |||
143 | // Save number of bits | ||
144 | Encode(bits, context->count, 8); | ||
145 | |||
146 | // Pad out to 56 mod 64. | ||
147 | index =(unsigned int)((context->count[0] >> 3) & 0x3f); | ||
148 | padLen =(index < 56) ?(56 - index) :(120 - index); | ||
149 | MD5Update(context, PADDING, padLen); | ||
150 | |||
151 | // Append length(before padding) | ||
152 | MD5Update(context, bits, 8); | ||
153 | |||
154 | // Store state in digest | ||
155 | Encode(digest, context->state, 16); | ||
156 | |||
157 | // Zeroize sensitive information. | ||
158 | MD5_memset((POINTER)context, 0, sizeof(*context)); | ||
159 | } | ||
160 | |||
161 | // MD5 basic transformation. Transforms state based on block. | ||
162 | static void MD5Transform(UINT4 state[4], unsigned char block[64]) | ||
163 | { | ||
164 | UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; | ||
165 | Decode(x, block, 64); | ||
166 | |||
167 | // Round 1 | ||
168 | FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ | ||
169 | FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ | ||
170 | FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ | ||
171 | FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ | ||
172 | FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ | ||
173 | FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ | ||
174 | FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ | ||
175 | FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ | ||
176 | FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ | ||
177 | FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ | ||
178 | FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ | ||
179 | FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ | ||
180 | FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ | ||
181 | FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ | ||
182 | FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ | ||
183 | FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ | ||
184 | |||
185 | // Round 2 | ||
186 | GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ | ||
187 | GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ | ||
188 | GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ | ||
189 | GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ | ||
190 | GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ | ||
191 | GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ | ||
192 | GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ | ||
193 | GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ | ||
194 | GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ | ||
195 | GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ | ||
196 | GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ | ||
197 | GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ | ||
198 | GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ | ||
199 | GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ | ||
200 | GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ | ||
201 | GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ | ||
202 | |||
203 | // Round 3 | ||
204 | HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ | ||
205 | HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ | ||
206 | HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ | ||
207 | HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ | ||
208 | HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ | ||
209 | HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ | ||
210 | HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ | ||
211 | HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ | ||
212 | HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ | ||
213 | HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ | ||
214 | HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ | ||
215 | HH(b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ | ||
216 | HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ | ||
217 | HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ | ||
218 | HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ | ||
219 | HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ | ||
220 | |||
221 | // Round 4 | ||
222 | II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ | ||
223 | II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ | ||
224 | II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ | ||
225 | II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ | ||
226 | II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ | ||
227 | II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ | ||
228 | II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ | ||
229 | II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ | ||
230 | II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ | ||
231 | II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ | ||
232 | II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ | ||
233 | II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ | ||
234 | II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ | ||
235 | II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ | ||
236 | II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ | ||
237 | II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ | ||
238 | state[0] += a; | ||
239 | state[1] += b; | ||
240 | state[2] += c; | ||
241 | state[3] += d; | ||
242 | |||
243 | // Zeroize sensitive information. | ||
244 | MD5_memset((POINTER)x, 0, sizeof(x)); | ||
245 | } | ||
246 | |||
247 | // Encodes input(UINT4) into output(unsigned char). Assumes len is a | ||
248 | // multiple of 4. | ||
249 | static void Encode(unsigned char *output, UINT4 *input, unsigned int len) | ||
250 | { | ||
251 | unsigned int i, j; | ||
252 | for (i = 0, j = 0; j < len; i++, j += 4) { | ||
253 | output[j] = (unsigned char) (input[i] & 0xff); | ||
254 | output[j+1] = (unsigned char) ((input[i] >> 8) & 0xff); | ||
255 | output[j+2] = (unsigned char) ((input[i] >> 16) & 0xff); | ||
256 | output[j+3] = (unsigned char) ((input[i] >> 24) & 0xff); | ||
257 | } | ||
258 | } | ||
259 | |||
260 | // Decodes input(unsigned char) into output(UINT4). Assumes len is a | ||
261 | // multiple of 4. | ||
262 | static void Decode(UINT4 *output, unsigned char *input, unsigned int len) | ||
263 | { | ||
264 | unsigned int i, j; | ||
265 | for (i = 0, j = 0; j < len; i++, j += 4) | ||
266 | output[i] =((UINT4)input[j]) |(((UINT4)input[j+1]) << 8) | | ||
267 | (((UINT4)input[j+2]) << 16) |(((UINT4)input[j+3]) << 24); | ||
268 | } | ||
269 | |||
270 | // Note: Replace "for loop" with standard memset if possible. | ||
271 | static void MD5_memset(POINTER output, int value, unsigned int len) | ||
272 | { | ||
273 | unsigned int i; | ||
274 | for (i = 0; i < len; i++) | ||
275 | ((char *)output)[i] =(char)value; | ||
276 | } | ||
277 | |||
278 | // Note: Replace "for loop" with standard memcpy if possible. | ||
279 | static void MD5_memcpy(POINTER output,POINTER input,unsigned int len) | ||
280 | { | ||
281 | unsigned int i; | ||
282 | for (i = 0; i < len; i++) | ||
283 | output[i] = input[i]; | ||
284 | } | ||
285 | |||
286 | void qtMD5(const QByteArray &src, unsigned char *digest) | ||
287 | { | ||
288 | MD5_CTX context; | ||
289 | |||
290 | MD5Init(&context); | ||
291 | MD5Update(&context, (unsigned char *) src.data(), src.size()); | ||
292 | MD5Final(digest, &context); | ||
293 | } | ||
294 | |||
295 | QString qtMD5(const QByteArray &src) | ||
296 | { | ||
297 | unsigned char digest[16]; | ||
298 | qtMD5(src, digest); | ||
299 | |||
300 | QString output, tmp; | ||
301 | for (int i = 0; i < 16; ++i) | ||
302 | output += tmp.sprintf("%02x", digest[i]); | ||
303 | return output; | ||
304 | } | ||