md5sum a file that contain the sum itself?

2019-07-30 19:29发布

I have written a small app in C++ consisting of a single EXE file.

I want to put in its "about" dialog the md5sum of the executable itself. It should be embedded statically into the executable (so that can be seen from hex editor), rather than computed on the fly.

3条回答
Emotional °昔
2楼-- · 2019-07-30 19:40

As both @Shi & @matthewdaniel have already said, this can't be done directly.
However a couple of workarounds are possible:

  • Calculating MD5 of your application, and packaging your executable inside a container app that will simply extract it and check it's MD5
  • Compiling your code and hashing only the code segments or other segments (except the Data), and than adding the MD5 check code. This will work as the MD5 string will be stored in the Data segment keeping the validity of the precalculated hash of any other memory segment valid.
查看更多
啃猪蹄的小仙女
3楼-- · 2019-07-30 19:55

As soon as you add the md5 to the file the file will have a different md5. There is no way to get the md5 in the file itself.

查看更多
女痞
4楼-- · 2019-07-30 19:58

This is not possible.

If you enter the md5 hash into the binary, the binary will change, so the md5 hash changes as well. If you create a new one, and try to add it to the binary, the binary will change again.

So best is to put the hash into a file, and read that file and display its content.

Another way could be to create the md5 hash of the binary, and then append it to the executable. In order to fetch the value, you read the last 32 byte of the binary and display it as md5. Of course, if you create a hash of the complete executable, it won't match the hash - you have to create the hash of the executable excluding the last 32 byte.

If you store the 128 bit md5 hash in a raw format (base 256 instead of base 16), you only need 16 byte.

查看更多
登录 后发表回答