# Untitled

By: a guest on Aug 21st, 2012  |  syntax: None  |  size: 10.86 KB  |  hits: 18  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. //MD5 random shizzle 4 seed trickenZ
2. // 16 bytes == 128 bit digest
3. #define MD5_DIGEST_LENGTH 16
4.
5. // MD5 Hash
6. typedef struct
7. {
8.         unsigned int    buf[4];
9.     unsigned int        bits[2];
10.     unsigned char       in[64];
11. } MD5Context_t;
12.
13. // The four core functions - F1 is optimized somewhat
14. // #define F1(x, y, z) (x & y | ~x & z)
15. #define F1(x, y, z) (z ^ (x & (y ^ z)))
16. #define F2(x, y, z) F1(z, x, y)
17. #define F3(x, y, z) (x ^ y ^ z)
18. #define F4(x, y, z) (y ^ (x | ~z))
19.
20. // This is the central step in the MD5 algorithm.
21. #define MD5STEP(f, w, x, y, z, data, s) \
22.         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
23.
24. //-----------------------------------------------------------------------------
25. // Purpose: The core of the MD5 algorithm, this alters an existing MD5 hash to
26. //  reflect the addition of 16 longwords of new data.  MD5Update blocks
27. //  the data and converts bytes into longwords for this routine.
28. // Input  : buf[4] -
29. //                      in[16] -
30. // Output : static void
31. //-----------------------------------------------------------------------------
32. static void MD5Transform(unsigned int buf[4], unsigned int const in[16])
33. {
34.     register unsigned int a, b, c, d;
35.
36.     a = buf[0];
37.     b = buf[1];
38.     c = buf[2];
39.     d = buf[3];
40.
41.     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
42.     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
43.     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
44.     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
45.     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
46.     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
47.     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
48.     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
49.     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
50.     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
51.     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
52.     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
53.     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
54.     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
55.     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
56.     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
57.
58.     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
59.     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
60.     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
61.     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
62.     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
63.     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
64.     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
65.     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
66.     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
67.     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
68.     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
69.     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
70.     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
71.     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
72.     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
73.     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
74.
75.     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
76.     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
77.     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
78.     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
79.     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
80.     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
81.     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
82.     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
83.     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
84.     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
85.     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
86.     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
87.     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
88.     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
89.     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
90.     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
91.
92.     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
93.     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
94.     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
95.     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
96.     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
97.     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
98.     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
99.     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
100.     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
101.     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
102.     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
103.     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
104.     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
105.     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
106.     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
107.     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
108.
109.     buf[0] += a;
110.     buf[1] += b;
111.     buf[2] += c;
112.     buf[3] += d;
113. }
114.
115. //-----------------------------------------------------------------------------
116. // Purpose: Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious initialization constants.
117.
118. // Input  : *ctx -
119. //-----------------------------------------------------------------------------
120. void MD5Init(MD5Context_t *ctx)
121. {
122.     ctx->buf[0] = 0x67452301;
123.     ctx->buf[1] = 0xefcdab89;
125.     ctx->buf[3] = 0x10325476;
126.
127.     ctx->bits[0] = 0;
128.     ctx->bits[1] = 0;
129. }
130.
131. //-----------------------------------------------------------------------------
132. // Purpose: Update context to reflect the concatenation of another buffer full of bytes.
133. // Input  : *ctx -
134. //                      *buf -
135. //                      len -
136. //-----------------------------------------------------------------------------
137. void MD5Update(MD5Context_t *ctx, unsigned char const *buf, unsigned int len)
138. {
139.     unsigned int t;
140.
141.     /* Update bitcount */
142.
143.     t = ctx->bits[0];
144.     if ((ctx->bits[0] = t + ((unsigned int) len << 3)) < t)
145.         ctx->bits[1]++;         /* Carry from low to high */
146.     ctx->bits[1] += len >> 29;
147.
148.     t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
149.
150.     /* Handle any leading odd-sized chunks */
151.
152.     if (t)
153.         {
154.         unsigned char *p = (unsigned char *) ctx->in + t;
155.
156.         t = 64 - t;
157.         if (len < t)
158.                 {
159.             memcpy(p, buf, len);
160.             return;
161.         }
162.         memcpy(p, buf, t);
163.         //byteReverse(ctx->in, 16);
164.         MD5Transform(ctx->buf, (unsigned int *) ctx->in);
165.         buf += t;
166.         len -= t;
167.     }
168.     /* Process data in 64-byte chunks */
169.
170.     while (len >= 64)
171.         {
172.         memcpy(ctx->in, buf, 64);
173.         //byteReverse(ctx->in, 16);
174.         MD5Transform(ctx->buf, (unsigned int *) ctx->in);
175.         buf += 64;
176.         len -= 64;
177.     }
178.
179.     /* Handle any remaining bytes of data. */
180.     memcpy(ctx->in, buf, len);
181. }
182.
183. //-----------------------------------------------------------------------------
184. // Purpose: Final wrapup - pad to 64-byte boundary with the bit pattern
185. // 1 0* (64-bit count of bits processed, MSB-first)
186. // Input  : digest[MD5_DIGEST_LENGTH] -
187. //                      *ctx -
188. //-----------------------------------------------------------------------------
189. void MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5Context_t *ctx)
190. {
191.     unsigned count;
192.     unsigned char *p;
193.
194.     /* Compute number of bytes mod 64 */
195.     count = (ctx->bits[0] >> 3) & 0x3F;
196.
197.     /* Set the first char of padding to 0x80.  This is safe since there is
198.        always at least one byte free */
199.     p = ctx->in + count;
200.     *p++ = 0x80;
201.
202.     /* Bytes of padding needed to make 64 bytes */
203.     count = 64 - 1 - count;
204.
205.     /* Pad out to 56 mod 64 */
206.     if (count < 8)
207.         {
208.         /* Two lots of padding:  Pad the first block to 64 bytes */
209.         memset(p, 0, count);
210.         //byteReverse(ctx->in, 16);
211.         MD5Transform(ctx->buf, (unsigned int *) ctx->in);
212.
213.         /* Now fill the next block with 56 bytes */
214.         memset(ctx->in, 0, 56);
215.     }
216.         else
217.         {
218.         /* Pad block to 56 bytes */
219.         memset(p, 0, count - 8);
220.     }
221.     //byteReverse(ctx->in, 14);
222.
223.     /* Append length in bits and transform */
224.     ((unsigned int *) ctx->in)[14] = ctx->bits[0];
225.     ((unsigned int *) ctx->in)[15] = ctx->bits[1];
226.
227.     MD5Transform(ctx->buf, (unsigned int *) ctx->in);
228.     //byteReverse((unsigned char *) ctx->buf, 4);
229.     memcpy(digest, ctx->buf, MD5_DIGEST_LENGTH);
230.     memset(ctx, 0, sizeof(ctx));        /* In case it's sensitive */
231. }
232.
233. char *V_strncat(char *pDest, const char *pSrc, size_t destBufferSize, int max_chars_to_copy )
234. {
235.         size_t charstocopy = (size_t)0;
236.
237.         Assert( destBufferSize >= 0 );
238.         //AssertValidStringPtr( pDest);
239.         //AssertValidStringPtr( pSrc );
240.
241.         size_t len = strlen(pDest);
242.         size_t srclen = strlen( pSrc );
243.         if ( max_chars_to_copy <= COPY_ALL_CHARACTERS )
244.         {
245.                 charstocopy = srclen;
246.         }
247.         else
248.         {
249.                 charstocopy = (size_t)MIN( max_chars_to_copy, (int)srclen );
250.         }
251.
252.         if ( len + charstocopy >= destBufferSize )
253.         {
254.                 charstocopy = destBufferSize - len - 1;
255.         }
256.
257.         if ( !charstocopy )
258.         {
259.                 return pDest;
260.         }
261.
262.         char *pOut = strncat( pDest, pSrc, charstocopy );
263.         pOut[destBufferSize-1] = 0;
264.         return pOut;
265. }
266.
267. int V_snprintf( char *pDest, int maxLen, char const *pFormat, ... )
268. {
269.         Assert( maxLen >= 0 );
270.         //AssertValidWritePtr( pDest, maxLen );
271.         //AssertValidStringPtr( pFormat );
272.
273.         va_list marker;
274.
275.         va_start( marker, pFormat );
276. #ifdef _WIN32
277.         int len = _vsnprintf( pDest, maxLen, pFormat, marker );
278. #elif defined _LINUX || defined __APPLE__
279.         int len = vsnprintf( pDest, maxLen, pFormat, marker );
280. #else
281.         #error "define vsnprintf type."
282. #endif
283.         va_end( marker );
284.
285.         // Len < 0 represents an overflow
286.         if( len < 0 )
287.         {
288.                 len = maxLen;
289.                 pDest[maxLen-1] = 0;
290.         }
291.
292.         return len;
293. }
294.
295. void V_binarytohex( const byte *in, int inputbytes, char *out, int outsize )
296. {
297.         Assert( outsize >= 1 );
298.         char doublet[10];
299.         int i;
300.
301.         out[0]=0;
302.
303.         for ( i = 0; i < inputbytes; i++ )
304.         {
305.                 unsigned char c = in[i];
306.                 V_snprintf( doublet, sizeof( doublet ), "%02x", c );
307.                 V_strncat( out, doublet, outsize, COPY_ALL_CHARACTERS );
308.         }
309. }
310.
311. //-----------------------------------------------------------------------------
312. // Purpose:
313. // Input  : *hash -
314. //                      hashlen -
315. // Output : char
316. //-----------------------------------------------------------------------------
317. char *MD5_Print( unsigned char *hash, int hashlen )
318. {
319.         static char szReturn[64];
320.
321.         Assert( hashlen <= 32 );
322.
323.         V_binarytohex( hash, hashlen, szReturn, sizeof( szReturn ) );
324.         return szReturn;
325. }
326.
327. //-----------------------------------------------------------------------------
328. // Purpose: generate pseudo random number from a seed number
329. // Input  : seed number
330. // Output : pseudo random number
331. //-----------------------------------------------------------------------------
332. unsigned int MD5_PseudoRandom(unsigned int nSeed)
333. {
334.         MD5Context_t ctx;
335.         unsigned char digest[MD5_DIGEST_LENGTH]; // The MD5 Hash
336.
337.         memset( &ctx, 0, sizeof( ctx ) );
338.
339.         MD5Init(&ctx);
340.         MD5Update(&ctx, (unsigned char*)&nSeed, sizeof(nSeed) );
341.         MD5Final(digest, &ctx);
342.
343.         return *(unsigned int*)(digest+6);      // use 4 middle bytes for random value
344. }