md5.asp
上传用户:rblchem
上传日期:2022-04-27
资源大小:1941k
文件大小:12k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. <%
  2. ' Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm,
  3. ' as set out in the memo RFC1321.
  4. '
  5. ' See the VB6 project that accompanies this sample for full code comments on how
  6. ' it works.
  7. '
  8. ' ASP VBScript code for generating an MD5 'digest' or 'signature' of a string. The
  9. ' MD5 algorithm is one of the industry standard methods for generating digital
  10. ' signatures. It is generically known as a digest, digital signature, one-way
  11. ' encryption, hash or checksum algorithm. A common use for MD5 is for password
  12. ' encryption as it is one-way in nature, that does not mean that your passwords
  13. ' are not free from a dictionary attack.
  14. '
  15. ' This is 'free' software with the following restrictions:
  16. '
  17. ' You may not redistribute this code as a 'sample' or 'demo'. However, you are free
  18. ' to use the source code in your own code, but you may not claim that you created
  19. ' the sample code. It is expressly forbidden to sell or profit from this source code
  20. ' other than by the knowledge gained or the enhanced value added by your own code.
  21. '
  22. ' Use of this software is also done so at your own risk. The code is supplied as
  23. ' is without warranty or guarantee of any kind.
  24. '
  25. ' Should you wish to commission some derivative work based on this code provided
  26. ' here, or any consultancy work, please do not hesitate to contact us.
  27. '
  28. ' Web Site:  http://www.frez.co.uk
  29. ' E-mail:    sales@frez.co.uk
  30. Private Const BITS_TO_A_BYTE = 8
  31. Private Const BYTES_TO_A_WORD = 4
  32. Private Const BITS_TO_A_WORD = 32
  33. Private m_lOnBits(30)
  34. Private m_l2Power(30)
  35.  
  36.     m_lOnBits(0) = CLng(1)
  37.     m_lOnBits(1) = CLng(3)
  38.     m_lOnBits(2) = CLng(7)
  39.     m_lOnBits(3) = CLng(15)
  40.     m_lOnBits(4) = CLng(31)
  41.     m_lOnBits(5) = CLng(63)
  42.     m_lOnBits(6) = CLng(127)
  43.     m_lOnBits(7) = CLng(255)
  44.     m_lOnBits(8) = CLng(511)
  45.     m_lOnBits(9) = CLng(1023)
  46.     m_lOnBits(10) = CLng(2047)
  47.     m_lOnBits(11) = CLng(4095)
  48.     m_lOnBits(12) = CLng(8191)
  49.     m_lOnBits(13) = CLng(16383)
  50.     m_lOnBits(14) = CLng(32767)
  51.     m_lOnBits(15) = CLng(65535)
  52.     m_lOnBits(16) = CLng(131071)
  53.     m_lOnBits(17) = CLng(262143)
  54.     m_lOnBits(18) = CLng(524287)
  55.     m_lOnBits(19) = CLng(1048575)
  56.     m_lOnBits(20) = CLng(2097151)
  57.     m_lOnBits(21) = CLng(4194303)
  58.     m_lOnBits(22) = CLng(8388607)
  59.     m_lOnBits(23) = CLng(16777215)
  60.     m_lOnBits(24) = CLng(33554431)
  61.     m_lOnBits(25) = CLng(67108863)
  62.     m_lOnBits(26) = CLng(134217727)
  63.     m_lOnBits(27) = CLng(268435455)
  64.     m_lOnBits(28) = CLng(536870911)
  65.     m_lOnBits(29) = CLng(1073741823)
  66.     m_lOnBits(30) = CLng(2147483647)
  67.     
  68.     m_l2Power(0) = CLng(1)
  69.     m_l2Power(1) = CLng(2)
  70.     m_l2Power(2) = CLng(4)
  71.     m_l2Power(3) = CLng(8)
  72.     m_l2Power(4) = CLng(16)
  73.     m_l2Power(5) = CLng(32)
  74.     m_l2Power(6) = CLng(64)
  75.     m_l2Power(7) = CLng(128)
  76.     m_l2Power(8) = CLng(256)
  77.     m_l2Power(9) = CLng(512)
  78.     m_l2Power(10) = CLng(1024)
  79.     m_l2Power(11) = CLng(2048)
  80.     m_l2Power(12) = CLng(4096)
  81.     m_l2Power(13) = CLng(8192)
  82.     m_l2Power(14) = CLng(16384)
  83.     m_l2Power(15) = CLng(32768)
  84.     m_l2Power(16) = CLng(65536)
  85.     m_l2Power(17) = CLng(131072)
  86.     m_l2Power(18) = CLng(262144)
  87.     m_l2Power(19) = CLng(524288)
  88.     m_l2Power(20) = CLng(1048576)
  89.     m_l2Power(21) = CLng(2097152)
  90.     m_l2Power(22) = CLng(4194304)
  91.     m_l2Power(23) = CLng(8388608)
  92.     m_l2Power(24) = CLng(16777216)
  93.     m_l2Power(25) = CLng(33554432)
  94.     m_l2Power(26) = CLng(67108864)
  95.     m_l2Power(27) = CLng(134217728)
  96.     m_l2Power(28) = CLng(268435456)
  97.     m_l2Power(29) = CLng(536870912)
  98.     m_l2Power(30) = CLng(1073741824)
  99. Private Function LShift(lValue, iShiftBits)
  100.     If iShiftBits = 0 Then
  101.         LShift = lValue
  102.         Exit Function
  103.     ElseIf iShiftBits = 31 Then
  104.         If lValue And 1 Then
  105.             LShift = &H80000000
  106.         Else
  107.             LShift = 0
  108.         End If
  109.         Exit Function
  110.     ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  111.         Err.Raise 6
  112.     End If
  113.     If (lValue And m_l2Power(31 - iShiftBits)) Then
  114.         LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
  115.     Else
  116.         LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
  117.     End If
  118. End Function
  119. Private Function RShift(lValue, iShiftBits)
  120.     If iShiftBits = 0 Then
  121.         RShift = lValue
  122.         Exit Function
  123.     ElseIf iShiftBits = 31 Then
  124.         If lValue And &H80000000 Then
  125.             RShift = 1
  126.         Else
  127.             RShift = 0
  128.         End If
  129.         Exit Function
  130.     ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  131.         Err.Raise 6
  132.     End If
  133.     
  134.     RShift = (lValue And &H7FFFFFFE)  m_l2Power(iShiftBits)
  135.     If (lValue And &H80000000) Then
  136.         RShift = (RShift Or (&H40000000  m_l2Power(iShiftBits - 1)))
  137.     End If
  138. End Function
  139. Private Function RotateLeft(lValue, iShiftBits)
  140.     RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
  141. End Function
  142. Private Function AddUnsigned(lX, lY)
  143.     Dim lX4
  144.     Dim lY4
  145.     Dim lX8
  146.     Dim lY8
  147.     Dim lResult
  148.  
  149.     lX8 = lX And &H80000000
  150.     lY8 = lY And &H80000000
  151.     lX4 = lX And &H40000000
  152.     lY4 = lY And &H40000000
  153.  
  154.     lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
  155.  
  156.     If lX4 And lY4 Then
  157.         lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
  158.     ElseIf lX4 Or lY4 Then
  159.         If lResult And &H40000000 Then
  160.             lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
  161.         Else
  162.             lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
  163.         End If
  164.     Else
  165.         lResult = lResult Xor lX8 Xor lY8
  166.     End If
  167.  
  168.     AddUnsigned = lResult
  169. End Function
  170. Private Function F(x, y, z)
  171.     F = (x And y) Or ((Not x) And z)
  172. End Function
  173. Private Function G(x, y, z)
  174.     G = (x And z) Or (y And (Not z))
  175. End Function
  176. Private Function H(x, y, z)
  177.     H = (x Xor y Xor z)
  178. End Function
  179. Private Function I(x, y, z)
  180.     I = (y Xor (x Or (Not z)))
  181. End Function
  182. Private Sub FF(a, b, c, d, x, s, ac)
  183.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac))
  184.     a = RotateLeft(a, s)
  185.     a = AddUnsigned(a, b)
  186. End Sub
  187. Private Sub GG(a, b, c, d, x, s, ac)
  188.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac))
  189.     a = RotateLeft(a, s)
  190.     a = AddUnsigned(a, b)
  191. End Sub
  192. Private Sub HH(a, b, c, d, x, s, ac)
  193.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac))
  194.     a = RotateLeft(a, s)
  195.     a = AddUnsigned(a, b)
  196. End Sub
  197. Private Sub II(a, b, c, d, x, s, ac)
  198.     a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac))
  199.     a = RotateLeft(a, s)
  200.     a = AddUnsigned(a, b)
  201. End Sub
  202. Private Function ConvertToWordArray(sMessage)
  203.     Dim lMessageLength
  204.     Dim lNumberOfWords
  205.     Dim lWordArray()
  206.     Dim lBytePosition
  207.     Dim lByteCount
  208.     Dim lWordCount
  209.     
  210.     Const MODULUS_BITS = 512
  211.     Const CONGRUENT_BITS = 448
  212.     
  213.     lMessageLength = Len(sMessage)
  214.     
  215.     lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS)  BITS_TO_A_BYTE))  (MODULUS_BITS  BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS  BITS_TO_A_WORD)
  216.     ReDim lWordArray(lNumberOfWords - 1)
  217.     
  218.     lBytePosition = 0
  219.     lByteCount = 0
  220.     Do Until lByteCount >= lMessageLength
  221.         lWordCount = lByteCount  BYTES_TO_A_WORD
  222.         lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  223.         lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
  224.         lByteCount = lByteCount + 1
  225.     Loop
  226.     lWordCount = lByteCount  BYTES_TO_A_WORD
  227.     lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  228.     lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
  229.     lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
  230.     lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
  231.     
  232.     ConvertToWordArray = lWordArray
  233. End Function
  234. Private Function WordToHex(lValue)
  235.     Dim lByte
  236.     Dim lCount
  237.     
  238.     For lCount = 0 To 3
  239.         lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
  240.         WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
  241.     Next
  242. End Function
  243. Public Function MD5(sMessage)
  244.     Dim x
  245.     Dim k
  246.     Dim AA
  247.     Dim BB
  248.     Dim CC
  249.     Dim DD
  250.     Dim a
  251.     Dim b
  252.     Dim c
  253.     Dim d
  254.     
  255.     Const S11 = 7
  256.     Const S12 = 12
  257.     Const S13 = 17
  258.     Const S14 = 22
  259.     Const S21 = 5
  260.     Const S22 = 9
  261.     Const S23 = 14
  262.     Const S24 = 20
  263.     Const S31 = 4
  264.     Const S32 = 11
  265.     Const S33 = 16
  266.     Const S34 = 23
  267.     Const S41 = 6
  268.     Const S42 = 10
  269.     Const S43 = 15
  270.     Const S44 = 21
  271.     x = ConvertToWordArray(sMessage)
  272.     
  273.     a = &H67452301
  274.     b = &HEFCDAB89
  275.     c = &H98BADCFE
  276.     d = &H10325476
  277.     For k = 0 To UBound(x) Step 16
  278.         AA = a
  279.         BB = b
  280.         CC = c
  281.         DD = d
  282.     
  283.         FF a, b, c, d, x(k + 0), S11, &HD76AA478
  284.         FF d, a, b, c, x(k + 1), S12, &HE8C7B756
  285.         FF c, d, a, b, x(k + 2), S13, &H242070DB
  286.         FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
  287.         FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
  288.         FF d, a, b, c, x(k + 5), S12, &H4787C62A
  289.         FF c, d, a, b, x(k + 6), S13, &HA8304613
  290.         FF b, c, d, a, x(k + 7), S14, &HFD469501
  291.         FF a, b, c, d, x(k + 8), S11, &H698098D8
  292.         FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
  293.         FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
  294.         FF b, c, d, a, x(k + 11), S14, &H895CD7BE
  295.         FF a, b, c, d, x(k + 12), S11, &H6B901122
  296.         FF d, a, b, c, x(k + 13), S12, &HFD987193
  297.         FF c, d, a, b, x(k + 14), S13, &HA679438E
  298.         FF b, c, d, a, x(k + 15), S14, &H49B40821
  299.     
  300.         GG a, b, c, d, x(k + 1), S21, &HF61E2562
  301.         GG d, a, b, c, x(k + 6), S22, &HC040B340
  302.         GG c, d, a, b, x(k + 11), S23, &H265E5A51
  303.         GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
  304.         GG a, b, c, d, x(k + 5), S21, &HD62F105D
  305.         GG d, a, b, c, x(k + 10), S22, &H2441453
  306.         GG c, d, a, b, x(k + 15), S23, &HD8A1E681
  307.         GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
  308.         GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
  309.         GG d, a, b, c, x(k + 14), S22, &HC33707D6
  310.         GG c, d, a, b, x(k + 3), S23, &HF4D50D87
  311.         GG b, c, d, a, x(k + 8), S24, &H455A14ED
  312.         GG a, b, c, d, x(k + 13), S21, &HA9E3E905
  313.         GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
  314.         GG c, d, a, b, x(k + 7), S23, &H676F02D9
  315.         GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
  316.             
  317.         HH a, b, c, d, x(k + 5), S31, &HFFFA3942
  318.         HH d, a, b, c, x(k + 8), S32, &H8771F681
  319.         HH c, d, a, b, x(k + 11), S33, &H6D9D6122
  320.         HH b, c, d, a, x(k + 14), S34, &HFDE5380C
  321.         HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
  322.         HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
  323.         HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
  324.         HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
  325.         HH a, b, c, d, x(k + 13), S31, &H289B7EC6
  326.         HH d, a, b, c, x(k + 0), S32, &HEAA127FA
  327.         HH c, d, a, b, x(k + 3), S33, &HD4EF3085
  328.         HH b, c, d, a, x(k + 6), S34, &H4881D05
  329.         HH a, b, c, d, x(k + 9), S31, &HD9D4D039
  330.         HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
  331.         HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
  332.         HH b, c, d, a, x(k + 2), S34, &HC4AC5665
  333.     
  334.         II a, b, c, d, x(k + 0), S41, &HF4292244
  335.         II d, a, b, c, x(k + 7), S42, &H432AFF97
  336.         II c, d, a, b, x(k + 14), S43, &HAB9423A7
  337.         II b, c, d, a, x(k + 5), S44, &HFC93A039
  338.         II a, b, c, d, x(k + 12), S41, &H655B59C3
  339.         II d, a, b, c, x(k + 3), S42, &H8F0CCC92
  340.         II c, d, a, b, x(k + 10), S43, &HFFEFF47D
  341.         II b, c, d, a, x(k + 1), S44, &H85845DD1
  342.         II a, b, c, d, x(k + 8), S41, &H6FA87E4F
  343.         II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
  344.         II c, d, a, b, x(k + 6), S43, &HA3014314
  345.         II b, c, d, a, x(k + 13), S44, &H4E0811A1
  346.         II a, b, c, d, x(k + 4), S41, &HF7537E82
  347.         II d, a, b, c, x(k + 11), S42, &HBD3AF235
  348.         II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
  349.         II b, c, d, a, x(k + 9), S44, &HEB86D391
  350.     
  351.         a = AddUnsigned(a, AA)
  352.         b = AddUnsigned(b, BB)
  353.         c = AddUnsigned(c, CC)
  354.         d = AddUnsigned(d, DD)
  355.     Next
  356.     
  357.     MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
  358. End Function
  359. %>