ASP VBScript CRC32算法源码
参考VB6版的CRC32算法所写:
ASP的Byte()不像VB6的Byte()那样,可以直接用Byte(位置)得到其二进制值。所以,这里用了ado.stream,既读取文件的二进制,又顺便用AscB(objAdo.Read(1))来代替Buffer(i)。
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Response.Write CRC32("D:\aaa.txt")
Public Function CRC32(Path)
Dim objAdo
Set objAdo = CreateObject("adodb.stream")
objAdo.Open
objAdo.Type = 1
objAdo.LoadFromFile Path
Dim crc32Result
Dim i
Dim j
Dim dwCrc
Dim iLookup
Dim Lb
Dim Ub
'常数
Const Num0 = &H0
Const Num1 = &H1
Const Num2 = &H2
Const Num8 = &H8
Const Num255 = &HFF
Const Num256 = &H100
Const Num16777215 = &HFFFFFF
Const dwPolynomial = &HEDB88320
Const Num2147483647 = &H7FFFFFFF
Const NumNegative1 = &HFFFFFFFF
Const NumNegative2 = &HFFFFFFFE
Const NumNegative256 = &HFFFFFF00
'CRC32表
Dim crc32Table(&HFF)
'初始化CRC32表
For i = Num0 To Num255
dwCrc = i
For j = Num8 To Num1 Step NumNegative1
If (dwCrc And Num1) Then
dwCrc = ((dwCrc And NumNegative2) \ Num2) And Num2147483647
dwCrc = dwCrc Xor dwPolynomial
Else
dwCrc = ((dwCrc And NumNegative2) \ Num2) And Num2147483647
End If
Next
crc32Table(i) = dwCrc
Next
crc32Result = NumNegative1
'计算CRC32码
For i = 0 To objAdo.Size-1
objAdo.Position=i
iLookup = (crc32Result And Num255) Xor AscB(objAdo.Read(1))
crc32Result = ((crc32Result And NumNegative256) \ Num256) And Num16777215
crc32Result = crc32Result Xor crc32Table(iLookup)
Next
CRC32 = Hex(Not (crc32Result))
End Function
%>