Advertisement
unbanked

MD5_chayoung

Apr 15th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 10.25 KB | None | 0 0
  1. ' Original Source http://chayoung.tistory.com/entry/VBScript-MD5
  2. ' found via http://stackoverflow.com/questions/10198690/how-to-generate-md5-using-vb-in-classic-asp/10198875#10198875
  3. ' modified from original to input a null at end of program
  4.  
  5. Private Const BITS_TO_A_BYTE = 8
  6. Private Const BYTES_TO_A_WORD = 4
  7. Private Const BITS_TO_A_WORD = 32
  8. Private m_lOnBits(30)
  9. Private m_l2Power(30)
  10. Private strMessage
  11.  
  12.  
  13. strMessage =Inputbox("Enter phrase to hash","Input Required")
  14. 'WScript.Echo strMessage
  15.  
  16.  
  17. m_lOnBits(0) = CLng(1)
  18. m_lOnBits(1) = CLng(3)
  19. m_lOnBits(2) = CLng(7)
  20. m_lOnBits(3) = CLng(15)
  21. m_lOnBits(4) = CLng(31)
  22. m_lOnBits(5) = CLng(63)
  23. m_lOnBits(6) = CLng(127)
  24. m_lOnBits(7) = CLng(255)
  25. m_lOnBits(8) = CLng(511)
  26. m_lOnBits(9) = CLng(1023)
  27. m_lOnBits(10) = CLng(2047)
  28. m_lOnBits(11) = CLng(4095)
  29. m_lOnBits(12) = CLng(8191)
  30. m_lOnBits(13) = CLng(16383)
  31. m_lOnBits(14) = CLng(32767)
  32. m_lOnBits(15) = CLng(65535)
  33. m_lOnBits(16) = CLng(131071)
  34. m_lOnBits(17) = CLng(262143)
  35. m_lOnBits(18) = CLng(524287)
  36. m_lOnBits(19) = CLng(1048575)
  37. m_lOnBits(20) = CLng(2097151)
  38. m_lOnBits(21) = CLng(4194303)
  39. m_lOnBits(22) = CLng(8388607)
  40. m_lOnBits(23) = CLng(16777215)
  41. m_lOnBits(24) = CLng(33554431)
  42. m_lOnBits(25) = CLng(67108863)
  43. m_lOnBits(26) = CLng(134217727)
  44. m_lOnBits(27) = CLng(268435455)
  45. m_lOnBits(28) = CLng(536870911)
  46. m_lOnBits(29) = CLng(1073741823)
  47. m_lOnBits(30) = CLng(2147483647)
  48. m_l2Power(0) = CLng(1)
  49. m_l2Power(1) = CLng(2)
  50. m_l2Power(2) = CLng(4)
  51. m_l2Power(3) = CLng(8)
  52. m_l2Power(4) = CLng(16)
  53. m_l2Power(5) = CLng(32)
  54. m_l2Power(6) = CLng(64)
  55. m_l2Power(7) = CLng(128)
  56. m_l2Power(8) = CLng(256)
  57. m_l2Power(9) = CLng(512)
  58. m_l2Power(10) = CLng(1024)
  59. m_l2Power(11) = CLng(2048)
  60. m_l2Power(12) = CLng(4096)
  61. m_l2Power(13) = CLng(8192)
  62. m_l2Power(14) = CLng(16384)
  63. m_l2Power(15) = CLng(32768)
  64. m_l2Power(16) = CLng(65536)
  65. m_l2Power(17) = CLng(131072)
  66. m_l2Power(18) = CLng(262144)
  67. m_l2Power(19) = CLng(524288)
  68. m_l2Power(20) = CLng(1048576)
  69. m_l2Power(21) = CLng(2097152)
  70. m_l2Power(22) = CLng(4194304)
  71. m_l2Power(23) = CLng(8388608)
  72. m_l2Power(24) = CLng(16777216)
  73. m_l2Power(25) = CLng(33554432)
  74. m_l2Power(26) = CLng(67108864)
  75. m_l2Power(27) = CLng(134217728)
  76. m_l2Power(28) = CLng(268435456)
  77. m_l2Power(29) = CLng(536870912)
  78. m_l2Power(30) = CLng(1073741824)
  79.  
  80. Private Function LShift(lValue, iShiftBits)
  81.    
  82.     If iShiftBits = 0 Then
  83.         LShift = lValue
  84.        
  85.         Exit Function
  86.        
  87.     ElseIf iShiftBits = 31 Then
  88.        
  89.         If lValue And 1 Then
  90.             LShift = &H80000000
  91.         Else
  92.             LShift = 0
  93.         End If
  94.        
  95.         Exit Function
  96.        
  97.     ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  98.         Err.Raise 6
  99.     End If
  100.    
  101.     If (lValue And m_l2Power(31 - iShiftBits)) Then
  102.         LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
  103.     Else
  104.         LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
  105.     End If
  106.    
  107. End Function
  108.  
  109. Private Function RShift(lValue, iShiftBits)
  110.    
  111.     If iShiftBits = 0 Then
  112.         RShift = lValue
  113.        
  114.         Exit Function
  115.        
  116.     ElseIf iShiftBits = 31 Then
  117.         If lValue And &H80000000 Then
  118.             RShift = 1
  119.         Else
  120.             RShift = 0
  121.         End If
  122.        
  123.         Exit Function
  124.        
  125.     ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  126.         Err.Raise 6
  127.     End If
  128.    
  129.     RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
  130.    
  131.     If (lValue And &H80000000) Then
  132.         RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
  133.     End If
  134.    
  135. End Function
  136.  
  137. Private Function RotateLeft(lValue, iShiftBits)
  138.     RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
  139. End Function
  140.  
  141. Private Function AddUnsigned(lX, lY)
  142. Dim lX4
  143. Dim lY4
  144. Dim lX8
  145. Dim lY8
  146. Dim lResult
  147.  
  148.     lX8 = lX And &H80000000
  149.     lY8 = lY And &H80000000
  150.     lX4 = lX And &H40000000
  151.     lY4 = lY And &H40000000
  152.     lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
  153.  
  154.     If lX4 And lY4 Then
  155.         lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
  156.     ElseIf lX4 Or lY4 Then
  157.         If lResult And &H40000000 Then
  158.             lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
  159.         Else
  160.             lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
  161.         End If
  162.     Else
  163.         lResult = lResult Xor lX8 Xor lY8
  164.     End If
  165.    
  166.     AddUnsigned = lResult
  167.    
  168. End Function
  169.  
  170. Private Function F(x, y, z)
  171.     F = (x And y) Or ((Not x) And z)
  172. End Function
  173.  
  174. Private Function G(x, y, z)
  175.     G = (x And z) Or (y And (Not z))
  176. End Function
  177.  
  178. Private Function H(x, y, z)
  179.     H = (x Xor y Xor z)
  180. End Function
  181.  
  182. Private Function I(x, y, z)
  183.     I = (y Xor (x Or (Not z)))
  184. End Function
  185.  
  186. Private Sub FF(a, b, c, d, x, s, ac)
  187.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac))
  188.     a = RotateLeft(a, s)
  189.     a = AddUnsigned(a, b)
  190. End Sub
  191.  
  192. Private Sub GG(a, b, c, d, x, s, ac)
  193.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac))
  194.     a = RotateLeft(a, s)
  195.     a = AddUnsigned(a, b)
  196. End Sub
  197.  
  198. Private Sub HH(a, b, c, d, x, s, ac)
  199.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac))
  200.     a = RotateLeft(a, s)
  201.     a = AddUnsigned(a, b)
  202. End Sub
  203.  
  204. Private Sub II(a, b, c, d, x, s, ac)
  205.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac))
  206.     a = RotateLeft(a, s)
  207.     a = AddUnsigned(a, b)
  208. End Sub
  209.  
  210. Private Function ConvertToWordArray(sMessage)
  211. Dim lMessageLength
  212. Dim lNumberOfWords
  213. Dim lWordArray()
  214. Dim lBytePosition
  215. Dim lByteCount
  216. Dim lWordCount
  217. Const MODULUS_BITS = 512
  218. Const CONGRUENT_BITS = 448
  219.  
  220.     lMessageLength = Len(sMessage)
  221.     lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
  222.     ReDim lWordArray(lNumberOfWords - 1)
  223.     lBytePosition = 0
  224.     lByteCount = 0
  225.  
  226.     Do Until lByteCount >= lMessageLength
  227.         lWordCount = lByteCount \ BYTES_TO_A_WORD
  228.         lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  229.         lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
  230.         lByteCount = lByteCount + 1
  231.     Loop
  232.    
  233.     lWordCount = lByteCount \ BYTES_TO_A_WORD
  234.     lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  235.     lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
  236.     lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
  237.     lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
  238.    
  239.     ConvertToWordArray = lWordArray
  240.    
  241. End Function
  242.  
  243. Private Function WordToHex(lValue)
  244. Dim lByte
  245. Dim lCount
  246.  
  247.     For lCount = 0 To 3
  248.         lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
  249.         WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
  250.     Next
  251.    
  252. End Function
  253.  
  254. Public Function MD5(sMessage)
  255. Dim x
  256. Dim k
  257. Dim AA
  258. Dim BB
  259. Dim CC
  260. Dim DD
  261. Dim a
  262. Dim b
  263. Dim c
  264. Dim d
  265. Const S11 = 7
  266. Const S12 = 12
  267. Const S13 = 17
  268. Const S14 = 22
  269. Const S21 = 5
  270. Const S22 = 9
  271. Const S23 = 14
  272. Const S24 = 20
  273. Const S31 = 4
  274. Const S32 = 11
  275. Const S33 = 16
  276. Const S34 = 23
  277. Const S41 = 6
  278. Const S42 = 10
  279. Const S43 = 15
  280. Const S44 = 21
  281.  
  282.     x = ConvertToWordArray(sMessage)
  283.     a = &H67452301
  284.     b = &HEFCDAB89
  285.     c = &H98BADCFE
  286.     d = &H10325476
  287.    
  288.     For k = 0 To UBound(x) Step 16
  289.         AA = a
  290.         BB = b
  291.         CC = c
  292.         DD = d
  293.         FF a, b, c, d, x(k + 0), S11, &HD76AA478
  294.         FF d, a, b, c, x(k + 1), S12, &HE8C7B756
  295.         FF c, d, a, b, x(k + 2), S13, &H242070DB
  296.         FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
  297.         FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
  298.         FF d, a, b, c, x(k + 5), S12, &H4787C62A
  299.         FF c, d, a, b, x(k + 6), S13, &HA8304613
  300.         FF b, c, d, a, x(k + 7), S14, &HFD469501
  301.         FF a, b, c, d, x(k + 8), S11, &H698098D8
  302.         FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
  303.         FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
  304.         FF b, c, d, a, x(k + 11), S14, &H895CD7BE
  305.         FF a, b, c, d, x(k + 12), S11, &H6B901122
  306.         FF d, a, b, c, x(k + 13), S12, &HFD987193
  307.         FF c, d, a, b, x(k + 14), S13, &HA679438E
  308.         FF b, c, d, a, x(k + 15), S14, &H49B40821
  309.         GG a, b, c, d, x(k + 1), S21, &HF61E2562
  310.         GG d, a, b, c, x(k + 6), S22, &HC040B340
  311.         GG c, d, a, b, x(k + 11), S23, &H265E5A51
  312.         GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
  313.         GG a, b, c, d, x(k + 5), S21, &HD62F105D
  314.         GG d, a, b, c, x(k + 10), S22, &H2441453
  315.         GG c, d, a, b, x(k + 15), S23, &HD8A1E681
  316.         GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
  317.         GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
  318.         GG d, a, b, c, x(k + 14), S22, &HC33707D6
  319.         GG c, d, a, b, x(k + 3), S23, &HF4D50D87
  320.         GG b, c, d, a, x(k + 8), S24, &H455A14ED
  321.         GG a, b, c, d, x(k + 13), S21, &HA9E3E905
  322.         GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
  323.         GG c, d, a, b, x(k + 7), S23, &H676F02D9
  324.         GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
  325.         HH a, b, c, d, x(k + 5), S31, &HFFFA3942
  326.         HH d, a, b, c, x(k + 8), S32, &H8771F681
  327.         HH c, d, a, b, x(k + 11), S33, &H6D9D6122
  328.         HH b, c, d, a, x(k + 14), S34, &HFDE5380C
  329.         HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
  330.         HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
  331.         HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
  332.         HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
  333.         HH a, b, c, d, x(k + 13), S31, &H289B7EC6
  334.         HH d, a, b, c, x(k + 0), S32, &HEAA127FA
  335.         HH c, d, a, b, x(k + 3), S33, &HD4EF3085
  336.         HH b, c, d, a, x(k + 6), S34, &H4881D05
  337.         HH a, b, c, d, x(k + 9), S31, &HD9D4D039
  338.         HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
  339.         HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
  340.         HH b, c, d, a, x(k + 2), S34, &HC4AC5665
  341.         II a, b, c, d, x(k + 0), S41, &HF4292244
  342.         II d, a, b, c, x(k + 7), S42, &H432AFF97
  343.         II c, d, a, b, x(k + 14), S43, &HAB9423A7
  344.         II b, c, d, a, x(k + 5), S44, &HFC93A039
  345.         II a, b, c, d, x(k + 12), S41, &H655B59C3
  346.         II d, a, b, c, x(k + 3), S42, &H8F0CCC92
  347.         II c, d, a, b, x(k + 10), S43, &HFFEFF47D
  348.         II b, c, d, a, x(k + 1), S44, &H85845DD1
  349.         II a, b, c, d, x(k + 8), S41, &H6FA87E4F
  350.         II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
  351.         II c, d, a, b, x(k + 6), S43, &HA3014314
  352.         II b, c, d, a, x(k + 13), S44, &H4E0811A1
  353.         II a, b, c, d, x(k + 4), S41, &HF7537E82
  354.         II d, a, b, c, x(k + 11), S42, &HBD3AF235
  355.         II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
  356.         II b, c, d, a, x(k + 9), S44, &HEB86D391
  357.         a = AddUnsigned(a, AA)
  358.         b = AddUnsigned(b, BB)
  359.         c = AddUnsigned(c, CC)
  360.         d = AddUnsigned(d, DD)
  361.     Next
  362.  
  363.     MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
  364.    
  365. End Function
  366.  
  367. ' Test
  368. WScript.Echo strMessage,"hashed to MD5" & vbNewLine & vbNewLine & MD5(strMessage)'vbCrlf,strMessage::changed commas to & to prevent spaces
  369. strMessage =Inputbox("Hit 'Enter' to clear InputBox variable","")
  370. 'WScript.Echo strMessage
  371. WScript.Echo "Variables Cleared If These Hashes Match" & vbNewLine &vbNewLine & "d41d8cd98f00b204e9800998ecf8427e" & vbNewLine & MD5(strMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement