这个方法主要封装了Cryptography内的类型,另外自己添加了比较常用的CRC32算法,以及数据转换到文本的方法。
不建议用这个类库内的函数进行大型数据的校验,因为这里的函数都必须要完全将数据读入内存之后执行算法。如果要进行大量数据的校验,建议自己使用递进算法进行计算。
代码
1
Imports
System.Security.Cryptography
2
3 ''' <summary> 计算哈希校验值,本类型内方法仅供小型计算 </summary>
4 Public NotInheritable Class ClsoHash
5
6 ' CRC32 算法
7 Shared Function CRC32( ByVal data() As Byte ) As UInt32
8 Static crc As UInt32, crctbl( 255 ) As UInt32
9 If data.Length = 0 Then Return 0
10 If crc = 0 Then
11 For i As Short = 0 To 255
12 crc = i
13 For j As Byte = 0 To 7
14 If crc And 1 Then crc = (crc >> 1 ) Xor & HEDB88320 & Else crc >>= 1
15 Next
16 crctbl(i) = crc
17 Next
18 crc = 1
19 End If
20 CRC32 = UInt32.MaxValue
21 For Each b As Byte In data
22 b = b Xor (CRC32 And & HFF)
23 CRC32 >>= 8
24 CRC32 = CRC32 Xor crctbl(b)
25 Next
26 Return Not CRC32
27 End Function
28
29 ' Adler32 算法
30 Shared Function Adler32( ByVal data() As Byte , ByVal offset As Integer , ByVal count As Integer ) As UInteger
31 Dim checksum As UInteger = 1
32 Const BASE As UInteger = 65521
33 Dim s1 As UInteger = checksum And 65535
34 Dim s2 As UInteger = checksum >> 16
35 While count > 0
36 Dim n As Integer = 3800
37 If n > count Then n = count
38 count -= n
39 While n > 0
40 s1 = s1 + CUInt((data(offset) And 255 )) : offset += 1
41 s2 = s2 + s1
42 n -= 1
43 End While
44 s1 = s1 Mod BASE
45 s2 = s2 Mod BASE
46 End While
47 Return (s2 << 16 ) Or s1
48 End Function
49 Shared Function Adler32( ByVal buffer As Byte ()) As UInteger
50 Return Adler32(buffer, 0 , buffer.Length)
51 End Function
52
53
54 ' MD5算法
55 Shared Function MD5( ByVal data() As Byte ) As Byte ()
56 Return ( New MD5CryptoServiceProvider).ComputeHash(data)
57 End Function
58 ' MD5CSP算法
59 Shared Function MD5CSP( ByVal data() As Byte ) As Byte ()
60 Return ( New MD5CryptoServiceProvider).ComputeHash(data)
61 End Function
62 ' HMACMD5算法
63 Shared Function HMACMD5( ByVal data() As Byte ) As Byte ()
64 Return ( New HMACMD5).ComputeHash(data)
65 End Function
66 ' HMACRIPEMD160算法
67 Shared Function HMACRIPEMD160( ByVal data() As Byte ) As Byte ()
68 Return ( New HMACRIPEMD160).ComputeHash(data)
69 End Function
70
71 ' SHA 1、256、384、512算法
72 Shared Function SHA1( ByVal data() As Byte ) As Byte ()
73 Return ( New SHA1Managed).ComputeHash(data)
74 End Function
75 Shared Function SHA256( ByVal data() As Byte ) As Byte ()
76 Return ( New SHA256Managed).ComputeHash(data)
77 End Function
78 Shared Function SHA384( ByVal data() As Byte ) As Byte ()
79 Return ( New SHA384Managed).ComputeHash(data)
80 End Function
81 Shared Function SHA512( ByVal data() As Byte ) As Byte ()
82 Return ( New SHA512Managed).ComputeHash(data)
83 End Function
84 ' HMACSHA 1、256、384、512算法
85 Shared Function HMACSHA1( ByVal data() As Byte ) As Byte ()
86 Return ( New HMACSHA1).ComputeHash(data)
87 End Function
88 Shared Function HMACSHA256( ByVal data() As Byte ) As Byte ()
89 Return ( New HMACSHA256).ComputeHash(data)
90 End Function
91 Shared Function HMACSHA384( ByVal data() As Byte ) As Byte ()
92 Return ( New HMACSHA384).ComputeHash(data)
93 End Function
94 Shared Function HMACSHA512( ByVal data() As Byte ) As Byte ()
95 Return ( New HMACSHA512).ComputeHash(data)
96 End Function
97
98 ' RIPEMD160算法
99 Shared Function RIPEMD160( ByVal data() As Byte ) As Byte ()
100 Return ( New RIPEMD160Managed).ComputeHash(data)
101 End Function
102
103
104 ' 获取各种哈希数据字符串表示
105 Shared Function HashToText( ByVal data() As Byte ) As String
106 Dim sb As New System.Text.StringBuilder
107 For Each b As Byte In data
108 sb.Append(b.ToString( " X2 " ))
109 Next
110 Return sb.ToString
111 End Function
112 ' CRC32 和 Adler32 的字符串表示
113 Shared Function HashToText( ByVal uint As UInteger ) As String
114 Return Hex (uint)
115 End Function
116 Shared Function HashToText( ByVal int As Integer ) As String
117 Return Hex ( int )
118 End Function
119
120 Shared Function HashToBase64( ByVal data() As Byte ) As String
121 Return Convert.ToBase64String(data)
122 End Function
123
124 End Class
2
3 ''' <summary> 计算哈希校验值,本类型内方法仅供小型计算 </summary>
4 Public NotInheritable Class ClsoHash
5
6 ' CRC32 算法
7 Shared Function CRC32( ByVal data() As Byte ) As UInt32
8 Static crc As UInt32, crctbl( 255 ) As UInt32
9 If data.Length = 0 Then Return 0
10 If crc = 0 Then
11 For i As Short = 0 To 255
12 crc = i
13 For j As Byte = 0 To 7
14 If crc And 1 Then crc = (crc >> 1 ) Xor & HEDB88320 & Else crc >>= 1
15 Next
16 crctbl(i) = crc
17 Next
18 crc = 1
19 End If
20 CRC32 = UInt32.MaxValue
21 For Each b As Byte In data
22 b = b Xor (CRC32 And & HFF)
23 CRC32 >>= 8
24 CRC32 = CRC32 Xor crctbl(b)
25 Next
26 Return Not CRC32
27 End Function
28
29 ' Adler32 算法
30 Shared Function Adler32( ByVal data() As Byte , ByVal offset As Integer , ByVal count As Integer ) As UInteger
31 Dim checksum As UInteger = 1
32 Const BASE As UInteger = 65521
33 Dim s1 As UInteger = checksum And 65535
34 Dim s2 As UInteger = checksum >> 16
35 While count > 0
36 Dim n As Integer = 3800
37 If n > count Then n = count
38 count -= n
39 While n > 0
40 s1 = s1 + CUInt((data(offset) And 255 )) : offset += 1
41 s2 = s2 + s1
42 n -= 1
43 End While
44 s1 = s1 Mod BASE
45 s2 = s2 Mod BASE
46 End While
47 Return (s2 << 16 ) Or s1
48 End Function
49 Shared Function Adler32( ByVal buffer As Byte ()) As UInteger
50 Return Adler32(buffer, 0 , buffer.Length)
51 End Function
52
53
54 ' MD5算法
55 Shared Function MD5( ByVal data() As Byte ) As Byte ()
56 Return ( New MD5CryptoServiceProvider).ComputeHash(data)
57 End Function
58 ' MD5CSP算法
59 Shared Function MD5CSP( ByVal data() As Byte ) As Byte ()
60 Return ( New MD5CryptoServiceProvider).ComputeHash(data)
61 End Function
62 ' HMACMD5算法
63 Shared Function HMACMD5( ByVal data() As Byte ) As Byte ()
64 Return ( New HMACMD5).ComputeHash(data)
65 End Function
66 ' HMACRIPEMD160算法
67 Shared Function HMACRIPEMD160( ByVal data() As Byte ) As Byte ()
68 Return ( New HMACRIPEMD160).ComputeHash(data)
69 End Function
70
71 ' SHA 1、256、384、512算法
72 Shared Function SHA1( ByVal data() As Byte ) As Byte ()
73 Return ( New SHA1Managed).ComputeHash(data)
74 End Function
75 Shared Function SHA256( ByVal data() As Byte ) As Byte ()
76 Return ( New SHA256Managed).ComputeHash(data)
77 End Function
78 Shared Function SHA384( ByVal data() As Byte ) As Byte ()
79 Return ( New SHA384Managed).ComputeHash(data)
80 End Function
81 Shared Function SHA512( ByVal data() As Byte ) As Byte ()
82 Return ( New SHA512Managed).ComputeHash(data)
83 End Function
84 ' HMACSHA 1、256、384、512算法
85 Shared Function HMACSHA1( ByVal data() As Byte ) As Byte ()
86 Return ( New HMACSHA1).ComputeHash(data)
87 End Function
88 Shared Function HMACSHA256( ByVal data() As Byte ) As Byte ()
89 Return ( New HMACSHA256).ComputeHash(data)
90 End Function
91 Shared Function HMACSHA384( ByVal data() As Byte ) As Byte ()
92 Return ( New HMACSHA384).ComputeHash(data)
93 End Function
94 Shared Function HMACSHA512( ByVal data() As Byte ) As Byte ()
95 Return ( New HMACSHA512).ComputeHash(data)
96 End Function
97
98 ' RIPEMD160算法
99 Shared Function RIPEMD160( ByVal data() As Byte ) As Byte ()
100 Return ( New RIPEMD160Managed).ComputeHash(data)
101 End Function
102
103
104 ' 获取各种哈希数据字符串表示
105 Shared Function HashToText( ByVal data() As Byte ) As String
106 Dim sb As New System.Text.StringBuilder
107 For Each b As Byte In data
108 sb.Append(b.ToString( " X2 " ))
109 Next
110 Return sb.ToString
111 End Function
112 ' CRC32 和 Adler32 的字符串表示
113 Shared Function HashToText( ByVal uint As UInteger ) As String
114 Return Hex (uint)
115 End Function
116 Shared Function HashToText( ByVal int As Integer ) As String
117 Return Hex ( int )
118 End Function
119
120 Shared Function HashToBase64( ByVal data() As Byte ) As String
121 Return Convert.ToBase64String(data)
122 End Function
123
124 End Class