1 |
#Region "Microsoft.VisualBasic::20f1fc1f750fc08e30c48889e5f3b55a, Microsoft.VisualBasic.Core\Extensions\Security\Md5.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
#End Region
|
43 |
|
44 |
Imports System.Runtime.CompilerServices
|
45 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
46 |
Imports Microsoft.VisualBasic.Linq
|
47 |
Imports Microsoft.VisualBasic.Scripting.MetaData
|
48 |
|
49 |
Namespace SecurityString
|
50 |
|
51 |
<Package("Md5Hash", Publisher:="Microsoft Corp.", Description:="Represents the abstract class from which all implementations of the System.Security.Cryptography.MD5 hash algorithm inherit.")>
|
52 |
Public Module MD5Hash
|
53 |
|
54 |
<ExportAPI("Uid")>
|
55 |
Public Function NewUid() As String
|
56 |
Dim input As String = Guid.NewGuid.ToString & Now.ToString
|
57 |
Return GetMd5Hash(input)
|
58 |
End Function
|
59 |
|
60 |
ReadOnly __hashProvider As New Md5HashProvider
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
69 |
<ExportAPI("Md5")>
|
70 |
<Extension>
|
71 |
Public Function GetMd5Hash(input As String) As String
|
72 |
SyncLock __hashProvider
|
73 |
Return __hashProvider.GetMd5Hash(input)
|
74 |
End SyncLock
|
75 |
End Function
|
76 |
|
77 |
|
78 |
Gets the hashcode of the input string. (<paramref name="input"/> => <see cref="MD5Hash.GetMd5Hash"/> => <see cref="MD5Hash.ToLong(String)"/>)
|
79 |
|
80 |
<param name="input">任意字符串</param>
|
81 |
|
82 |
<ExportAPI("GetHashCode", Info:="Gets the hashcode of the input string.")>
|
83 |
Public Function GetHashCode(input As String) As Long
|
84 |
Dim md5 As String = MD5Hash.GetMd5Hash(input)
|
85 |
Return ToLong(md5)
|
86 |
End Function
|
87 |
|
88 |
|
89 |
Gets the hashcode of the input string.
|
90 |
|
91 |
|
92 |
<ExportAPI("GetHashCode", Info:="Gets the hashcode of the input string.")>
|
93 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
94 |
Public Function GetHashCode(data As IEnumerable(Of Byte)) As Long
|
95 |
Return GetMd5Hash(data.ToArray).ToLong
|
96 |
End Function
|
97 |
|
98 |
|
99 |
Gets the hashcode of the md5 string.
|
100 |
|
101 |
<param name="md5">计算所得到的MD5哈希值</param>
|
102 |
|
103 |
<ExportAPI("As.Long", Info:="Gets the hashcode of the md5 string.")>
|
104 |
<Extension> Public Function ToLong(md5 As String) As Long
|
105 |
Dim bytes = StringToByteArray(md5)
|
106 |
Return ToLong(bytes)
|
107 |
End Function
|
108 |
|
109 |
|
110 |
CityHash algorithm for convert the md5 hash value as a <see cref="Int64"/> value.
|
111 |
|
112 |
<param name="bytes">
|
113 |
this input value should compute from <see cref="Md5HashProvider.GetMd5Bytes(Byte())"/>
|
114 |
</param>
|
115 |
|
116 |
<remarks>
|
117 |
http://stackoverflow.com/questions/9661227/convert-md5-to-long
|
118 |
|
119 |
The very best solution I found (based on my needs... mix of speed and good hash function) is Google
|
120 |
The input can be any byte array including an MD5 result and the output is an unsigned 64-bit long.
|
121 |
'''
|
122 |
CityHash has a very good but Not perfect hash distribution, And Is very fast.
|
123 |
'''
|
124 |
I ported CityHash from C++ To C# In half an hour. A Java port should be straightforward too.
|
125 |
'''
|
126 |
Just XORing the bits doesn't give as good a distribution (though admittedly that will be very fast).
|
127 |
'''
|
128 |
I'm not familiar enough with Java to tell you exactly how to populate a long from a byte array
|
129 |
(there could be a good helper I
|
130 |
in Java wrong).
|
131 |
Essentially, though, you
|
132 |
'''
|
133 |
Long a = md5[0] * 256 * md5[1] + 256 * 256 * md5[2] + 256 * 256 * 256 * md5[3];
|
134 |
Long b = md5[4] * 256 * md5[5] + 256 * 256 * md5[6] + 256 * 256 * 256 * md5[7];
|
135 |
Long result = a ^ b;
|
136 |
|
137 |
Note I have made no attempt To deal With endianness. If you just care about a consistent hash value,
|
138 |
though, endianness should Not matter.
|
139 |
</remarks>
|
140 |
<ExportAPI("As.Long")> <Extension>
|
141 |
Public Function ToLong(bytes As Byte()) As Long
|
142 |
Dim md5 As Long() = bytes.Select(Function(x) CLng(x)).ToArray
|
143 |
Dim a As Long = md5(0) * 256 * md5(1) + 256 * 256 * md5(2) + 256 * 256 * 256 * md5(3)
|
144 |
Dim b As Long = md5(4) * 256 * md5(5) + 256 * 256 * md5(6) + 256 * 256 * 256 * md5(7)
|
145 |
Dim result As Long = a Xor b
|
146 |
|
147 |
Return result
|
148 |
End Function
|
149 |
|
150 |
|
151 |
由于md5是大小写无关的,故而在这里都会自动的被转换为小写形式,所以调用这个函数的时候不需要在额外的转换了
|
152 |
|
153 |
<param name="hex"></param>
|
154 |
|
155 |
<ExportAPI("As.Bytes")>
|
156 |
Public Function StringToByteArray(hex As String) As Byte()
|
157 |
Dim NumberChars As Integer = hex.Length
|
158 |
Dim bytes As Byte() = New Byte(NumberChars / 2 - 1) {}
|
159 |
|
160 |
hex = hex.ToLower
|
161 |
|
162 |
For i As Integer = 0 To NumberChars - 2 Step 2
|
163 |
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
|
164 |
Next
|
165 |
Return bytes
|
166 |
End Function
|
167 |
|
168 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
169 |
<ExportAPI("Md5")>
|
170 |
Public Function GetMd5Hash(input As Byte()) As String
|
171 |
Return New Md5HashProvider().GetMd5Hash(input)
|
172 |
End Function
|
173 |
|
174 |
<Extension>
|
175 |
Public Function GetMd5Hash2(Of T)(x As T) As String
|
176 |
Dim raw As String = x.ToString & x.GetHashCode
|
177 |
Return raw.GetMd5Hash
|
178 |
End Function
|
179 |
|
180 |
<Extension>
|
181 |
Public Function GetMd5Hash(Of T As Language.BaseClass)(x As T) As String
|
182 |
Dim raw As String = x.__toString & x.GetHashCode
|
183 |
Return raw.GetMd5Hash
|
184 |
End Function
|
185 |
|
186 |
|
187 |
Verify a hash against a string.
|
188 |
|
189 |
|
190 |
<param name="comparedHash"></param>
|
191 |
|
192 |
<remarks></remarks>
|
193 |
|
194 |
<ExportAPI("Md5.Verify", Info:="Verify a hash against a string.")>
|
195 |
Public Function VerifyMd5Hash(input As String, comparedHash As String) As Boolean
|
196 |
If String.IsNullOrEmpty(input) OrElse String.IsNullOrEmpty(comparedHash) Then
|
197 |
Return False
|
198 |
End If
|
199 |
|
200 |
Dim hashOfInput As String = GetMd5Hash(input)
|
201 |
Return String.Equals(hashOfInput, comparedHash, StringComparison.OrdinalIgnoreCase)
|
202 |
End Function
|
203 |
|
204 |
|
205 |
校验两个文件的哈希值是否一致
|
206 |
|
207 |
<param name="query"></param>
|
208 |
<param name="subject"></param>
|
209 |
|
210 |
<remarks></remarks>
|
211 |
|
212 |
<ExportAPI("File.Equals")>
|
213 |
Public Function VerifyFile(query As String, subject As String) As Boolean
|
214 |
Dim md5 As New Md5HashProvider()
|
215 |
Dim a1 As Byte() = IO.File.ReadAllBytes(query)
|
216 |
Dim a2 As Byte() = IO.File.ReadAllBytes(subject)
|
217 |
Dim m1 As String = md5.GetMd5Hash(a1)
|
218 |
Dim m2 As String = md5.GetMd5Hash(a2)
|
219 |
|
220 |
Return String.Equals(m1, m2)
|
221 |
End Function
|
222 |
|
223 |
|
224 |
Get the md5 hash calculation value for a specific file.(获取文件对象的哈希值,请注意,当文件不存在或者文件的长度为零的时候,会返回空字符串)
|
225 |
|
226 |
<param name="PathUri">The file path of the target file to be calculated.</param>
|
227 |
|
228 |
<remarks></remarks>
|
229 |
|
230 |
<ExportAPI("File.Md5", Info:="Get the md5 hash calculation value for a specific file.")>
|
231 |
<Extension>
|
232 |
Public Function GetFileHashString(<Parameter("Path.Uri", "The file path of the target file to be calculated.")> PathUri As String) As String
|
233 |
If Not PathUri.FileExists OrElse FileIO.FileSystem.GetFileInfo(PathUri).Length = 0 Then
|
234 |
Return ""
|
235 |
End If
|
236 |
|
237 |
Dim bufs As Byte() = IO.File.ReadAllBytes(PathUri)
|
238 |
Return GetMd5Hash(bufs)
|
239 |
End Function
|
240 |
|
241 |
|
242 |
SHA256 8 bits salt value for the private key.
|
243 |
|
244 |
<param name="value"></param>
|
245 |
|
246 |
<ExportAPI("SaltValue", Info:="SHA256 8 bits salt value for the private key.")>
|
247 |
Public Function SaltValue(value As String) As String
|
248 |
Dim hash As String = GetMd5Hash(value)
|
249 |
Dim chars() = {
|
250 |
hash(0),
|
251 |
hash(1),
|
252 |
hash(3),
|
253 |
hash(5),
|
254 |
hash(15),
|
255 |
hash(23),
|
256 |
hash(28),
|
257 |
hash(31)
|
258 |
}
|
259 |
Return chars.CharString
|
260 |
End Function
|
261 |
End Module
|
262 |
End Namespace
|