1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
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
|