Der Snnippet
http://dotnet-snippets.de/dns/md5-string-validieren-SID466.aspx
von Jan Welker brachte mich auf die Idee eine kleine Klasse zu schreiben um mehrere Hashtypen zu validieren statt nur einen. Einfach den Hashtyp und den Hash angeben - Fertig.
Man kann entweder ein String angeben oder ein Byte Array angeben.
Imports System.Text.RegularExpressions
Public Class HashValidator
Public Enum ALGO As Integer
CRC16 = 4
CRC32 = 8
MD5 = 32
SHA1 = 40
SHA256 = 64
SHA384 = 96
SHA512 = 128
End Enum
Public Shared Function IsValid(ByVal Expression As String, ByVal Algorythm As ALGO) As Boolean
Dim Validator As Regex = Nothing
Select Case Algorythm
Case ALGO.CRC16 : Validator = New Regex("^([a-fA-F0-9]){4}$")
Case ALGO.CRC32 : Validator = New Regex("^([a-fA-F0-9]){8}$")
Case ALGO.MD5 : Validator = New Regex("^([a-fA-F0-9]){32}$")
Case ALGO.SHA1 : Validator = New Regex("^([a-fA-F0-9]){40}$")
Case ALGO.SHA256 : Validator = New Regex("^([a-fA-F0-9]){64}$")
Case ALGO.SHA384 : Validator = New Regex("^([a-fA-F0-9]){96}$")
Case ALGO.SHA512 : Validator = New Regex("^([a-fA-F0-9]){128}$")
End Select
Return Validator.IsMatch(Expression)
End Function
Public Shared Function IsValid(ByVal Expression() As Byte, ByVal Algorythm As ALGO) As Boolean
Dim TmpValue As String = ""
Dim HashString As String = ""
For i As Integer = 0 To Expression.Length - 1
TmpValue = Hex(Expression(i))
If Len(TmpValue) = 1 Then TmpValue = "0" & TmpValue
HashString &= TmpValue
Next
Return IsValid(HashString, Algorythm)
End Function
End Class
Kommentare zum Snippet