简体   繁体   中英

Is there a way to test if a string is an MD5 hash?

I am trying to input a text file that contains MD5 hashes and keywords (one per line) into a C# app. Is there a way to check if a string is an MD5 hash? I looked on MSDN and couldn't find anything in the MD5 class.

Use Regex like this:

public static bool IsMD5(string input)
{
    if (String.IsNullOrEmpty(input))
    {
        return false;
    }

    return Regex.IsMatch(input, "^[0-9a-fA-F]{32}$", RegexOptions.Compiled);
}

Well, an MD5 hash is really just binary data - if you've got a string then it's presumably encoded in some fashion, eg base64 or hex. You can test whether the string is correctly encoded for the right length of binary (16 bytes). That's all though - while there may be binary values which are never the result of hashing any data, I highly doubt that you can recognise such values. Ideally, there should be no such values, of course...

A MD5 hash is a 128 bit value. It is usually represented as a byte[] with a length of 16, or as a string where each byte is represented by two hexadecimal digits. A MD5 hash has no internal structure or any kind of 'signature' that allows you detect if a 128 bit value is a MD5 hash or not.

如果它的32字节长和0-9 af它可能是md5,但不是100%

首先要检查文件以确定MD5哈希值是如何编码的,然后根据它设计匹配项。

I think the correct one is this one that includes also the capitals sometimes hashes come also in capitals so why miss that.

[0-9a-fA-F]{32}

or

[0-9a-f]{32}(?i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM