English 中文(简体)
Is there a way to test if a string is an MD5 hash?
原标题:
  • 时间:2009-11-11 14:17:56
  •  标签:
  • c#
  • md5

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, e.g. 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.

if its 32 bytes long and 0-9 a-f its probably md5, but not 100%

First thing to do is examine the file to work out how the MD5 hashes are encoded, then design a match based on that.

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)




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签