开发者

Saving a MD5 hash of a text file into the same text file?

Today I read about MD5 hash and was wondering if this is possible. seems like a recursive problem..开发者_Go百科. or is there a solution?


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.


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.


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.


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 :)


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜