c++hashtimestampchecksumrecompile

Keep .exe timestamp from changing


Does anybody know of a way to prevent the timestamp of an executable from changing? I'm trying to generate a consistent hash code for the .exe but I think the timestamp may be preventing that from happening. Each time I recompile the code (VS C++) the FastSum generates a different checksum.


Solution

  • Depending on what you have to checksum, you can either strip off the COFF header (where the timestamp resides) or the Optional Header. In the latter case, you just only save the section table and section data (the binary content of the executable). If you make sure your source code is not changed and compile and link flags are not changed, the section data should remain the same. If you want to include version numbers or size of code in the checksum, you must include the Optional Header.

    To find the start of Optional Header, follow the procedure:

    1. Read 4-byte signature base address from 0x3c.
    2. Goto the signature offset.
    3. Offset 20 bytes. This is the start of the Optional Header.
    4. You should expect 0x10b here if it is 32-bit exe file or 0x20b if 64-bit.

    To find the start of section table, follow the procedure:

    1. Read 4-byte signature base address from 0x3c.
    2. Goto the signature offset.
    3. offset 16 bytes.
    4. Read 2-byte Optional Header size here.
    5. Goto the Optional Header.
    6. Offset Optional Header size bytes. This is the start of the section table.
    7. You should expect a section name here (like ".text", ".data", etc).

    For complete specification of PE & COFF format, download this: Microsoft PE and COFF Specification.