Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for stringhash.vbs

(view source code of stringhash.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim intValid
  4. Dim objMD5, objSHA256
  5. Dim strAlgorithm, strHash, strString
  6.  
  7. intValid = 0
  8.  
  9. With WScript.Arguments.Named
  10. 	If .Count <> 2 Then Syntax
  11. 	If .Exists("A") Then
  12. 		strAlgorithm = UCase( .Item("A") )
  13. 		intValid = intValid + 1
  14. 	End If
  15. 	If .Exists("S") Then
  16. 		strString = .Item("S")
  17. 		intValid = intValid + 1
  18. 	End If
  19. End With
  20. If intValid <> WScript.Arguments.Count Then Syntax
  21.  
  22. Select Case strAlgorithm
  23. 	Case "MD5"
  24. 		Set objMD5 = New MD5
  25. 		strHash = objMD5.hash( strString )
  26. 		Set objMD5 = Nothing
  27. 	Case "SHA256", "SHA-256"
  28. 		Set objSHA256 = New SHA256
  29. 		strHash = objSHA256.SHA256( strString )
  30. 		Set objSHA256 = Nothing
  31. 	Case Else
  32. 		Syntax
  33. End Select
  34.  
  35. WScript.Echo strHash
  36.  
  37.  
  38.  
  39.  
  40. Sub Syntax
  41. 	Dim strMsg
  42. 	strMsg = vbCrLf _
  43. 	       & "StringHash.vbs,  Version 1.00" _
  44. 	       & vbCrLf _
  45. 	       & "Get the MD5 or SHA-256 hash value for the specified string" _
  46. 	       & vbCrLf & vbCrLf _
  47. 	       & "Usage:  CSCRIPT  //NoLogo  StringHash.vbs  /A:hashAlgorithm  /S:""string""" _
  48. 	       & vbCrLf & vbCrLf _
  49. 	       & "Where:  hashAlgorithm  is either MD5 or SHA256" _
  50. 	       & vbCrLf _
  51. 	       & "        string         must be enclosed in doublequotes if it contains spaces" _
  52. 	       & vbCrLf & vbCrLf _
  53. 	       & "Note:   This script uses the MD5 and SHA256 classes by Frez Systems Limited," _
  54. 	       & vbCrLf _
  55. 	       & "        http://www.frez.co.uk, which were adapted for use in VBScript" _
  56. 	       & vbCrLf _
  57. 	       & "        by Michal Gabrukiewicz (MD5) and Rob van der Woude (SHA-256)" _
  58. 	       & vbCrLf & vbCrLf _
  59. 	       & "Written by Rob van der Woude" _
  60. 	       & vbCrLf _
  61. 	       & "http://www.robvanderwoude.com"
  62. 	WScript.Echo strMsg
  63. 	WScript.Quit 1
  64. End Sub
  65.  
  66.  
  67.  
  68. '*********************************************************
  69. '** Code modified for use as class by Rob van der Woude **
  70. '** http://www.robvanderwoude.com                       **
  71. '*********************************************************
  72.  
  73. ' See the VB6 project that accompanies this sample for full code comments on how
  74. ' it works.
  75. '
  76. ' ASP VBScript code for generating a SHA256 'digest' or 'signature' of a string. The
  77. ' MD5 algorithm is one of the industry standard methods for generating digital
  78. ' signatures. It is generically known as a digest, digital signature, one-way
  79. ' encryption, hash or checksum algorithm. A common use for SHA256 is for password
  80. ' encryption as it is one-way in nature, that does not mean that your passwords
  81. ' are not free from a dictionary attack. 
  82. '
  83. ' If you are using the routine for passwords, you can make it a little more secure
  84. ' by concatenating some known random characters to the password before you generate
  85. ' the signature and on subsequent tests, so even if a hacker knows you are using
  86. ' SHA-256 for your passwords, the random characters will make it harder to dictionary
  87. ' attack.
  88. '
  89. ' NOTE: Due to the way in which the string is processed the routine assumes a
  90. ' single byte character set. VB passes unicode (2-byte) character strings, the
  91. ' ConvertToWordArray function uses on the first byte for each character. This
  92. ' has been done this way for ease of use, to make the routine truely portable
  93. ' you could accept a byte array instead, it would then be up to the calling
  94. ' routine to make sure that the byte array is generated from their string in
  95. ' a manner consistent with the string type.
  96. '
  97. ' This is 'free' software with the following restrictions:
  98. '
  99. ' You may not redistribute this code as a 'sample' or 'demo'. However, you are free
  100. ' to use the source code in your own code, but you may not claim that you created
  101. ' the sample code. It is expressly forbidden to sell or profit from this source code
  102. ' other than by the knowledge gained or the enhanced value added by your own code.
  103. '
  104. ' Use of this software is also done so at your own risk. The code is supplied as
  105. ' is without warranty or guarantee of any kind.
  106. '
  107. ' Should you wish to commission some derivative work based on this code provided
  108. ' here, or any consultancy work, please do not hesitate to contact us.
  109. '
  110. ' Web Site:  http://www.frez.co.uk
  111. ' E-mail:	sales@frez.co.uk
  112. Class SHA256
  113. 	Private m_lOnBits(30)
  114. 	Private m_l2Power(30)
  115. 	Private K(63)
  116.  
  117. 	Private BITS_TO_A_BYTE
  118. 	Private BYTES_TO_A_WORD
  119. 	Private BITS_TO_A_WORD
  120.  
  121. 	Private Sub Class_Initialize()
  122. 		BITS_TO_A_BYTE = 8
  123. 		BYTES_TO_A_WORD = 4
  124. 		BITS_TO_A_WORD = 32
  125. 		m_lOnBits(0) = CLng(1)
  126. 		m_lOnBits(1) = CLng(3)
  127. 		m_lOnBits(2) = CLng(7)
  128. 		m_lOnBits(3) = CLng(15)
  129. 		m_lOnBits(4) = CLng(31)
  130. 		m_lOnBits(5) = CLng(63)
  131. 		m_lOnBits(6) = CLng(127)
  132. 		m_lOnBits(7) = CLng(255)
  133. 		m_lOnBits(8) = CLng(511)
  134. 		m_lOnBits(9) = CLng(1023)
  135. 		m_lOnBits(10) = CLng(2047)
  136. 		m_lOnBits(11) = CLng(4095)
  137. 		m_lOnBits(12) = CLng(8191)
  138. 		m_lOnBits(13) = CLng(16383)
  139. 		m_lOnBits(14) = CLng(32767)
  140. 		m_lOnBits(15) = CLng(65535)
  141. 		m_lOnBits(16) = CLng(131071)
  142. 		m_lOnBits(17) = CLng(262143)
  143. 		m_lOnBits(18) = CLng(524287)
  144. 		m_lOnBits(19) = CLng(1048575)
  145. 		m_lOnBits(20) = CLng(2097151)
  146. 		m_lOnBits(21) = CLng(4194303)
  147. 		m_lOnBits(22) = CLng(8388607)
  148. 		m_lOnBits(23) = CLng(16777215)
  149. 		m_lOnBits(24) = CLng(33554431)
  150. 		m_lOnBits(25) = CLng(67108863)
  151. 		m_lOnBits(26) = CLng(134217727)
  152. 		m_lOnBits(27) = CLng(268435455)
  153. 		m_lOnBits(28) = CLng(536870911)
  154. 		m_lOnBits(29) = CLng(1073741823)
  155. 		m_lOnBits(30) = CLng(2147483647)
  156.  
  157. 		m_l2Power(0) = CLng(1)
  158. 		m_l2Power(1) = CLng(2)
  159. 		m_l2Power(2) = CLng(4)
  160. 		m_l2Power(3) = CLng(8)
  161. 		m_l2Power(4) = CLng(16)
  162. 		m_l2Power(5) = CLng(32)
  163. 		m_l2Power(6) = CLng(64)
  164. 		m_l2Power(7) = CLng(128)
  165. 		m_l2Power(8) = CLng(256)
  166. 		m_l2Power(9) = CLng(512)
  167. 		m_l2Power(10) = CLng(1024)
  168. 		m_l2Power(11) = CLng(2048)
  169. 		m_l2Power(12) = CLng(4096)
  170. 		m_l2Power(13) = CLng(8192)
  171. 		m_l2Power(14) = CLng(16384)
  172. 		m_l2Power(15) = CLng(32768)
  173. 		m_l2Power(16) = CLng(65536)
  174. 		m_l2Power(17) = CLng(131072)
  175. 		m_l2Power(18) = CLng(262144)
  176. 		m_l2Power(19) = CLng(524288)
  177. 		m_l2Power(20) = CLng(1048576)
  178. 		m_l2Power(21) = CLng(2097152)
  179. 		m_l2Power(22) = CLng(4194304)
  180. 		m_l2Power(23) = CLng(8388608)
  181. 		m_l2Power(24) = CLng(16777216)
  182. 		m_l2Power(25) = CLng(33554432)
  183. 		m_l2Power(26) = CLng(67108864)
  184. 		m_l2Power(27) = CLng(134217728)
  185. 		m_l2Power(28) = CLng(268435456)
  186. 		m_l2Power(29) = CLng(536870912)
  187. 		m_l2Power(30) = CLng(1073741824)
  188.  
  189. 		K(0) = &H428A2F98
  190. 		K(1) = &H71374491
  191. 		K(2) = &HB5C0FBCF
  192. 		K(3) = &HE9B5DBA5
  193. 		K(4) = &H3956C25B
  194. 		K(5) = &H59F111F1
  195. 		K(6) = &H923F82A4
  196. 		K(7) = &HAB1C5ED5
  197. 		K(8) = &HD807AA98
  198. 		K(9) = &H12835B01
  199. 		K(10) = &H243185BE
  200. 		K(11) = &H550C7DC3
  201. 		K(12) = &H72BE5D74
  202. 		K(13) = &H80DEB1FE
  203. 		K(14) = &H9BDC06A7
  204. 		K(15) = &HC19BF174
  205. 		K(16) = &HE49B69C1
  206. 		K(17) = &HEFBE4786
  207. 		K(18) = &HFC19DC6
  208. 		K(19) = &H240CA1CC
  209. 		K(20) = &H2DE92C6F
  210. 		K(21) = &H4A7484AA
  211. 		K(22) = &H5CB0A9DC
  212. 		K(23) = &H76F988DA
  213. 		K(24) = &H983E5152
  214. 		K(25) = &HA831C66D
  215. 		K(26) = &HB00327C8
  216. 		K(27) = &HBF597FC7
  217. 		K(28) = &HC6E00BF3
  218. 		K(29) = &HD5A79147
  219. 		K(30) = &H6CA6351
  220. 		K(31) = &H14292967
  221. 		K(32) = &H27B70A85
  222. 		K(33) = &H2E1B2138
  223. 		K(34) = &H4D2C6DFC
  224. 		K(35) = &H53380D13
  225. 		K(36) = &H650A7354
  226. 		K(37) = &H766A0ABB
  227. 		K(38) = &H81C2C92E
  228. 		K(39) = &H92722C85
  229. 		K(40) = &HA2BFE8A1
  230. 		K(41) = &HA81A664B
  231. 		K(42) = &HC24B8B70
  232. 		K(43) = &HC76C51A3
  233. 		K(44) = &HD192E819
  234. 		K(45) = &HD6990624
  235. 		K(46) = &HF40E3585
  236. 		K(47) = &H106AA070
  237. 		K(48) = &H19A4C116
  238. 		K(49) = &H1E376C08
  239. 		K(50) = &H2748774C
  240. 		K(51) = &H34B0BCB5
  241. 		K(52) = &H391C0CB3
  242. 		K(53) = &H4ED8AA4A
  243. 		K(54) = &H5B9CCA4F
  244. 		K(55) = &H682E6FF3
  245. 		K(56) = &H748F82EE
  246. 		K(57) = &H78A5636F
  247. 		K(58) = &H84C87814
  248. 		K(59) = &H8CC70208
  249. 		K(60) = &H90BEFFFA
  250. 		K(61) = &HA4506CEB
  251. 		K(62) = &HBEF9A3F7
  252. 		K(63) = &HC67178F2
  253. 	End Sub
  254.  
  255.  
  256. 	Private Function LShift(lValue, iShiftBits)
  257. 		If iShiftBits = 0 Then
  258. 			LShift = lValue
  259. 			Exit Function
  260. 		ElseIf iShiftBits = 31 Then
  261. 			If lValue And 1 Then
  262. 				LShift = &H80000000
  263. 			Else
  264. 				LShift = 0
  265. 			End If
  266. 			Exit Function
  267. 		ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  268. 			Err.Raise 6
  269. 		End If
  270.  
  271. 		If (lValue And m_l2Power(31 - iShiftBits)) Then
  272. 			LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
  273. 		Else
  274. 			LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
  275. 		End If
  276. 	End Function
  277.  
  278. 	Private Function RShift(lValue, iShiftBits)
  279. 		If iShiftBits = 0 Then
  280. 			RShift = lValue
  281. 			Exit Function
  282. 		ElseIf iShiftBits = 31 Then
  283. 			If lValue And &H80000000 Then
  284. 				RShift = 1
  285. 			Else
  286. 				RShift = 0
  287. 			End If
  288. 			Exit Function
  289. 		ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  290. 			Err.Raise 6
  291. 		End If
  292.  
  293. 		RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
  294.  
  295. 		If (lValue And &H80000000) Then
  296. 			RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
  297. 		End If
  298. 	End Function
  299.  
  300. 	Private Function AddUnsigned(lX, lY)
  301. 		Dim lX4
  302. 		Dim lY4
  303. 		Dim lX8
  304. 		Dim lY8
  305. 		Dim lResult
  306.  
  307. 		lX8 = lX And &H80000000
  308. 		lY8 = lY And &H80000000
  309. 		lX4 = lX And &H40000000
  310. 		lY4 = lY And &H40000000
  311.  
  312. 		lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
  313.  
  314. 		If lX4 And lY4 Then
  315. 			lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
  316. 		ElseIf lX4 Or lY4 Then
  317. 			If lResult And &H40000000 Then
  318. 				lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
  319. 			Else
  320. 				lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
  321. 			End If
  322. 		Else
  323. 			lResult = lResult Xor lX8 Xor lY8
  324. 		End If
  325.  
  326. 		AddUnsigned = lResult
  327. 	End Function
  328.  
  329. 	Private Function Ch(x, y, z)
  330. 		Ch = ((x And y) Xor ((Not x) And z))
  331. 	End Function
  332.  
  333. 	Private Function Maj(x, y, z)
  334. 		Maj = ((x And y) Xor (x And z) Xor (y And z))
  335. 	End Function
  336.  
  337. 	Private Function S(x, n)
  338. 		S = (RShift(x, (n And m_lOnBits(4))) Or LShift(x, (32 - (n And m_lOnBits(4)))))
  339. 	End Function
  340.  
  341. 	Private Function R(x, n)
  342. 		R = RShift(x, CInt(n And m_lOnBits(4)))
  343. 	End Function
  344.  
  345. 	Private Function Sigma0(x)
  346. 		Sigma0 = (S(x, 2) Xor S(x, 13) Xor S(x, 22))
  347. 	End Function
  348.  
  349. 	Private Function Sigma1(x)
  350. 		Sigma1 = (S(x, 6) Xor S(x, 11) Xor S(x, 25))
  351. 	End Function
  352.  
  353. 	Private Function Gamma0(x)
  354. 		Gamma0 = (S(x, 7) Xor S(x, 18) Xor R(x, 3))
  355. 	End Function
  356.  
  357. 	Private Function Gamma1(x)
  358. 		Gamma1 = (S(x, 17) Xor S(x, 19) Xor R(x, 10))
  359. 	End Function
  360.  
  361. 	Private Function ConvertToWordArray(sMessage)
  362. 		Dim lMessageLength
  363. 		Dim lNumberOfWords
  364. 		Dim lWordArray()
  365. 		Dim lBytePosition
  366. 		Dim lByteCount
  367. 		Dim lWordCount
  368. 		Dim lByte
  369.  
  370. 		Const MODULUS_BITS = 512
  371. 		Const CONGRUENT_BITS = 448
  372.  
  373. 		lMessageLength = Len(sMessage)
  374.  
  375. 		lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
  376. 		ReDim lWordArray(lNumberOfWords - 1)
  377.  
  378. 		lBytePosition = 0
  379. 		lByteCount = 0
  380. 		Do Until lByteCount >= lMessageLength
  381. 			lWordCount = lByteCount \ BYTES_TO_A_WORD
  382.  
  383. 			lBytePosition = (3 - (lByteCount Mod BYTES_TO_A_WORD)) * BITS_TO_A_BYTE
  384.  
  385. 			lByte = AscB(Mid(sMessage, lByteCount + 1, 1))
  386.  
  387. 			lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(lByte, lBytePosition)
  388. 			lByteCount = lByteCount + 1
  389. 		Loop
  390.  
  391. 		lWordCount = lByteCount \ BYTES_TO_A_WORD
  392. 		lBytePosition = (3 - (lByteCount Mod BYTES_TO_A_WORD)) * BITS_TO_A_BYTE
  393.  
  394. 		lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
  395.  
  396. 		lWordArray(lNumberOfWords - 1) = LShift(lMessageLength, 3)
  397. 		lWordArray(lNumberOfWords - 2) = RShift(lMessageLength, 29)
  398.  
  399. 		ConvertToWordArray = lWordArray
  400. 	End Function
  401.  
  402. 	Public Function SHA256(sMessage)
  403. 		Dim HASH(7)
  404. 		Dim M
  405. 		Dim W(63)
  406. 		Dim a
  407. 		Dim b
  408. 		Dim c
  409. 		Dim d
  410. 		Dim e
  411. 		Dim F
  412. 		Dim g
  413. 		Dim H
  414. 		Dim i
  415. 		Dim j
  416. 		Dim T1
  417. 		Dim T2
  418.  
  419. 		HASH(0) = &H6A09E667
  420. 		HASH(1) = &HBB67AE85
  421. 		HASH(2) = &H3C6EF372
  422. 		HASH(3) = &HA54FF53A
  423. 		HASH(4) = &H510E527F
  424. 		HASH(5) = &H9B05688C
  425. 		HASH(6) = &H1F83D9AB
  426. 		HASH(7) = &H5BE0CD19
  427.  
  428. 		M = ConvertToWordArray(sMessage)
  429.  
  430. 		For i = 0 To UBound(M) Step 16
  431. 			a = HASH(0)
  432. 			b = HASH(1)
  433. 			c = HASH(2)
  434. 			d = HASH(3)
  435. 			e = HASH(4)
  436. 			f = HASH(5)
  437. 			g = HASH(6)
  438. 			h = HASH(7)
  439.  
  440. 			For j = 0 To 63
  441. 				If j < 16 Then
  442. 					W(j) = M(j + i)
  443. 				Else
  444. 					W(j) = AddUnsigned(AddUnsigned(AddUnsigned(Gamma1(W(j - 2)), W(j - 7)), Gamma0(W(j - 15))), W(j - 16))
  445. 				End If
  446.  
  447. 				T1 = AddUnsigned(AddUnsigned(AddUnsigned(AddUnsigned(h, Sigma1(e)), Ch(e, f, g)), K(j)), W(j))
  448. 				T2 = AddUnsigned(Sigma0(a), Maj(a, b, c))
  449.  
  450. 				h = g
  451. 				g = f
  452. 				f = e
  453. 				e = AddUnsigned(d, T1)
  454. 				d = c
  455. 				c = b
  456. 				b = a
  457. 				a = AddUnsigned(T1, T2)
  458. 			Next
  459.  
  460. 			HASH(0) = AddUnsigned(a, HASH(0))
  461. 			HASH(1) = AddUnsigned(b, HASH(1))
  462. 			HASH(2) = AddUnsigned(c, HASH(2))
  463. 			HASH(3) = AddUnsigned(d, HASH(3))
  464. 			HASH(4) = AddUnsigned(e, HASH(4))
  465. 			HASH(5) = AddUnsigned(f, HASH(5))
  466. 			HASH(6) = AddUnsigned(g, HASH(6))
  467. 			HASH(7) = AddUnsigned(h, HASH(7))
  468. 		Next
  469.  
  470. 		SHA256 = LCase(Right("00000000" & Hex(HASH(0)), 8) & Right("00000000" & Hex(HASH(1)), 8) & Right("00000000" & Hex(HASH(2)), 8) & Right("00000000" & Hex(HASH(3)), 8) & Right("00000000" & Hex(HASH(4)), 8) & Right("00000000" & Hex(HASH(5)), 8) & Right("00000000" & Hex(HASH(6)), 8) & Right("00000000" & Hex(HASH(7)), 8))
  471. 	End Function
  472. End Class
  473.  
  474.  
  475.  
  476. '**************************************************************************************************************
  477. '* ajaxed Copyright (C) 2003		
  478. '* License refer to license.txt		
  479. '**************************************************************************************************************
  480.  
  481. '**************************************************************************************************************
  482.  
  483. '' @CLASSTITLE:		MD5
  484. '' @CREATOR:		Michal Gabrukiewicz - gabru @ grafix.at
  485. '' @CREATEDON:		29.12.2007
  486. '' @CDESCRIPTION:	Thats a class for MD5 encryption.
  487. ''					MD5: Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm,
  488. ''					as set out in the memo RFC1321. One way encryption!
  489. ''					Encryption-code taken from Web Site: http://www.frez.co.uk and modified to a class. Many thanks!
  490. '' @VERSION:		0.1
  491.  
  492. '**************************************************************************************************************
  493. Class MD5
  494. 	Private BITS_TO_A_BYTE
  495. 	Private BYTES_TO_A_WORD
  496. 	Private BITS_TO_A_WORD
  497. 	Private P_ALGORITHM
  498.  
  499. 	Private m_lOnBits(30)
  500. 	Private m_l2Power(30)
  501.  
  502. 	'Construktor => set the default values
  503. 	Private sub Class_Initialize()
  504. 		BITS_TO_A_BYTE  =  8
  505. 		BYTES_TO_A_WORD =  4
  506. 		BITS_TO_A_WORD  = 32
  507. 		m_lOnBits(0)  = CLng(1)
  508. 		m_lOnBits(1)  = CLng(3)
  509. 		m_lOnBits(2)  = CLng(7)
  510. 		m_lOnBits(3)  = CLng(15)
  511. 		m_lOnBits(4)  = CLng(31)
  512. 		m_lOnBits(5)  = CLng(63)
  513. 		m_lOnBits(6)  = CLng(127)
  514. 		m_lOnBits(7)  = CLng(255)
  515. 		m_lOnBits(8)  = CLng(511)
  516. 		m_lOnBits(9)  = CLng(1023)
  517. 		m_lOnBits(10) = CLng(2047)
  518. 		m_lOnBits(11) = CLng(4095)
  519. 		m_lOnBits(12) = CLng(8191)
  520. 		m_lOnBits(13) = CLng(16383)
  521. 		m_lOnBits(14) = CLng(32767)
  522. 		m_lOnBits(15) = CLng(65535)
  523. 		m_lOnBits(16) = CLng(131071)
  524. 		m_lOnBits(17) = CLng(262143)
  525. 		m_lOnBits(18) = CLng(524287)
  526. 		m_lOnBits(19) = CLng(1048575)
  527. 		m_lOnBits(20) = CLng(2097151)
  528. 		m_lOnBits(21) = CLng(4194303)
  529. 		m_lOnBits(22) = CLng(8388607)
  530. 		m_lOnBits(23) = CLng(16777215)
  531. 		m_lOnBits(24) = CLng(33554431)
  532. 		m_lOnBits(25) = CLng(67108863)
  533. 		m_lOnBits(26) = CLng(134217727)
  534. 		m_lOnBits(27) = CLng(268435455)
  535. 		m_lOnBits(28) = CLng(536870911)
  536. 		m_lOnBits(29) = CLng(1073741823)
  537. 		m_lOnBits(30) = CLng(2147483647)
  538.  
  539. 		m_l2Power(0)  = CLng(1)
  540. 		m_l2Power(1)  = CLng(2)
  541. 		m_l2Power(2)  = CLng(4)
  542. 		m_l2Power(3)  = CLng(8)
  543. 		m_l2Power(4)  = CLng(16)
  544. 		m_l2Power(5)  = CLng(32)
  545. 		m_l2Power(6)  = CLng(64)
  546. 		m_l2Power(7)  = CLng(128)
  547. 		m_l2Power(8)  = CLng(256)
  548. 		m_l2Power(9)  = CLng(512)
  549. 		m_l2Power(10) = CLng(1024)
  550. 		m_l2Power(11) = CLng(2048)
  551. 		m_l2Power(12) = CLng(4096)
  552. 		m_l2Power(13) = CLng(8192)
  553. 		m_l2Power(14) = CLng(16384)
  554. 		m_l2Power(15) = CLng(32768)
  555. 		m_l2Power(16) = CLng(65536)
  556. 		m_l2Power(17) = CLng(131072)
  557. 		m_l2Power(18) = CLng(262144)
  558. 		m_l2Power(19) = CLng(524288)
  559. 		m_l2Power(20) = CLng(1048576)
  560. 		m_l2Power(21) = CLng(2097152)
  561. 		m_l2Power(22) = CLng(4194304)
  562. 		m_l2Power(23) = CLng(8388608)
  563. 		m_l2Power(24) = CLng(16777216)
  564. 		m_l2Power(25) = CLng(33554432)
  565. 		m_l2Power(26) = CLng(67108864)
  566. 		m_l2Power(27) = CLng(134217728)
  567. 		m_l2Power(28) = CLng(268435456)
  568. 		m_l2Power(29) = CLng(536870912)
  569. 		m_l2Power(30) = CLng(1073741824)
  570. 	end sub
  571.  
  572. 	'**********************************************************************************************************************
  573. 	'' @SDESCRIPTION:	returns a MD5 hash for the given value
  574. 	'' @PARAM:			val [string]: the string for hashing
  575. 	'' @RETURN:			[string] MD5 hash
  576. 	'**********************************************************************************************************************
  577. 	Public Function hash(val)
  578. 		hash = MD5(val & "")
  579. 	End Function
  580.  
  581. 	'**********************************************************************************************************************
  582. 	'* LShift
  583. 	'**********************************************************************************************************************
  584. 	Private Function LShift(lValue, iShiftBits)
  585. 		If iShiftBits = 0 Then
  586. 			LShift = lValue
  587. 			Exit Function
  588. 		ElseIf iShiftBits = 31 Then
  589. 			If lValue And 1 Then
  590. 				LShift = &H80000000
  591. 			Else
  592. 				LShift = 0
  593. 			End If
  594. 			Exit Function
  595. 		ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  596. 			Err.Raise 6
  597. 		End If
  598.  
  599. 		If (lValue And m_l2Power(31 - iShiftBits)) Then
  600. 			LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
  601. 		Else
  602. 			LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
  603. 		End If
  604. 	End Function
  605.  
  606. 	'**********************************************************************************************************************
  607. 	'* RShift
  608. 	'**********************************************************************************************************************
  609. 	Private Function RShift(lValue, iShiftBits)
  610. 		If iShiftBits = 0 Then
  611. 			RShift = lValue
  612. 			Exit Function
  613. 		ElseIf iShiftBits = 31 Then
  614. 			If lValue And &H80000000 Then
  615. 				RShift = 1
  616. 			Else
  617. 				RShift = 0
  618. 			End If
  619. 			Exit Function
  620. 		ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  621. 			Err.Raise 6
  622. 		End If
  623.  
  624. 		RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
  625.  
  626. 		If (lValue And &H80000000) Then
  627. 			RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
  628. 		End If
  629. 	End Function
  630.  
  631. 	'**********************************************************************************************************************
  632. 	'* RotateLeft
  633. 	'**********************************************************************************************************************
  634. 	Private Function RotateLeft(lValue, iShiftBits)
  635. 		RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
  636. 	End Function
  637.  
  638. 	'**********************************************************************************************************************
  639. 	'* AddUnsigned
  640. 	'**********************************************************************************************************************
  641. 	Private Function AddUnsigned(lX, lY)
  642. 		Dim lX4
  643. 		Dim lY4
  644. 		Dim lX8
  645. 		Dim lY8
  646. 		Dim lResult
  647.  
  648. 		lX8 = lX And &H80000000
  649. 		lY8 = lY And &H80000000
  650. 		lX4 = lX And &H40000000
  651. 		lY4 = lY And &H40000000
  652.  
  653. 		lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
  654.  
  655. 		If lX4 And lY4 Then
  656. 			lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
  657. 		ElseIf lX4 Or lY4 Then
  658. 			If lResult And &H40000000 Then
  659. 				lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
  660. 			Else
  661. 				lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
  662. 			End If
  663. 		Else
  664. 			lResult = lResult Xor lX8 Xor lY8
  665. 		End If
  666.  
  667. 		AddUnsigned = lResult
  668. 	End Function
  669.  
  670. 	'**********************************************************************************************************************
  671. 	'* F
  672. 	'**********************************************************************************************************************
  673. 	Private Function F(x, y, z)
  674. 		F = (x And y) Or ((Not x) And z)
  675. 	End Function
  676.  
  677. 	'**********************************************************************************************************************
  678. 	'* G
  679. 	'**********************************************************************************************************************
  680. 	Private Function G(x, y, z)
  681. 		G = (x And z) Or (y And (Not z))
  682. 	End Function
  683.  
  684. 	'**********************************************************************************************************************
  685. 	'* H
  686. 	'**********************************************************************************************************************
  687. 	Private Function H(x, y, z)
  688. 		H = (x Xor y Xor z)
  689. 	End Function
  690.  
  691. 	'**********************************************************************************************************************
  692. 	'* I
  693. 	'**********************************************************************************************************************
  694. 	Private Function I(x, y, z)
  695. 		I = (y Xor (x Or (Not z)))
  696. 	End Function
  697.  
  698. 	'**********************************************************************************************************************
  699. 	'* FF
  700. 	'**********************************************************************************************************************
  701. 	Private Sub FF(a, b, c, d, x, s, ac)
  702. 		a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac))
  703. 		a = RotateLeft(a, s)
  704. 		a = AddUnsigned(a, b)
  705. 	End Sub
  706.  
  707. 	'**********************************************************************************************************************
  708. 	'* GG
  709. 	'**********************************************************************************************************************
  710. 	Private Sub GG(a, b, c, d, x, s, ac)
  711. 		a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac))
  712. 		a = RotateLeft(a, s)
  713. 		a = AddUnsigned(a, b)
  714. 	End Sub
  715.  
  716. 	'**********************************************************************************************************************
  717. 	'* HH
  718. 	'**********************************************************************************************************************
  719. 	Private Sub HH(a, b, c, d, x, s, ac)
  720. 		a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac))
  721. 		a = RotateLeft(a, s)
  722. 		a = AddUnsigned(a, b)
  723. 	End Sub
  724.  
  725. 	'**********************************************************************************************************************
  726. 	'* II
  727. 	'**********************************************************************************************************************
  728. 	Private Sub II(a, b, c, d, x, s, ac)
  729. 		a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac))
  730. 		a = RotateLeft(a, s)
  731. 		a = AddUnsigned(a, b)
  732. 	End Sub
  733.  
  734. 	'**********************************************************************************************************************
  735. 	'* ConvertToWordArray
  736. 	'**********************************************************************************************************************
  737. 	Private Function ConvertToWordArray(sMessage)
  738. 		Dim lMessageLength
  739. 		Dim lNumberOfWords
  740. 		Dim lWordArray()
  741. 		Dim lBytePosition
  742. 		Dim lByteCount
  743. 		Dim lWordCount
  744.  
  745. 		Const MODULUS_BITS = 512
  746. 		Const CONGRUENT_BITS = 448
  747.  
  748. 		lMessageLength = Len(sMessage)
  749.  
  750. 		lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
  751. 		ReDim lWordArray(lNumberOfWords - 1)
  752.  
  753. 		lBytePosition = 0
  754. 		lByteCount = 0
  755. 		Do Until lByteCount >= lMessageLength
  756. 			lWordCount = lByteCount \ BYTES_TO_A_WORD
  757. 			lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  758. 			lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
  759. 			lByteCount = lByteCount + 1
  760. 		Loop
  761.  
  762. 		lWordCount = lByteCount \ BYTES_TO_A_WORD
  763. 		lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  764.  
  765. 		lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
  766.  
  767. 		lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
  768. 		lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
  769.  
  770. 		ConvertToWordArray = lWordArray
  771. 	End Function
  772.  
  773. 	'**********************************************************************************************************************
  774. 	'* WordToHex
  775. 	'**********************************************************************************************************************
  776. 	Private Function WordToHex(lValue)
  777. 		Dim lByte
  778. 		Dim lCount
  779.  
  780. 		For lCount = 0 To 3
  781. 			lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
  782. 			WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
  783. 		Next
  784. 	End Function
  785.  
  786. 	'**********************************************************************************************************************
  787. 	'* md5 
  788. 	'**********************************************************************************************************************
  789. 	Private Function MD5(sMessage)
  790. 		Dim x
  791. 		Dim k
  792. 		Dim AA
  793. 		Dim BB
  794. 		Dim CC
  795. 		Dim DD
  796. 		Dim a
  797. 		Dim b
  798. 		Dim c
  799. 		Dim d
  800.  
  801. 		Const S11 = 7
  802. 		Const S12 = 12
  803. 		Const S13 = 17
  804. 		Const S14 = 22
  805. 		Const S21 = 5
  806. 		Const S22 = 9
  807. 		Const S23 = 14
  808. 		Const S24 = 20
  809. 		Const S31 = 4
  810. 		Const S32 = 11
  811. 		Const S33 = 16
  812. 		Const S34 = 23
  813. 		Const S41 = 6
  814. 		Const S42 = 10
  815. 		Const S43 = 15
  816. 		Const S44 = 21
  817.  
  818. 		x = ConvertToWordArray(sMessage)
  819.  
  820. 		a = &H67452301
  821. 		b = &HEFCDAB89
  822. 		c = &H98BADCFE
  823. 		d = &H10325476
  824.  
  825. 		For k = 0 To UBound(x) Step 16
  826. 			AA = a
  827. 			BB = b
  828. 			CC = c
  829. 			DD = d
  830.  
  831. 			FF a, b, c, d, x(k + 0), S11, &HD76AA478
  832. 			FF d, a, b, c, x(k + 1), S12, &HE8C7B756
  833. 			FF c, d, a, b, x(k + 2), S13, &H242070DB
  834. 			FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
  835. 			FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
  836. 			FF d, a, b, c, x(k + 5), S12, &H4787C62A
  837. 			FF c, d, a, b, x(k + 6), S13, &HA8304613
  838. 			FF b, c, d, a, x(k + 7), S14, &HFD469501
  839. 			FF a, b, c, d, x(k + 8), S11, &H698098D8
  840. 			FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
  841. 			FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
  842. 			FF b, c, d, a, x(k + 11), S14, &H895CD7BE
  843. 			FF a, b, c, d, x(k + 12), S11, &H6B901122
  844. 			FF d, a, b, c, x(k + 13), S12, &HFD987193
  845. 			FF c, d, a, b, x(k + 14), S13, &HA679438E
  846. 			FF b, c, d, a, x(k + 15), S14, &H49B40821
  847.  
  848. 			GG a, b, c, d, x(k + 1), S21, &HF61E2562
  849. 			GG d, a, b, c, x(k + 6), S22, &HC040B340
  850. 			GG c, d, a, b, x(k + 11), S23, &H265E5A51
  851. 			GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
  852. 			GG a, b, c, d, x(k + 5), S21, &HD62F105D
  853. 			GG d, a, b, c, x(k + 10), S22, &H2441453
  854. 			GG c, d, a, b, x(k + 15), S23, &HD8A1E681
  855. 			GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
  856. 			GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
  857. 			GG d, a, b, c, x(k + 14), S22, &HC33707D6
  858. 			GG c, d, a, b, x(k + 3), S23, &HF4D50D87
  859. 			GG b, c, d, a, x(k + 8), S24, &H455A14ED
  860. 			GG a, b, c, d, x(k + 13), S21, &HA9E3E905
  861. 			GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
  862. 			GG c, d, a, b, x(k + 7), S23, &H676F02D9
  863. 			GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
  864.  
  865. 			HH a, b, c, d, x(k + 5), S31, &HFFFA3942
  866. 			HH d, a, b, c, x(k + 8), S32, &H8771F681
  867. 			HH c, d, a, b, x(k + 11), S33, &H6D9D6122
  868. 			HH b, c, d, a, x(k + 14), S34, &HFDE5380C
  869. 			HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
  870. 			HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
  871. 			HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
  872. 			HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
  873. 			HH a, b, c, d, x(k + 13), S31, &H289B7EC6
  874. 			HH d, a, b, c, x(k + 0), S32, &HEAA127FA
  875. 			HH c, d, a, b, x(k + 3), S33, &HD4EF3085
  876. 			HH b, c, d, a, x(k + 6), S34, &H4881D05
  877. 			HH a, b, c, d, x(k + 9), S31, &HD9D4D039
  878. 			HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
  879. 			HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
  880. 			HH b, c, d, a, x(k + 2), S34, &HC4AC5665
  881.  
  882. 			II a, b, c, d, x(k + 0), S41, &HF4292244
  883. 			II d, a, b, c, x(k + 7), S42, &H432AFF97
  884. 			II c, d, a, b, x(k + 14), S43, &HAB9423A7
  885. 			II b, c, d, a, x(k + 5), S44, &HFC93A039
  886. 			II a, b, c, d, x(k + 12), S41, &H655B59C3
  887. 			II d, a, b, c, x(k + 3), S42, &H8F0CCC92
  888. 			II c, d, a, b, x(k + 10), S43, &HFFEFF47D
  889. 			II b, c, d, a, x(k + 1), S44, &H85845DD1
  890. 			II a, b, c, d, x(k + 8), S41, &H6FA87E4F
  891. 			II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
  892. 			II c, d, a, b, x(k + 6), S43, &HA3014314
  893. 			II b, c, d, a, x(k + 13), S44, &H4E0811A1
  894. 			II a, b, c, d, x(k + 4), S41, &HF7537E82
  895. 			II d, a, b, c, x(k + 11), S42, &HBD3AF235
  896. 			II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
  897. 			II b, c, d, a, x(k + 9), S44, &HEB86D391
  898.  
  899. 			a = AddUnsigned(a, AA)
  900. 			b = AddUnsigned(b, BB)
  901. 			c = AddUnsigned(c, CC)
  902. 			d = AddUnsigned(d, DD)
  903. 		Next
  904.  
  905. 		MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
  906. 	End Function
  907.  
  908. End Class
  909.  

page last modified: 2024-04-16; loaded in 0.0211 seconds