patrafter
patrafter

Reputation: 39

Regular expression: searching an arbitrary file for MD5

See the following ASCII string of 34 characters. I'm trying to have 3 matches of MD5 hash on this string.

8AC905DD4AB2038E5F7EABEAE792AC41BC

[A-F0-9]{32} matches only the first 32 characters.

This is part of binary reverse engineering project I have.

How I can get 3 different matches from the mentioned string? Any idea is appreciated.

Cheers,

Upvotes: 2

Views: 880

Answers (1)

Tadeck
Tadeck

Reputation: 137350

You did not say what language do you use, thus I provided two snippets - for Python and for PHP. But the clue here is the (?=([A-F0-9]{32})) pattern (see examples below).

Solution (Python)

There is a solution, and the following is written in Python:

>>> import re 
>>> data = '8AC905DD4AB2038E5F7EABEAE792AC41BC'
>>> matches = re.finditer(r'(?=([A-F0-9]{32}))', data)
>>> results = [match.group(1) for match in matches]

Test

The result will be (as in this demo):

>>> results
['8AC905DD4AB2038E5F7EABEAE792AC41', 'AC905DD4AB2038E5F7EABEAE792AC41B', 'C905DD4AB2038E5F7EABEAE792AC41BC']

which is what you have been expecting.

Solution (PHP)

In PHP it could look like this:

<?php

$data = '8AC905DD4AB2038E5F7EABEAE792AC41BC';
$results = array();
preg_match_all('/(?=([A-F0-9]{32}))/', $data, $results);

and the $results would be the following array:

Array
(
    [0] => 8AC905DD4AB2038E5F7EABEAE792AC41
    [1] => AC905DD4AB2038E5F7EABEAE792AC41B
    [2] => C905DD4AB2038E5F7EABEAE792AC41BC
)

See this demo for a proof.

Upvotes: 3

Related Questions