1 #Region "Microsoft.VisualBasic::a6c09009091285fce2178f685885711e, Microsoft.VisualBasic.Core\Extensions\Security\Md5HashProvider.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class Md5HashProvider
35     
36     '         FunctionGetMd5Bytes, (+2 OverloadsGetMd5Hash, GetMd5hashLong
37     
38     '         Sub: (+2 Overloads) Dispose
39     
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.Runtime.CompilerServices
46 Imports System.Security.Cryptography
47 Imports System.Text
48
49 Namespace SecurityString
50
51     ''' <summary>
52     ''' 并行化的需求
53     ''' </summary>
54     ''' <remarks></remarks>
55     Public Class Md5HashProvider : Implements IDisposable
56
57         ReadOnly md5Hash As MD5 = Security.Cryptography.MD5.Create()
58
59         Public Function GetMd5Hash(input As StringAs String
60             If String.IsNullOrEmpty(input) Then
61                 Return ""
62             End If
63
64             ' Convert the input string to a byte array and compute the hash.
65             Dim data As Byte() = Encoding.UTF8.GetBytes(input)
66             Return GetMd5Hash(input:=data)
67         End Function
68
69         Public Function GetMd5Bytes(input As Byte()) As Byte()
70             If input.IsNullOrEmpty Then
71                 Return {}
72             End If
73
74             Dim data As Byte() = md5Hash.ComputeHash(input)
75             Return data
76         End Function
77
78         ''' <summary>
79         ''' <see cref="MD5.ComputeHash"/> -> <see cref="ToLong"/>
80         ''' </summary>
81         ''' <param name="input"></param>
82         ''' <returns></returns>
83         ''' 
84         <MethodImpl(MethodImplOptions.AggressiveInlining)>
85         Public Function GetMd5hashLong(input As Byte()) As Long
86             Return GetMd5Bytes(input).ToLong
87         End Function
88
89         Public Function GetMd5Hash(input As Byte()) As String
90             If input.IsNullOrEmpty Then
91                 Return ""
92             End If
93
94             ' Convert the input string to a byte array and compute the hash.
95             Dim data As Byte() = md5Hash.ComputeHash(input)
96             ' Create a new Stringbuilder to collect the bytes
97             ' and create a string.
98             Dim sBuilder As New StringBuilder()
99
100             Loop through each byte of the hashed data
101             ' and format each one as a hexadecimal string.
102             For i As Integer = 0 To data.Length - 1
103                 sBuilder.Append(data(i).ToString("x2"))
104             Next i
105
106             Return the hexadecimal string.
107             Return sBuilder.ToString()
108         End Function
109
110 #Region "IDisposable Support"
111         Private disposedValue As Boolean To detect redundant calls
112
113         ' IDisposable
114         Protected Overridable Sub Dispose(disposing As Boolean)
115             If Not disposedValue Then
116                 If disposing Then
117                     ' TODO: dispose managed state (managed objects).
118                     Call md5Hash.Dispose()
119                 End If
120
121                 ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
122                 ' TODO: set large fields to null.
123             End If
124             disposedValue = True
125         End Sub
126
127         ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
128         'Protected Overrides Sub Finalize()
129         '    Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
130         '    Dispose(False)
131         '    MyBase.Finalize()
132         'End Sub
133
134         ' This code added by Visual Basic to correctly implement the disposable pattern.
135         Public Sub Dispose() Implements IDisposable.Dispose
136             Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
137             Dispose(True)
138             GC.SuppressFinalize(Me)
139         End Sub
140 #End Region
141     End Class
142 End Namespace