简体   繁体   English

有没有办法测试字符串是否是MD5哈希?

[英]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. 我正在尝试将包含MD5哈希值和关键字(每行一个)的文本文件输入到C#应用程序中。 Is there a way to check if a string is an MD5 hash? 有没有办法检查字符串是否是MD5哈希? I looked on MSDN and couldn't find anything in the MD5 class. 我查看了MSDN,在MD5类中找不到任何内容。

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. 好吧,MD5哈希实际上只是二进制数据 - 如果你有一个字符串,那么它可能以某种方式编码,例如base64或hex。 You can test whether the string is correctly encoded for the right length of binary (16 bytes). 您可以测试字符串是否正确编码为正确长度的二进制(16字节)。 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. MD5哈希值是128位值。 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. 它通常表示为长度为16的byte[] ,或者表示为每个字节由两个十六进制数字表示的string 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. MD5哈希没有内部结构或任何类型的“签名”,允许您检测128位值是否为MD5哈希值。

如果它的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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