S.Michaelides
S.Michaelides

Reputation: 87

How Can i know if a string is on MD5 format

I want to check if a string is on md5 format in vb.net. I have found something similar on php but i do not know if this is possible in .net Does anyone of you know how to do this ?

Upvotes: 0

Views: 390

Answers (1)

dovid
dovid

Reputation: 6462

option 1:

Dim reg = New RegularExpressions.Regex("[0-9a-f]{32}", RegularExpressions.RegexOptions.Compiled)
Function LooksMd5(str As String) As Boolean
    Return reg.IsMatch(str)
End Function

option2 (check and get bytes in one step - try parse string to bytes array)

Function TryParseHex32(str As String, ByRef result As Byte()) As Boolean
    If str.Length <> 32 Then Return False

    ReDim result(16)

    For i = 0 To 16
        Try
            result(i) = Convert.ToByte(str.Substring(i * 2, 2), 16)
            'or result(i) = Byte.Parse(str.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber)
        Catch ex As Exception
            result = Nothing
            Return False
        End Try
    Next
    Return True
End Function

Upvotes: 2

Related Questions