Get MD5 hash of an embedded resource

I have an embedded resource bitmap with resource compiler and need to get the md5 hash of the file when the program starts.

Any suggestion is appreciated.
If the resource is static, why does it have to be calculated at runtime?
If you are getting an MD5 hash of an embedded resource at runtime to try to prevent possible corruption/substitution of the resource doing it at runtime will do not good.

If the resource is corrupt or replaced the hash will be from the bad resource.

You need to get the MD5 beforehand, and then program a check at startup if the MD5 matches the original hash.
You need to get the MD5 beforehand, and then program a check at startup if the MD5 matches the original hash.

which implies doing it at runtime to check the stored number. But any half decent hacker can change the hash if they can change the data, if you are using such a common and old algorithm. It may slow them down, but
-everyone knows md5
-the hacker has the original bitmap that he took out
- the hacker has the new bitmap
- you haven't really increased security.
if you at least did something screwy like hashed every nth byte of the data, maybe in reverse, they would at least have to disassemble your code to get to the bottom of it.
- you can screw with the round robin constants to have something md5-like that isn't md-5 as well, that would be aggravating to figure out, esp if you just kept on calling your routine md-5.

md5 is a bit of a long, but not too complex algorithm. do you need the code for it or something? Ive got one that I lifted from the web and sped up a bit if you can't find one for yourself. Its probably more c-ish than some would care for, but it works.

Last edited on

Thank you for the suggestions. Yes, what I am asking is how to extract the blob or binary data to match it against with.

How do I get or extract the data so I can perform the md5 comparison?
that I can't answer. How do you extract it to display it? "bitmap" is also a risky term, to some it means an RGB array and to others, an object representing a windows file format that has a few extra fields alongside the data array.

Can you get to an RGB or RGBA array of the image? It should be ok to just do that part; if the image data is the same then the other fields are pretty much irrelevant. You can check the size (rows/cols) field(s) first to save time: if the sizes mismatch its a no-go.

however you choose to do it must match ... the same bytes on the pre-computed answer and the live computed result ... of course!
Last edited on
Thanks for the follow-up reply jonnin!

I actually found a C# equivalent before posting this question that does exactly what I need, but I can't find any C++ way of doing it so far.

keyword: GetManifestResourceStream

There isn't one in pure c++. Dunno if windows libs pull in that function or not. Or some other library?

you need some way to look at what you have as if it were a disk file or a block of bytes. Once you have that you just run MD5 on it, which you can do with either someone's library and a bunch of effort or just grab the code and dump that into a function in your project.

the entire concept of resources is more of a windows concept (but the same idea is used in many other places like data-dlls / wadfiles / etc type things) so I would look there first. Some of this stuff may be under the managed umbrella and I have avoided that like the plague -- I saw what M$ did to java, and Ive seen what they did to c++, and Im not having any of it :)

try
https://stackoverflow.com/questions/1986205/embedding-resource-in-a-c-cli-project
or ask google 'get resource in c++ windows'
maybe https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadresource
not sure, a lot of the stuff is so dated, and I haven't done a real gui in over a decade.
Last edited on
Thank you guys for the responses and thanks jonnin for the follow-up resource links. With LoadResource and other API related functions, I finally got this one solved.

1
2
3
4
HRSRC myResource = FindResource(NULL, MAKEINTRESOURCE(MYBITMAP_ID), RT_BITMAP);
unsigned int myResourceSize = SizeofResource(NULL, myResource);
HGLOBAL myResourceData = LoadResource(NULL, myResource);
void* pBinaryData = LockResource(myResourceData);


Now I just convert the pBinaryData to string or char and make the conversion using any md5 c++ library.
Last edited on
Topic archived. No new replies allowed.