ASP - 生成md5

相关代码

使用方式:

1
2
3
<%
md5("Hello World")
%>

相关函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%
function md5(str)
md5 = bytesToHex(md5hashBytes(stringToUTFBytes(str)))
end function

function stringToUTFBytes(aString)
dim UTF8 : set UTF8 = Server.CreateObject("System.Text.UTF8Encoding")
stringToUTFBytes = UTF8.GetBytes_4(aString)
end function

function md5hashBytes(aBytes)
dim MD5 : set MD5 = Server.CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")

MD5.Initialize()
'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
md5hashBytes = MD5.ComputeHash_2((aBytes))
end function

function bytesToHex(aBytes)
dim hexStr, x
for x = 1 to lenb(aBytes)
hexStr = hex(ascb(midb((aBytes), x, 1)))
if len(hexStr) = 1 then hexStr = "0" & hexStr
bytesToHex = bytesToHex & hexStr
next
end function
%>

生成结果与使用PHP语言的md5函数生成的结果一致

参考