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