ASP 改良版MD5、SHA256多重加密类(二次及多次)(7)


ElseIf lX4 or lY4 Then
If lResult And &H40000000 Then
lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
Else
lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
End If
Else
lResult = lResult Xor lX8 Xor lY8
End If
SHA256_AddUnsigned = lResult
End Function
Private Function SHA256_CH(x, y, z)
SHA256_Ch = ((x And y) Xor ((Not x) And z))
End Function
Private Function SHA256_Maj(x, y, z)
SHA256_Maj = ((x And y) Xor (x And z) Xor (y And z))
End Function
Private Function SHA256_S(x, n)
SHA256_S = (SHA256_RShift(x, (n And SHA256_m_lOnBits(4))) or SHA256_LShift(x, (32 - (n And SHA256_m_lOnBits(4)))))
End Function
Private Function SHA256_R(x, n)
SHA256_R = SHA256_RShift(x, cLng(n And SHA256_m_lOnBits(4)))
End Function
Private Function SHA256_Sigma0(x)
SHA256_Sigma0 = (SHA256_S(x, 2) Xor SHA256_S(x, 13) Xor SHA256_S(x, 22))
End Function
Private Function SHA256_Sigma1(x)
SHA256_Sigma1 = (SHA256_S(x, 6) Xor SHA256_S(x, 11) Xor SHA256_S(x, 25))
End Function
Private Function SHA256_Gamma0(x)
SHA256_Gamma0 = (SHA256_S(x, 7) Xor SHA256_S(x, 18) Xor SHA256_R(x, 3))
End Function
Private Function SHA256_Gamma1(x)
SHA256_Gamma1 = (SHA256_S(x, 17) Xor SHA256_S(x, 19) Xor SHA256_R(x, 10))
End Function
Private Function SHA256_ConvertToWordArray(byVal sMessage)
Dim lMessageLength
Dim lNumberOfWords
Dim lWordArray()
Dim lBytePosition
Dim lByteCount
Dim lWordCount
Dim lByte
Const MODULUS_BITS = 512
Const CONGRUENT_BITS = 448
lMessageLength = Len(sMessage)
lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
ReDim lWordArray(lNumberOfWords - 1)
lBytePosition = 0
lByteCount = 0
Do Until lByteCount >= lMessageLength
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (3 - (lByteCount Mod BYTES_TO_A_WORD)) * BITS_TO_A_BYTE
lByte = AscB(Mid(sMessage, lByteCount + 1, 1))
lWordArray(lWordCount) = lWordArray(lWordCount) or SHA256_LShift(lByte, lBytePosition)
lByteCount = lByteCount + 1
Loop
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (3 - (lByteCount Mod BYTES_TO_A_WORD)) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) or SHA256_LShift(&H80, lBytePosition)
lWordArray(lNumberOfWords - 1) = SHA256_LShift(lMessageLength, 3)
lWordArray(lNumberOfWords - 2) = SHA256_RShift(lMessageLength, 29)
SHA256_ConvertToWordArray = lWordArray
End Function

' ==========================================================================================
' SHA256 加密算法
' ========================================================================================
' 调用方法:
' SHA256(sMessage, p_Len)
' p_Len控制生成字符串长度,1为8位,2为16位,最大取值为8,即加密后长度为64位
' 控制 8 位\16 位\24 位\32 位\40 位\48 位\56 位\64 位加密

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2198.html