我如何知道字符串是否为MD5格式

时间:2014-11-24 08:12:43

标签: vb.net md5

我想检查vb.net中的字符串是否为md5格式。我在php上发现了类似的东西,但我不知道这是否可能在.net你们有谁知道怎么做?

1 个答案:

答案 0 :(得分:1)

选项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(一步检查并获取字节 - 尝试将字符串解析为字节数组)

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