Diese Klasse vereint alle gängigen Hash verfahren in einer Klasse. Man kann entweder Strings berechnen oder auch Dateien
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class clsHash
Public Enum HASH As Integer
MD5 = 0
SHA1 = 1
SHA256 = 2
SHA384 = 3
SHA512 = 4
End Enum
''' <summary>
''' Diese Funktion berechnet einen Hash von einem String
''' </summary>
''' <param name="Value">Den String von dem ein Hash berechnet werden soll</param>
''' <param name="Hash">Gibt den Hashalgorithmus an</param>
Public Shared Function HashString(ByVal Value As String, ByVal Hash As HASH) As String
Dim Data(0) As Byte
Dim HashValue(0) As Byte
Dim Result As String = ""
Dim Tmp As String = ""
Select Case Hash
Case clsHash.HASH.MD5
Dim MD5 As New MD5CryptoServiceProvider
Data = Encoding.ASCII.GetBytes(Value)
HashValue = MD5.ComputeHash(Data)
Case clsHash.HASH.SHA1
Dim SHA1 As New SHA1Managed
Data = Encoding.ASCII.GetBytes(Value)
HashValue = SHA1.ComputeHash(Data)
Case clsHash.HASH.SHA256
Dim SHA256 As New SHA256Managed
Data = Encoding.ASCII.GetBytes(Value)
HashValue = SHA256.ComputeHash(Data)
Case clsHash.HASH.SHA384
Dim SHA384 As New SHA384Managed
Data = Encoding.ASCII.GetBytes(Value)
HashValue = SHA384.ComputeHash(Data)
Case clsHash.HASH.SHA512
Dim SHA512 As New SHA512Managed
Data = Encoding.ASCII.GetBytes(Value)
HashValue = SHA512.ComputeHash(Data)
End Select
For i As Integer = 0 To HashValue.Length - 1
Tmp = Hex(HashValue(i))
If Len(Tmp) = 1 Then Tmp = "0" & Tmp
Result += Tmp
Next
Return Result
End Function
''' <summary>
''' Diese Funktion berechnet einen Hash von einer Datei
''' </summary>
''' <param name="File">Die Datei von der ein Hash berechnet werden soll</param>
''' <param name="Hash">Gibt den Hashalgorithmus an</param>
Public Shared Function HashFile(ByVal File As String, ByVal Hash As HASH) As String
Dim FN As New FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Dim HashValue(0) As Byte
Dim Result As String = ""
Dim Tmp As String = ""
Select Case Hash
Case clsHash.HASH.MD5
Dim MD5 As New MD5CryptoServiceProvider
MD5.ComputeHash(FN)
HashValue = MD5.Hash
Case clsHash.HASH.SHA1
Dim SHA1 As New SHA1Managed
SHA1.ComputeHash(FN)
HashValue = SHA1.Hash
Case clsHash.HASH.SHA256
Dim SHA256 As New SHA256Managed
SHA256.ComputeHash(FN)
HashValue = SHA256.Hash
Case clsHash.HASH.SHA384
Dim SHA384 As New SHA384Managed
SHA384.ComputeHash(FN)
HashValue = SHA384.Hash
Case clsHash.HASH.SHA512
Dim SHA512 As New SHA512Managed
SHA512.ComputeHash(FN)
HashValue = SHA512.Hash
End Select
FN.Close()
For i As Integer = 0 To HashValue.Length - 1
Tmp = Hex(HashValue(i))
If Len(Tmp) = 1 Then Tmp = "0" & Tmp
Result += Tmp
Next
Return Result
End Function
End Class
Kommentare zum Snippet