Ooops
March 05, 2021, 07:37:04 AM

Author Topic: [bb] MD5 Digest by Craig Watson [ 1+ years ago ]  (Read 446 times)

Offline BlitzBot

[bb] MD5 Digest by Craig Watson [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : MD5 Digest
Author : Craig Watson
Posted : 1+ years ago

Description : This is an MD5 algorithm, converted from some C code with some help/ideas from a Javascript source.  I've written it so you should be able to use it as an include file without too much risk of function/variable clashing.

Any improvements, bugs, etc. let me know.

Update 30/3/2002 - Found out NOT was ~, so removed the pNot function.


Code :
Code: BlitzBasic
  1. ;Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm
  2.  
  3. ;This array needs to be here for Blitz
  4. Dim MD5_x(0)
  5.  
  6. ;Test it out    
  7. blah$ = Input$("Enter a string to MD5: ")
  8. Print MD5$(blah$)
  9.  
  10. ;The main MD5 function
  11. Function MD5$(sMessage$)
  12.  
  13.     ;Pads the String as per the MD5 standard
  14.     nblk = ((Len(sMessage$) + 8) Shr 6) + 1  ;number of 16-word blocks
  15.  
  16.         Dim MD5_x(nblk * 16 - 1)
  17.     ;Zero pad the string
  18.     For i = 0 To nblk * 16 - 1
  19.       MD5_x(i) = 0
  20.     Next
  21.     ;Convert to array of "words"
  22.     For i = 0 To (Len(sMessage$) - 1)
  23.       MD5_x((i Shr 2)) = MD5_x((i Shr 2)) Or (Asc(Mid(sMessage$, (i + 1), 1)) Shl ((i Mod 4) * 8))
  24.     Next
  25.     MD5_x((i Shr 2)) = MD5_x((i Shr 2)) Or (128 Shl (((i) Mod 4) * 8))
  26.     MD5_x(nblk * 16 - 2) = Len(sMessage$) * 8
  27.    
  28.         ;Set initial values
  29.     MD5_a = 1732584193 ;&H67452301
  30.     MD5_b = -271733879 ;&HEFCDAB89
  31.     MD5_c = -1732584194 ;&H98BADCFE
  32.     MD5_d = 271733878 ;&H10325476
  33.  
  34.         ;Loop through the words
  35.     For k = 0 To (nblk * 16 - 1) Step 16
  36.         MD5_AA = MD5_a
  37.         MD5_BB = MD5_b
  38.         MD5_CC = MD5_c
  39.         MD5_DD = MD5_d
  40.     ;Round 1
  41.         MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 0), 7, -680876936) ;&HD76AA478
  42.         MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 1), 12, -389564586) ;&HE8C7B756
  43.         MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 2), 17, 606105819 );&H242070DB
  44.         MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 3), 22, -1044525330) ;&HC1BDCEEE
  45.         MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 4), 7, -176418897) ;&HF57C0FAF
  46.         MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 5), 12, 1200080426 );&H4787C62A
  47.         MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 6), 17, -1473231341) ;&HA8304613
  48.         MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 7), 22, -45705983) ;&HFD469501
  49.         MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 8), 7, 1770035416) ;&H698098D8
  50.         MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 9), 12, -1958414417 );&H8B44F7AF
  51.         MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 10), 17, -42063 );&HFFFF5BB1
  52.         MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 11), 22, -1990404162) ;&H895CD7BE
  53.         MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 12), 7, 1804603682) ;&H6B901122
  54.         MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 13), 12, -40341101) ;&HFD987193
  55.         MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 14), 17, -1502002290) ;&HA679438E
  56.         MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 15), 22, 1236535329) ;&H49B40821
  57.     ;Round 2
  58.         MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 1), 5, -165796510) ;&HF61E2562
  59.         MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 6), 9, -1069501632) ;&HC040B340
  60.         MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 11), 14, 643717713) ;&H265E5A51
  61.         MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 0), 20, -373897302) ;&HE9B6C7AA
  62.         MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 5), 5, -701558691) ;&HD62F105D
  63.         MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 10), 9, 38016083) ;&H2441453
  64.         MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 15), 14, -660478335) ;&HD8A1E681
  65.         MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 4), 20, -405537848) ;&HE7D3FBC8
  66.         MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 9), 5, 568446438) ;&H21E1CDE6
  67.         MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 14), 9, -1019803690) ;&HC33707D6
  68.         MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 3), 14, -187363961) ;&HF4D50D87
  69.         MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 8), 20, 1163531501) ;&H455A14ED
  70.         MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 13), 5, -1444681467) ;&HA9E3E905
  71.         MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 2), 9, -51403784) ;&HFCEFA3F8
  72.         MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 7), 14, 1735328473) ;&H676F02D9
  73.         MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 12), 20, -1926607734) ;&H8D2A4C8A
  74.     ;Round 3
  75.         MD5_a =  MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 5), 4, -378558) ;&HFFFA3942
  76.         MD5_d =  MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 8), 11, -2022574463) ;&H8771F681
  77.         MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 11), 16, 1839030562) ;&H6D9D6122
  78.         MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 14), 23, -35309556) ;&HFDE5380C
  79.         MD5_a =  MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 1), 4, -1530992060) ;&HA4BEEA44
  80.         MD5_d = MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 4), 11, 1272893353) ;&H4BDECFA9
  81.         MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 7), 16, -155497632) ;&HF6BB4B60
  82.         MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 10), 23, -1094730640) ;&HBEBFBC70
  83.         MD5_a = MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 13), 4, 681279174) ;&H289B7EC6
  84.         MD5_d = MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 0), 11, -358537222) ;&HEAA127FA
  85.         MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 3), 16, -722521979) ;&HD4EF3085
  86.         MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 6), 23, 76029189) ;&H4881D05
  87.         MD5_a = MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 9), 4, -640364487) ;&HD9D4D039
  88.         MD5_d = MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 12), 11, -421815835) ;&HE6DB99E5
  89.         MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 15), 16, 530742520) ;&H1FA27CF8
  90.         MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 2), 23, -995338651) ;&HC4AC5665
  91.     ;Round 4
  92.         MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 0), 6, -198630844) ;&HF4292244
  93.         MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 7), 10, 1126891415) ;&H432AFF97
  94.         MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 14), 15, -1416354905) ;&HAB9423A7
  95.         MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 5), 21, -57434055) ;&HFC93A039
  96.         MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 12), 6, 1700485571) ;&H655B59C3
  97.         MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 3), 10, -1894986606) ;&H8F0CCC92
  98.         MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 10), 15, -1051523) ;&HFFEFF47D
  99.         MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 1), 21, -2054922799) ;&H85845DD1
  100.         MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 8), 6, 1873313359) ;&H6FA87E4F
  101.         MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 15), 10, -30611744) ;&HFE2CE6E0
  102.         MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 6), 15, -1560198380 );&HA3014314
  103.         MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 13), 21, 1309151649) ;&H4E0811A1      
  104.         MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 4), 6, -145523070) ;&HF7537E82
  105.         MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 11), 10, -1120210379) ;&HBD3AF235
  106.         MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 2), 15, 718787259) ;&H2AD7D2BB
  107.         MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 9), 21, -343485551) ;&HEB86D391
  108.  
  109.         MD5_a = MD5_a + MD5_AA
  110.         MD5_b = MD5_b + MD5_BB
  111.         MD5_c = MD5_c + MD5_CC
  112.         MD5_d = MD5_d + MD5_DD
  113.     Next
  114.    
  115.     Return Lower(WordToHex$(MD5_a) + WordToHex$(MD5_b) + WordToHex$(MD5_c) + WordToHex$(MD5_d))
  116. End Function
  117.  
  118. Function MD5_F(x, y, z)
  119.         Return (x And y) Or (~(x) And z)
  120. End Function
  121.  
  122. Function MD5_G(x, y, z)
  123.     Return (x And z) Or (y And (~(z)))
  124. End Function
  125.  
  126. Function MD5_H(x, y, z)
  127.     Return (x Xor y Xor z)
  128. End Function
  129.  
  130. Function MD5_I(x, y, z)
  131.     Return (y Xor (x Or (~(z))))
  132. End Function
  133.  
  134. Function MD5_FF(a, b, c, d, x, s, ac)
  135.     a = (a + ((MD5_F(b, c, d)+ x)+ ac))
  136.     a = RotateLeft(a, s)
  137.     Return a + b
  138. End Function
  139.  
  140. Function MD5_GG(a, b, c, d, x, s, ac)
  141.     a = (a + ((MD5_G(b, c, d) + x) + ac))
  142.     a = RotateLeft(a, s)
  143.     Return a + b
  144. End Function
  145.  
  146. Function MD5_HH(a, b, c, d, x, s, ac)
  147.     a = (a + ((MD5_H(b, c, d) + x) + ac))
  148.     a = RotateLeft(a, s)
  149.     Return a + b
  150. End Function
  151.  
  152. Function MD5_II(a, b, c, d, x, s, ac)
  153.     a = (a + ((MD5_I(b, c, d) + x) + ac))
  154.     a = RotateLeft(a, s)
  155.     Return a + b
  156. End Function
  157.  
  158. Function RotateLeft(lValue, iShiftBits)
  159.     Return (lValue Shl iShiftBits) Or (lValue Shr (32 - iShiftBits))
  160. End Function
  161.  
  162. Function WordToHex$(lValue)
  163.     For lCount = 0 To 3
  164.         lByte = (lValue Shr lCount * 8) And 255
  165.         ToHex$ = ToHex$ + Right("0" + Hex$(lByte), 2)
  166.     Next
  167.         Return ToHex$
  168. End Function


Comments :


Techlord(Posted 1+ years ago)

 Excellent Craig Kiesau!


Rck(Posted 1+ years ago)

 Just used this for anti-cheating on map files in a multiplayer game that I've been working on for over a year.Thank you


puki(Posted 1+ years ago)

 Bah, I thought this was something to do with the MD5 model format.


jtfrench(Posted 1+ years ago)

 Does this work for producing hashes of files also?


plash(Posted 1+ years ago)

 It does not contain a function to perform a hash on a file, so no. You would have to load all the data in a file (in string format) then pass it to the md5 function.


Kippykip(Posted 1+ years ago)

 Useful!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal