1
0

md5.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*----------------------------------------------------------------------------*-
  2. ===========================
  3. Y Sever Includes - MD5 Core
  4. ===========================
  5. Description:
  6. Provides an implementation of the MD5 hashing algorithmfor PAWN, either for
  7. efficient and progressive hashing of chunks of data or just a straight hash
  8. of a single string.
  9. Legal:
  10. The algorithm is due to Ron Rivest. This code was
  11. written by Colin Plumb in 1993, no copyright is claimed.
  12. This code is in the public domain; do with it what you wish.
  13. Equivalent code is available from RSA Data Security, Inc.
  14. This code has been tested against that, and is equivalent,
  15. except that you don't need to include two pages of legalese
  16. with every copy.
  17. Converted to PAWN by Alex "Y_Less" Cole.
  18. Version:
  19. 0.1
  20. Changelog:
  21. 26/12/07:
  22. First version.
  23. Functions:
  24. Public:
  25. -
  26. Core:
  27. -
  28. Stock:
  29. MD5_Hash - Takes a string and returns a 16 cell hash.
  30. MD5_Init - Initialises a hash structure.
  31. MD5_Update - Appends data to the hash in progress.
  32. MD5_Copy - Custom pack function.
  33. MD5_Final - Finalises a hash.
  34. MD5_File - Hashes a file incrementally.
  35. MD5_Data - Hashes binary data, not just strings.
  36. Static:
  37. MD5_Transform - Does the data mixing.
  38. Inline:
  39. MD5_Step - Does a single hash step.
  40. API:
  41. -
  42. Callbacks:
  43. -
  44. Definitions:
  45. -
  46. Enums:
  47. E_MD5_CONTEXT - Data structure for an in progress hashing.
  48. Macros:
  49. MD5_F1 - First hash part.
  50. MD5_F2 - Second hash part.
  51. MD5_F3 - Third hash part.
  52. MD5_F4 - Fourth hash part.
  53. Tags:
  54. -
  55. Variables:
  56. Global:
  57. -
  58. Static:
  59. -
  60. Commands:
  61. -
  62. Compile options:
  63. -
  64. -*----------------------------------------------------------------------------*/
  65. enum E_MD5_CONTEXT
  66. {
  67. E_MD5_CONTEXT_BUF[4],
  68. E_MD5_CONTEXT_BITS[2],
  69. E_MD5_CONTEXT_IN[64 char]
  70. }
  71. #define MD5_F1(%1,%2,%3) (%3 ^ (%1 & (%2 ^ %3)))
  72. #define MD5_F2(%1,%2,%3) MD5_F1(%3, %1, %2)
  73. #define MD5_F3(%1,%2,%3) (%1 ^ %2 ^ %3)
  74. #define MD5_F4(%1,%2,%3) (%2 ^ (%1 | ~%3))
  75. /*----------------------------------------------------------------------------*-
  76. Function:
  77. MD5_Hash
  78. Params:
  79. str[] - String to hash.
  80. Return:
  81. String representation of the hash.
  82. Notes:
  83. The simplest way to hash a string, simply pass a string and get a 4 cell
  84. hash returned.
  85. -*----------------------------------------------------------------------------*/
  86. stock MD5_Hash(str[])
  87. {
  88. new
  89. md5Data[E_MD5_CONTEXT],
  90. done,
  91. digest[33],
  92. len = strlen(str);
  93. MD5_Init(md5Data);
  94. len -= 64;
  95. while (done < len)
  96. {
  97. MD5_Update(md5Data, str[done], 64);
  98. done += 64;
  99. }
  100. len = (len + 64) - done;
  101. if (len)
  102. {
  103. MD5_Update(md5Data, str[done], len);
  104. }
  105. digest = MD5_Final(md5Data, true);
  106. return digest;
  107. }
  108. /*----------------------------------------------------------------------------*-
  109. Function:
  110. MD5_Data
  111. Params:
  112. data[] - Binary data to hash.
  113. len - Length of data to hash.
  114. Return:
  115. String representation of the hash.
  116. Notes:
  117. Hashes binary data, not just strings.
  118. -*----------------------------------------------------------------------------*/
  119. stock MD5_Data(data[], len)
  120. {
  121. new
  122. md5Data[E_MD5_CONTEXT],
  123. done,
  124. digest[33];
  125. MD5_Init(md5Data);
  126. len -= 64;
  127. while (done < len)
  128. {
  129. MD5_Update(md5Data, data[done], 64);
  130. done += 64;
  131. }
  132. len = (len + 64) - done;
  133. if (len)
  134. {
  135. MD5_Update(md5Data, data[done], len);
  136. }
  137. digest = MD5_Final(md5Data, true);
  138. return digest;
  139. }
  140. /*----------------------------------------------------------------------------*-
  141. Function:
  142. MD5_File
  143. Params:
  144. filename[] - File to hash.
  145. Return:
  146. -
  147. Notes:
  148. Hashes the file incrementally, not in one huge chunk.
  149. -*----------------------------------------------------------------------------*/
  150. stock MD5_File(filename[])
  151. {
  152. new
  153. digest[33],
  154. File:fHnd = fopen(filename, io_read);
  155. if (fHnd)
  156. {
  157. new
  158. md5Data[E_MD5_CONTEXT],
  159. data[64],
  160. len;
  161. MD5_Init(md5Data);
  162. MD5_File_loop:
  163. len = fblockread(fHnd, data);
  164. if (len)
  165. {
  166. MD5_Update(md5Data, data, len);
  167. goto MD5_File_loop;
  168. }
  169. digest = MD5_Final(md5Data, true);
  170. fclose(fHnd);
  171. }
  172. return digest;
  173. }
  174. /*----------------------------------------------------------------------------*-
  175. Function:
  176. MD5_Init
  177. Params:
  178. ctx[E_MD5_CONTEXT] - Hash data.
  179. Return:
  180. -
  181. Notes:
  182. Sets up the data for hashing.
  183. -*----------------------------------------------------------------------------*/
  184. stock MD5_Init(ctx[E_MD5_CONTEXT])
  185. {
  186. ctx[E_MD5_CONTEXT_BUF][0] = 0x67452301;
  187. ctx[E_MD5_CONTEXT_BUF][1] = 0xEFCDAB89;
  188. ctx[E_MD5_CONTEXT_BUF][2] = 0x98BADCFE;
  189. ctx[E_MD5_CONTEXT_BUF][3] = 0x10325476;
  190. ctx[E_MD5_CONTEXT_BITS][0] = 0;
  191. ctx[E_MD5_CONTEXT_BITS][1] = 0;
  192. }
  193. /*----------------------------------------------------------------------------*-
  194. Function:
  195. MD5_Update
  196. Params:
  197. ctx[E_MD5_CONTEXT] - Hash data.
  198. data[] - String to append.
  199. len - Length of string to append.
  200. Return:
  201. -
  202. Notes:
  203. Adds data to the current hash.
  204. -*----------------------------------------------------------------------------*/
  205. stock MD5_Update(ctx[E_MD5_CONTEXT], data[], len)
  206. {
  207. new
  208. t = ctx[E_MD5_CONTEXT_BITS][0],
  209. s,
  210. buf = 0;
  211. if ((ctx[E_MD5_CONTEXT_BITS][0] = t + (len << 3)) < t)
  212. {
  213. ctx[E_MD5_CONTEXT_BITS][1]++;
  214. }
  215. ctx[E_MD5_CONTEXT_BITS][1] += len >>> 29;
  216. t = (t >>> 3) & 0x3F;
  217. if (t)
  218. {
  219. s = 64 - t;
  220. if (len < s)
  221. {
  222. MD5_Copy(ctx[E_MD5_CONTEXT_IN], data, t, len);
  223. return;
  224. }
  225. MD5_Copy(ctx[E_MD5_CONTEXT_IN], data, t, s);
  226. MD5_Transform(ctx[E_MD5_CONTEXT_BUF], ctx[E_MD5_CONTEXT_IN]);
  227. buf += s;
  228. len -= s;
  229. }
  230. while (len >= 64)
  231. {
  232. MD5_Copy(ctx[E_MD5_CONTEXT_IN], data[buf], 0, 64);
  233. MD5_Transform(ctx[E_MD5_CONTEXT_BUF], ctx[E_MD5_CONTEXT_IN]);
  234. buf += 64;
  235. len -= 64;
  236. }
  237. MD5_Copy(ctx[E_MD5_CONTEXT_IN], data[buf], 0, len);
  238. }
  239. /*----------------------------------------------------------------------------*-
  240. Function:
  241. MD5_Copy
  242. Params:
  243. dest[] - Packed destination array.
  244. src[] - Unpacked source array.
  245. start - Start BYTE in the dest array.
  246. len - Length of data to copy.
  247. Return:
  248. -
  249. Notes:
  250. Custom strpack implementation allowing offset starts.
  251. -*----------------------------------------------------------------------------*/
  252. stock MD5_Copy(dest[], src[], start, len)
  253. {
  254. new
  255. i = start >>> 2,
  256. j = 0,
  257. ch;
  258. while (j < len)
  259. {
  260. ch = src[j++] & 0xFF;
  261. switch (start++ & 0x03)
  262. {
  263. case 0:
  264. {
  265. dest[i] = ch;
  266. }
  267. case 1:
  268. {
  269. dest[i] |= ch << 8;
  270. }
  271. case 2:
  272. {
  273. dest[i] |= ch << 16;
  274. }
  275. case 3:
  276. {
  277. dest[i++] |= ch << 24;
  278. }
  279. }
  280. }
  281. }
  282. /*----------------------------------------------------------------------------*-
  283. Function:
  284. MD5_Final
  285. Params:
  286. ctx[E_MD5_CONTEXT] - Hash data.
  287. string - Wehter or not to create a string version of the hash.
  288. Return:
  289. -
  290. Notes:
  291. Terminates the pending data, appends the length and finishes hashing.
  292. -*----------------------------------------------------------------------------*/
  293. stock MD5_Final(ctx[E_MD5_CONTEXT], string = false)
  294. {
  295. new
  296. count,
  297. index,
  298. digest[33];
  299. count = (ctx[E_MD5_CONTEXT_BITS][0] >>> 3) & 0x3F;
  300. if (!(count & 0x03))
  301. {
  302. ctx[E_MD5_CONTEXT_IN][count / 4] = 0;
  303. }
  304. ctx[E_MD5_CONTEXT_IN][count / 4] |= (0x80 << (8 * (count & 0x03)));
  305. index = (count / 4) + 1;
  306. count = 64 - 1 - count;
  307. if (count < 8)
  308. {
  309. while (index < 64 char)
  310. {
  311. ctx[E_MD5_CONTEXT_IN][index++] = 0;
  312. }
  313. MD5_Transform(ctx[E_MD5_CONTEXT_BUF], ctx[E_MD5_CONTEXT_IN]);
  314. index = 0;
  315. while (index < 56 char)
  316. {
  317. ctx[E_MD5_CONTEXT_IN][index++] = 0;
  318. }
  319. }
  320. else
  321. {
  322. while (index < 56 char)
  323. {
  324. ctx[E_MD5_CONTEXT_IN][index++] = 0;
  325. }
  326. }
  327. ctx[E_MD5_CONTEXT_IN][14] = ctx[E_MD5_CONTEXT_BITS][0];
  328. ctx[E_MD5_CONTEXT_IN][15] = ctx[E_MD5_CONTEXT_BITS][1];
  329. MD5_Transform(ctx[E_MD5_CONTEXT_BUF], ctx[E_MD5_CONTEXT_IN]);
  330. if (string)
  331. {
  332. index = 0;
  333. do
  334. {
  335. format(digest, sizeof (digest), "%s%02x", digest, (ctx[E_MD5_CONTEXT_BUF][index / 4] >> ((index & 0x03) * 8)) & 0xFF);
  336. }
  337. while (++index < 16);
  338. }
  339. return digest;
  340. }
  341. /*----------------------------------------------------------------------------*-
  342. Function:
  343. MD5_Step
  344. Params:
  345. func - Hash function to use.
  346. a - Data cell 1.
  347. b - Data cell 2.
  348. c - Data cell 3.
  349. d - Data cell 4.
  350. data - New input.
  351. s - Seed.
  352. Return:
  353. -
  354. Notes:
  355. Does a single hash step.
  356. -*----------------------------------------------------------------------------*/
  357. #define MD5_Step(%1,%2,%3,%4,%5,%6,%7) \
  358. %2 += %1(%3, %4, %5) + %6, %2 = %2 << %7 | %2 >>> (32 - %7), %2 += %3
  359. /*----------------------------------------------------------------------------*-
  360. Function:
  361. MD5_Transform
  362. Params:
  363. buf[] - Current hash.
  364. in[] - New data.
  365. Return:
  366. -
  367. Notes:
  368. Does the hashing steps on the current data.
  369. -*----------------------------------------------------------------------------*/
  370. static stock MD5_Transform(buf[], in[])
  371. {
  372. new
  373. a = buf[0],
  374. b = buf[1],
  375. c = buf[2],
  376. d = buf[3];
  377. #pragma tabsize 4
  378. MD5_Step(MD5_F1, a, b, c, d, in[0] + 0xD76AA478, 7);
  379. MD5_Step(MD5_F1, d, a, b, c, in[1] + 0xE8C7B756, 12);
  380. MD5_Step(MD5_F1, c, d, a, b, in[2] + 0x242070DB, 17);
  381. MD5_Step(MD5_F1, b, c, d, a, in[3] + 0xC1BDCEEE, 22);
  382. MD5_Step(MD5_F1, a, b, c, d, in[4] + 0xF57C0FAF, 7);
  383. MD5_Step(MD5_F1, d, a, b, c, in[5] + 0x4787C62A, 12);
  384. MD5_Step(MD5_F1, c, d, a, b, in[6] + 0xA8304613, 17);
  385. MD5_Step(MD5_F1, b, c, d, a, in[7] + 0xFD469501, 22);
  386. MD5_Step(MD5_F1, a, b, c, d, in[8] + 0x698098D8, 7);
  387. MD5_Step(MD5_F1, d, a, b, c, in[9] + 0x8B44F7AF, 12);
  388. MD5_Step(MD5_F1, c, d, a, b, in[10] + 0xFFFF5BB1, 17);
  389. MD5_Step(MD5_F1, b, c, d, a, in[11] + 0x895CD7BE, 22);
  390. MD5_Step(MD5_F1, a, b, c, d, in[12] + 0x6B901122, 7);
  391. MD5_Step(MD5_F1, d, a, b, c, in[13] + 0xFD987193, 12);
  392. MD5_Step(MD5_F1, c, d, a, b, in[14] + 0xA679438E, 17);
  393. MD5_Step(MD5_F1, b, c, d, a, in[15] + 0x49B40821, 22);
  394. MD5_Step(MD5_F2, a, b, c, d, in[1] + 0xF61E2562, 5);
  395. MD5_Step(MD5_F2, d, a, b, c, in[6] + 0xC040B340, 9);
  396. MD5_Step(MD5_F2, c, d, a, b, in[11] + 0x265E5A51, 14);
  397. MD5_Step(MD5_F2, b, c, d, a, in[0] + 0xE9B6C7AA, 20);
  398. MD5_Step(MD5_F2, a, b, c, d, in[5] + 0xD62F105D, 5);
  399. MD5_Step(MD5_F2, d, a, b, c, in[10] + 0x02441453, 9);
  400. MD5_Step(MD5_F2, c, d, a, b, in[15] + 0xD8A1E681, 14);
  401. MD5_Step(MD5_F2, b, c, d, a, in[4] + 0xE7D3FBC8, 20);
  402. MD5_Step(MD5_F2, a, b, c, d, in[9] + 0x21E1CDE6, 5);
  403. MD5_Step(MD5_F2, d, a, b, c, in[14] + 0xC33707D6, 9);
  404. MD5_Step(MD5_F2, c, d, a, b, in[3] + 0xF4D50D87, 14);
  405. MD5_Step(MD5_F2, b, c, d, a, in[8] + 0x455A14ED, 20);
  406. MD5_Step(MD5_F2, a, b, c, d, in[13] + 0xA9E3E905, 5);
  407. MD5_Step(MD5_F2, d, a, b, c, in[2] + 0xFCEFA3F8, 9);
  408. MD5_Step(MD5_F2, c, d, a, b, in[7] + 0x676F02D9, 14);
  409. MD5_Step(MD5_F2, b, c, d, a, in[12] + 0x8D2A4C8A, 20);
  410. MD5_Step(MD5_F3, a, b, c, d, in[5] + 0xFFFA3942, 4);
  411. MD5_Step(MD5_F3, d, a, b, c, in[8] + 0x8771F681, 11);
  412. MD5_Step(MD5_F3, c, d, a, b, in[11] + 0x6D9D6122, 16);
  413. MD5_Step(MD5_F3, b, c, d, a, in[14] + 0xFDE5380C, 23);
  414. MD5_Step(MD5_F3, a, b, c, d, in[1] + 0xA4BEEA44, 4);
  415. MD5_Step(MD5_F3, d, a, b, c, in[4] + 0x4BDECFA9, 11);
  416. MD5_Step(MD5_F3, c, d, a, b, in[7] + 0xF6BB4B60, 16);
  417. MD5_Step(MD5_F3, b, c, d, a, in[10] + 0xBEBFBC70, 23);
  418. MD5_Step(MD5_F3, a, b, c, d, in[13] + 0x289B7EC6, 4);
  419. MD5_Step(MD5_F3, d, a, b, c, in[0] + 0xEAA127FA, 11);
  420. MD5_Step(MD5_F3, c, d, a, b, in[3] + 0xD4EF3085, 16);
  421. MD5_Step(MD5_F3, b, c, d, a, in[6] + 0x04881D05, 23);
  422. MD5_Step(MD5_F3, a, b, c, d, in[9] + 0xD9D4D039, 4);
  423. MD5_Step(MD5_F3, d, a, b, c, in[12] + 0xE6DB99E5, 11);
  424. MD5_Step(MD5_F3, c, d, a, b, in[15] + 0x1FA27CF8, 16);
  425. MD5_Step(MD5_F3, b, c, d, a, in[2] + 0xC4AC5665, 23);
  426. MD5_Step(MD5_F4, a, b, c, d, in[0] + 0xF4292244, 6);
  427. MD5_Step(MD5_F4, d, a, b, c, in[7] + 0x432AFF97, 10);
  428. MD5_Step(MD5_F4, c, d, a, b, in[14] + 0xAB9423A7, 15);
  429. MD5_Step(MD5_F4, b, c, d, a, in[5] + 0xFC93A039, 21);
  430. MD5_Step(MD5_F4, a, b, c, d, in[12] + 0x655B59C3, 6);
  431. MD5_Step(MD5_F4, d, a, b, c, in[3] + 0x8F0CCC92, 10);
  432. MD5_Step(MD5_F4, c, d, a, b, in[10] + 0xFFEFF47D, 15);
  433. MD5_Step(MD5_F4, b, c, d, a, in[1] + 0x85845DD1, 21);
  434. MD5_Step(MD5_F4, a, b, c, d, in[8] + 0x6FA87E4F, 6);
  435. MD5_Step(MD5_F4, d, a, b, c, in[15] + 0xFE2CE6E0, 10);
  436. MD5_Step(MD5_F4, c, d, a, b, in[6] + 0xA3014314, 15);
  437. MD5_Step(MD5_F4, b, c, d, a, in[13] + 0x4E0811A1, 21);
  438. MD5_Step(MD5_F4, a, b, c, d, in[4] + 0xF7537E82, 6);
  439. MD5_Step(MD5_F4, d, a, b, c, in[11] + 0xBD3AF235, 10);
  440. MD5_Step(MD5_F4, c, d, a, b, in[2] + 0x2AD7D2BB, 15);
  441. MD5_Step(MD5_F4, b, c, d, a, in[9] + 0xEB86D391, 21);
  442. #pragma tabsize 4
  443. buf[0] += a;
  444. buf[1] += b;
  445. buf[2] += c;
  446. buf[3] += d;
  447. }