📄 md5.bas
字号:
Attribute VB_Name = "MD50"
'OperConA ,OperConB ,OperConC ,OperConD为初始化数字
'OperConA, OperConD,OperConB,OperConC之间可以相互对调
Const OperConA = "0x01234567"
Const OperConB = "0x89abcdef"
Const OperConC = "0xfedcba98"
Const OperConD = "0x76543210"
's11,s12,s13,s14为第一轮的参数
Const s11 = 7
Const s12 = 12
Const s13 = 17
Const s14 = 22
's21,s22,s23,s24为第二轮的参数
Const s21 = 5
Const s22 = 9
Const s23 = 14
Const s24 = 20
's31,s32,s33,s34为第三轮的参数
Const s31 = 4
Const s32 = 11
Const s33 = 16
Const s34 = 23
's41,s42,s43,s44为第四轮的参数
Const s41 = 6
Const s42 = 10
Const s43 = 15
Const s44 = 21
Dim padding(64) As Long '常数数组
Private Type context
count(2) As Long
state(4) As Long
buffer(64) As String
End Type
Dim newcontext As context
Private Sub md5memcopy(dststr() As String, scrstr() As String, strlen As Integer)
Dim i As Integer
For i = 0 To strlen
dststr(i) = scrstr(i)
Next
End Sub
'将字符型11位16进制数据转化为长整型数据
Private Function ConvNum(inputstr As String) As Long
'设计步骤:按一般化输入处理
'设计时考虑输入的字串是否超过11位(0x,h,加上8位数据),是否有不在0-9,a-f
'范围之外的数字(输入的16进制范围在0-9,a-f),将字串转化为长整型数据,不用转化函数
'采用保险的逐位求和的方法。某位值*16的n次方,将所有的和相加得到结果
End Function
'DealWeiF,DealWeiG,DealWeiH,DealWeiI函数处理位操作
Private Function DealWeiF(x As Long, Y As Long, Z As Long) As Long
DealWeiF = x And Y Or Not (x) And Z
End Function
Private Function DealWeiG(x As Long, Y As Long, Z As Long) As Long
DealWeiG = x And Y Or Y And Not (Z)
End Function
Private Function DealWeiH(x As Long, Y As Long, Z As Long) As Long
DealWeiH = x Xor Y Xor Z
End Function
Private Function DealWeiI(x As Long, Y As Long, Z As Long) As Long
DealWeiI = Y Xor (Not (Z) Or x)
End Function
'第一轮计算函数
Private Function FistRoundOp(a As Long, b As Long, c As Long, d As Long, k As Integer, s As Integer, i As Long) As Long
a = a + DealWeiF(b, c, d) + k + i
FistRoundOp = MoveUp(a, s) + b
End Function
'第二轮计算函数
Private Function SecondRoundOp(a As Long, b As Long, c As Long, d As Long, k As Integer, s As Integer, i As Long) As Long
a = a + DealWeiG(b, c, d) + k + i
SecondRoundOp = MoveUp(a, s) + b
End Function
'第三轮计算函数
Private Function ThreeRoundOp(a As Long, b As Long, c As Long, d As Long, k As Integer, s As Integer, i As Long) As Long
a = a + DealWeiH(b, c, d) + k + i
ThreeRoundOp = MoveUp(a, s) + b
End Function
'第四轮计算函数
Private Function fourroundop(a As Long, b As Long, c As Long, d As Long, k As Integer, s As Integer, i As Long) As Long
a = a + DealWeiI(b, c, d) + k + i
fourroundop = MoveUp(a, s) + b
End Function
'最后一次计算函数
Private Function LastRoundOp(a As Long, aa As Long) As Long
LastRoundOp = a + aa
End Function
Private Sub md5init()
Dim i As Integer
newcontext.count(0) = newcontext.count(1) = 0
padding(0) = ConvNum("0x00000080")
For i = 1 To 63
padding(0) = 0
Next
newcontext.state(0) = ConvNum(OperConA)
newcontext.state(1) = ConvNum(OperConB)
newcontext.state(2) = ConvNum(OperConC)
newcontext.state(3) = ConvNum(OperConD)
End Sub
Private Sub md5update(inputstr() As String, inputlen As Integer)
Dim i As Integer, partlen As Integer, index As Integer
index = (newcontext.count(0) \ 8 And 64)
newcontext.count(0) = newcontext.count(0) + inputlen * 8
If newcontext.count(0) < inputlen * 8 Then
newcontext.count(1) = newcontext.count(1) + 1
End If
newcontext.count(1) = newcontext.count(1) + inputlen \ (2 ^ 29)
' context->count[1] += ((UINT4)inputLen >> 29);
partlen = 64 - index
' Transform as many times as possible.
If (inputlen >= partlen) Then
'MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen);
Call md5memcopy(newcontext.buffer(index), inputstr(partlen), partlen)
Call md5transform(newcontext.buffer(index))
For i = partlen To inputlen - 63 Step 64
Call md5transform(inputstr(i))
index = 0
Else
i = 0
End If
' Buffer remaining input
' MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
Call md5memcopy(newcontext.buffer(index), inputstr(i), inputlen - i)
End Sub
'Decodes input (unsigned char) into output (UINT4). Assumes len is
'a multiple of 4.
Private Sub decode(outputstr() As String, inputstr() As String, strlen As Integer)
Dim i As Integer, j As Integer
i = 0
For j = 0 To strlen Step 4
outputstr(i) = CStr(CLng(inputstr(j)) Or CLng((inputstr(j + 1)) / (2 ^ 8)) Or _
CLng((inputstr(j + 2)) / (2 ^ 16)) Or CLng((inputstr(j + 3)) / (2 ^ 24)))
i = i + 1
Next
End Sub
'Encodes input (UINT4) into output (unsigned char). Assumes len is
'a multiple of 4.
Private Sub encode(outputstr() As String, anyinput() As Long, strlen As Integer)
Dim i As Integer
Dim j As Integer
i = 0
For j = 0 To strlen Step 4
output(j) = CStr(anyinput(i) And 256)
output(j + 1) = CStr(anyinput(i) \ (2 ^ 8) And 256)
output(j + 2) = CStr(anyinput(i) \ (2 ^ 16) And 256)
output(j + 3) = CStr(anyinput(i) \ (2 ^ 24) And 256)
i = i + 1
Next
End Sub
Private Sub md5memsetstr(outputstr() As Long, value As Integer, strlen As Integer)
Dim i As Integer
For i = 0 To strlen
outputstr(i) = CStr(value)
Next
End Sub
Private Sub md5memset(anyoutput() As Long, value As Integer, strlen As Integer)
Dim i As Integer
For i = 0 To strlen
anyoutput(i) = value
Next
End Sub
'MD5 finalization. Ends an MD5 message-digest operation, writing the
' the message digest and zeroizing the context.
Private Sub md5final(digest() As String)
Dim bits(8) As String
Dim index As Integer, padLen As Integer
' /* Save number of bits */
Call encode(bits(8), newcontext.count(1), 8)
' /* Pad out to 56 mod 64.
index = ((newcontext.count(0) \ 8) & 64)
If index < 56 Then
padLen = 56 - index
Else
padLen = 120 - index
End If
Call md5update(CStr(padding(64)), padLen)
' /* Append length (before padding) */
Call md5update(CStr(bits(8)), 8)
'/* Store state in digest */
Call encode(CStr(digest(16)), newcontext.state(4), 16)
'/* Zeroize sensitive information.
Call md5memsetstr(newcontext.buffer(64), 0, 64)
End Sub
Private Sub md5transform(block() As String)
' UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Dim a As Long, b As Long, c As Long, d As Long
Dim x(16) As Long
Call decode(x(16), block(64), 64)
a = newcontext.state(0)
b = newcontext.state(1)
c = newcontext.state(2)
d = newcontext.state(3)
'第一轮操作
a = FistRoundOp(a, b, c, d, x(0), s11, ConvNum("0xd76aa478"))
a = FistRoundOp(d, a, b, c, x(1), s12, ConvNum("0xe8c7b756"))
a = FistRoundOp(c, d, a, b, x(2), s13, ConvNum("0x242070db"))
a = FistRoundOp(b, c, d, a, x(3), s14, ConvNum("0xc1bdceee"))
a = FistRoundOp(a, b, c, d, x(4), s11, ConvNum("0xf57c0faf"))
a = FistRoundOp(d, a, b, c, x(5), s12, ConvNum("0x4787c62a"))
a = FistRoundOp(c, d, a, b, x(6), s13, ConvNum("0xa8304613"))
a = FistRoundOp(b, c, d, a, x(7), s14, ConvNum("0xfd469501"))
a = FistRoundOp(a, b, c, d, x(8), s11, ConvNum("0x698098d8"))
a = FistRoundOp(d, a, b, c, x(9), s12, ConvNum("0x8b44f7af"))
a = FistRoundOp(c, d, a, b, x(10), s13, ConvNum("0xffff5bb1"))
a = FistRoundOp(b, c, d, a, x(11), s14, ConvNum("0x895cd7be"))
a = FistRoundOp(a, b, c, d, x(12), s11, ConvNum("0x6b901122"))
a = FistRoundOp(d, a, b, c, x(13), s12, ConvNum("0xfd987193"))
a = FistRoundOp(c, d, a, b, x(14), s13, ConvNum("0xa679438e"))
a = FistRoundOp(b, c, d, a, x(15), s14, ConvNum("0x49b40821"))
'第二轮操作
a = SecondRoundOp(a, b, c, d, x(1), s21, ConvNum("0xf61e2562"))
a = SecondRoundOp(d, a, b, c, x(6), s22, ConvNum("0xc040b340"))
a = SecondRoundOp(c, d, a, b, x(11), s23, ConvNum("0x265e5a51"))
a = SecondRoundOp(b, c, d, a, x(0), s24, ConvNum("0xe9b6c7aa"))
a = SecondRoundOp(a, b, c, d, x(5), s21, ConvNum("0xd62f105d"))
a = SecondRoundOp(d, a, b, c, x(10), s22, ConvNum("0x02441453")) '*****
a = SecondRoundOp(c, d, a, b, x(15), s23, ConvNum("0xd8a1e681"))
a = SecondRoundOp(b, c, d, a, x(4), s24, ConvNum("0xe7d3fbc8"))
a = SecondRoundOp(a, b, c, d, x(9), s21, ConvNum("0x21e1cde6"))
a = SecondRoundOp(d, a, b, c, x(14), s22, ConvNum("0xc33707d6"))
a = SecondRoundOp(c, d, a, b, x(3), s23, ConvNum("0xf4d50d87"))
a = SecondRoundOp(b, c, d, a, x(8), s24, ConvNum("0x455a14ed"))
a = SecondRoundOp(a, b, c, d, x(13), s21, ConvNum("0xa9e3e905"))
a = SecondRoundOp(d, a, b, c, x(2), s22, ConvNum("0xfcefa3f8"))
a = SecondRoundOp(c, d, a, b, x(7), s23, ConvNum("0x676f02d9"))
a = SecondRoundOp(b, c, d, a, x(12), s24, ConvNum("0x8d2a4c8a"))
'第三轮操作
a = ThreeRoundOp(a, b, c, d, x(5), s31, ConvNum("0xfffa3942"))
a = ThreeRoundOp(d, a, b, c, x(8), s32, ConvNum("0x8771f681"))
a = ThreeRoundOp(c, d, a, b, x(11), s33, ConvNum("0x6d9d6122"))
a = ThreeRoundOp(b, c, d, a, x(14), s34, ConvNum("0xfde5380c"))
a = ThreeRoundOp(a, b, c, d, x(1), s31, ConvNum("0xa4beea44"))
a = ThreeRoundOp(d, a, b, c, x(4), s32, ConvNum("0x4bdecfa9"))
a = ThreeRoundOp(c, d, a, b, x(7), s33, ConvNum("0xf6bb4b60"))
a = ThreeRoundOp(b, c, d, a, x(10), s34, ConvNum("0xbebfbc70"))
a = ThreeRoundOp(a, b, c, d, x(13), s31, ConvNum("0x289b7ec6"))
a = ThreeRoundOp(d, a, b, c, x(0), s32, ConvNum("0xeaa127fa"))
a = ThreeRoundOp(c, d, a, b, x(3), s33, ConvNum("0xd4ef3085"))
a = ThreeRoundOp(b, c, d, a, x(6), s34, ConvNum("0x04881d05")) '*****
a = ThreeRoundOp(a, b, c, d, x(9), s31, ConvNum("0xd9d4d039"))
a = ThreeRoundOp(d, a, b, c, x(12), s32, ConvNum("0xe6db99e5"))
a = ThreeRoundOp(c, d, a, b, x(15), s33, ConvNum("0x1fa27cf8"))
a = ThreeRoundOp(b, c, d, a, x(2), s34, ConvNum("0xc4ac5665"))
'第四轮操作
a = fourroundop(a, b, c, d, x(0), s41, ConvNum("0xf4292244"))
a = fourroundop(d, a, b, c, x(7), s42, ConvNum("0x432aff97"))
a = fourroundop(c, d, a, b, x(14), s43, ConvNum("0xab9423a7"))
a = fourroundop(b, c, d, a, x(5), s44, ConvNum("0xfc93a039"))
a = fourroundop(a, b, c, d, x(12), s41, ConvNum("0x655b59c3"))
a = fourroundop(d, a, b, c, x(3), s42, ConvNum("0x8f0ccc92"))
a = fourroundop(c, d, a, b, x(10), s43, ConvNum("0xffeff47d"))
a = fourroundop(b, c, d, a, x(1), s44, ConvNum("0x85845dd1"))
a = fourroundop(a, b, c, d, x(8), s41, ConvNum("0x6fa87e4f"))
a = fourroundop(d, a, b, c, x(15), s42, ConvNum("0xfe2ce6e0"))
a = fourroundop(c, d, a, b, x(6), s43, ConvNum("0xa3014314"))
a = fourroundop(b, c, d, a, x(13), s44, ConvNum("0x4e0811a1"))
a = fourroundop(a, b, c, d, x(4), s41, ConvNum("0xf7537e82"))
a = fourroundop(d, a, b, c, x(11), s42, ConvNum("0xbd3af235"))
a = fourroundop(c, d, a, b, x(2), s43, ConvNum("0x2ad7d2bb"))
a = fourroundop(b, c, d, a, x(9), s44, ConvNum("0xeb86d391"))
newcontext.state(0) = LastRoundOp(a, newcontext.state(0))
newcontext.state(1) = LastRoundOp(b, newcontext.state(1))
newcontext.state(2) = LastRoundOp(c, newcontext.state(2))
newcontext.state(3) = LastRoundOp(d, newcontext.state(3))
Call md5memset(x(16), 0, 16)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -