Advertisement
Maddin

PAWN SAMP MD5 by Y_Less

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