【VB.NET】快速哈希表(MD5、SHA、CRC32)支持输出格式文本

 

这是以前我集合的一个计算数据哈希校验值的一个类,最近又拿出来翻新了一下,仅供各位博友参考。

这个方法主要封装了Cryptography内的类型,另外自己添加了比较常用的CRC32算法,以及数据转换到文本的方法。

不建议用这个类库内的函数进行大型数据的校验,因为这里的函数都必须要完全将数据读入内存之后执行算法。如果要进行大量数据的校验,建议自己使用递进算法进行计算。

 

  
    
Imports System.Security.Cryptography

''' <summary> 计算哈希校验值,本类型内方法仅供小型计算 </summary>
Public NotInheritable Class ClsoHash

' CRC32 算法
Shared Function CRC32( ByVal data() As Byte ) As UInt32
Static crc As UInt32, crctbl( 255 ) As UInt32
If data.Length = 0 Then Return 0
If crc = 0 Then
For i As Short = 0 To 255
crc
= i
For j As Byte = 0 To 7
If crc And 1 Then crc = (crc >> 1 ) Xor & HEDB88320 & Else crc >>= 1
Next
crctbl(i)
= crc
Next
crc
= 1
End If
CRC32
= UInt32.MaxValue
For Each b As Byte In data
b
= b Xor (CRC32 And & HFF)
CRC32
>>= 8
CRC32
= CRC32 Xor crctbl(b)
Next
Return Not CRC32
End Function

' Adler32 算法
Shared Function Adler32( ByVal data() As Byte , ByVal offset As Integer , ByVal count As Integer ) As UInteger
Dim checksum As UInteger = 1
Const BASE As UInteger = 65521
Dim s1 As UInteger = checksum And 65535
Dim s2 As UInteger = checksum >> 16
While count > 0
Dim n As Integer = 3800
If n > count Then n = count
count
-= n
While n > 0
s1
= s1 + CUInt((data(offset) And 255 )) : offset += 1
s2
= s2 + s1
n
-= 1
End While
s1
= s1 Mod BASE
s2
= s2 Mod BASE
End While
Return (s2 << 16 ) Or s1
End Function
Shared Function Adler32( ByVal buffer As Byte ()) As UInteger
Return Adler32(buffer, 0 , buffer.Length)
End Function


' MD5算法
Shared Function MD5( ByVal data() As Byte ) As Byte ()
Return ( New MD5CryptoServiceProvider).ComputeHash(data)
End Function
' MD5CSP算法
Shared Function MD5CSP( ByVal data() As Byte ) As Byte ()
Return ( New MD5CryptoServiceProvider).ComputeHash(data)
End Function
' HMACMD5算法
Shared Function HMACMD5( ByVal data() As Byte ) As Byte ()
Return ( New HMACMD5).ComputeHash(data)
End Function
' HMACRIPEMD160算法
Shared Function HMACRIPEMD160( ByVal data() As Byte ) As Byte ()
Return ( New HMACRIPEMD160).ComputeHash(data)
End Function

' SHA 1、256、384、512算法
Shared Function SHA1( ByVal data() As Byte ) As Byte ()
Return ( New SHA1Managed).ComputeHash(data)
End Function
Shared Function SHA256( ByVal data() As Byte ) As Byte ()
Return ( New SHA256Managed).ComputeHash(data)
End Function
Shared Function SHA384( ByVal data() As Byte ) As Byte ()
Return ( New SHA384Managed).ComputeHash(data)
End Function
Shared Function SHA512( ByVal data() As Byte ) As Byte ()
Return ( New SHA512Managed).ComputeHash(data)
End Function
' HMACSHA 1、256、384、512算法
Shared Function HMACSHA1( ByVal data() As Byte ) As Byte ()
Return ( New HMACSHA1).ComputeHash(data)
End Function
Shared Function HMACSHA256( ByVal data() As Byte ) As Byte ()
Return ( New HMACSHA256).ComputeHash(data)
End Function
Shared Function HMACSHA384( ByVal data() As Byte ) As Byte ()
Return ( New HMACSHA384).ComputeHash(data)
End Function
Shared Function HMACSHA512( ByVal data() As Byte ) As Byte ()
Return ( New HMACSHA512).ComputeHash(data)
End Function

' RIPEMD160算法
Shared Function RIPEMD160( ByVal data() As Byte ) As Byte ()
Return ( New RIPEMD160Managed).ComputeHash(data)
End Function


' 获取各种哈希数据字符串表示
Shared Function HashToText( ByVal data() As Byte ) As String
Dim sb As New System.Text.StringBuilder
For Each b As Byte In data
sb.Append(b.ToString(
" X2 " ))
Next
Return sb.ToString
End Function
' CRC32 和 Adler32 的字符串表示
Shared Function HashToText( ByVal uint As UInteger ) As String
Return Hex (uint)
End Function
Shared Function HashToText( ByVal int As Integer ) As String
Return Hex ( int )
End Function

Shared Function HashToBase64( ByVal data() As Byte ) As String
Return Convert.ToBase64String(data)
End Function

End Class

你可能感兴趣的:(VB.NET)