In this article let’s understand how we can create a regex for MD5 strings and how it can be matched.
MD5 (Message-Digest Algorithm 5) is a widely-used cryptographic hash function that generates a fixed-size, 128-bit (16-byte) output from any input data. It is commonly used to verify the integrity of data, such as files downloaded from the internet and to generate hash values for indexing or comparison purposes.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Let’s look at regex expression for base64 strings. We will try a few approaches right from simple approach to RFC or advanced approach.
Regex to match MD5 string
Regular Expression for MD5 String can be represented as-
/^[a-fA-F0-9]{32}$/gm
Alternatively you can use the i
modifier in the regex takes care of the UPPERCASE letters.
/^[a-f0-9]{32}$/i
Test string examples for the above regex-
Input String | Match Output |
---|---|
ThisIsNotMD5 | does not match |
ff68180b384113ac235dabcad86a86a8 | matches |
fedaded989aed98a9fbbe29c999df50f | matches |
zxcvbncvwfrqghdfjgyetwrteyrutitureg | does not match |
Here is a detailed explanation of the above regex-
/^[a-fA-F0-9]{32}$/gm
^ asserts position at start of a line
Match a single character present in the list below [a-fA-F0-9]
{128} matches the previous token exactly 128 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
/gm matches the characters /gm literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to match MD5 regex pattern. In conclusion, understanding how to create and match MD5 strings using regular expressions can greatly enhance data integrity verification and pattern recognition. MD5’s cryptographic hash function and regex’s powerful text manipulation capabilities combine to provide valuable tools for developers and security professionals alike.