5

Today I read about MD5 hash and was wondering if this is possible. seems like a recursive problem... or is there a solution?

bakra
  • 387
  • 4
  • 15
  • Is this possible... md5(crap + x) == x ? like a document with its own MD5 info on its last page ? – bakra May 27 '10 at 11:34
  • Yes, as I wrote it is possible in theory - but to do so requires a lot of tries and luck ... you can't *construct* such a document with one shot. – tanascius May 27 '10 at 11:52

7 Answers7

4

It's not possible.

But you can rename the file according to the hash of its contents, which attaches the information without changing the hash.

Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
4

Consider any hash and imagine adding some random text to it. Since there is an infinite amount of possible texts you could add, but only a finite amount of possible hash values, there must be possible texts which yield the hash. The problem is just that you might not have enough resources to find it.

What you might be able to try is whether there are any MD5 hash values that, when hashed, yield themselves as a result (thanks to Francesco for that link!):
For all possible permutations of an MD5 hash, create the hash and compare the result with the original.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
  • 1
    That is, does the MD5 hash admit a fixed point? See http://stackoverflow.com/questions/235785/is-there-an-md5-fixed-point-where-md5x-x – Francesco May 27 '10 at 11:06
  • @Francesco: Thanks for the link, I added it to the answer. – sbi May 27 '10 at 11:09
  • I was thinking more of in these terms: md5(crap + x) == x? I mean a .doc containing other stuff + the MD5 hash info at the end. – bakra May 27 '10 at 11:32
  • @bakra: That's what my answer's first paragraph is all about. – sbi May 27 '10 at 11:41
3

Well, as soon as you fill in the MD5 sum the file will change and get a new MD5 - so: no, it is not feasible. In theory just trial-and-error could lead to a document that contains its own hash ... but the document would be probably quite garbled and not contain anything meaningful.

But it is possible to fill 128 bit with zeros (for instance) during calculation. This place will hold the MD5 sum and has to be zero again, when verifying the hash later.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
tanascius
  • 53,078
  • 22
  • 114
  • 136
2

You must exclude the hash sum from the calculation. Then you have use it, but you cant add the hash rum til the end of the file expecting it to be correct, or if you calculate the run over new yu will always end ut with a new MD5 hash. Ening up in a never ending story :)

eaanon01
  • 1,069
  • 7
  • 9
2

In theory, it's possible: possible file contents are infinite, possible hashes are not. In practise, being able to accomplish it would mean you've found a vulnerability in the algorithm, thus rendering the hash useless for security purposes.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
1

Well, adding a MD5 hash to a text changes the text, and thus the MD5 hash. Computing an MD5 hash of your text-including-the-to-be-computed MD5 hash is not possible.

Jochem Schulenklopper
  • 6,452
  • 4
  • 44
  • 62
0

Here is another trick...

Save Hash in begining of file and compute MD5 has excluding that region and only important data.

"HashMD5 = Md5.ComputeHash(bytes, 382, bytes.Length - 382)"

So it counts Hash only for data region(Custom Data Structure starting after offset 382 in file buffer)

-----------------------example data structure follow-----------

<StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Ansi)>
<Serializable()> Structure MyData
    Dim FileCheckSum() As Byte  '16 bytes HASh for file 'Config.bin'
    Dim Padding() As Byte        ' 0xFF x 20 bytes
    Dim RemoteDevice As RmtDevice

End Structure

In begining write this str to file config.bin using "xxxx.FileSystem.WriteAllBytes()"

Then open config file using hex editor find position of padding element start(in my case it was 382)

Later use this no as offset to calculate MD5 rather on whole file. This way one has little security against data corrupt.

Note!!- As long as no of structure elements/order/element nos remains same padding start position remains same.

Working for me..

Tested in .net

Pan
  • 1
  • 1