Click here to Skip to main content
11,437,002 members (65,952 online)
Click here to Skip to main content
Add your own
alternative version

An Analysis of the Windows PE Checksum Algorithm

, 7 Mar 2008 CPOL
An Analysis Demonstrating the PE Checksum is an Additive Checksum based on RFC 1071
crc32.zip
CRC32.exe
crc32source.zip
CRC32
CRC32
CRC32.suo
notepadaltered.zip
NOTEPAD.EXE
notepadoriginal.zip
NOTEPAD.EXE
pechecksum.zip
PEChecksum.exe
pechecksumsource.zip
PE Checksum
PEChecksum
res
Sample.ico
PEChecksum.suo
Sample.suo
// CRC32.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "crc.h"
#include "hex.h"

void Usage( void );

int _tmain(int argc, _TCHAR* argv[])
{
    if( argc != 2 )
    {
        Usage();
        return -1;
    }

    HANDLE hFile = INVALID_HANDLE_VALUE;
    HANDLE hFileMapping = NULL;
    PVOID pBaseAddress = NULL;
    DWORD dwFileLength = 0;

    try
    {
        /////////////////////////////////////////////////////////////
        hFile = CreateFile( argv[ 1 ], GENERIC_READ, FILE_SHARE_READ,
            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
        if ( INVALID_HANDLE_VALUE == hFile ||
            NULL == hFile) { throw GetLastError(); }

        /////////////////////////////////////////////////////////////
        hFileMapping = CreateFileMapping(hFile, NULL,
            PAGE_READONLY, 0, 0, NULL);
        if ( NULL == hFileMapping )
        {
            throw GetLastError();
        }

        /////////////////////////////////////////////////////////////
        pBaseAddress = MapViewOfFile( hFileMapping,
            FILE_MAP_READ, 0, 0, 0);
        if ( NULL == pBaseAddress )
        {
            throw GetLastError();
        }

        /////////////////////////////////////////////////////////////
        DWORD dwSize = 0;
        LARGE_INTEGER liSize = { 0, 0 };
        if( TRUE == GetFileSizeEx( hFile, &liSize ) )
        {
            dwSize = liSize.LowPart;
        }

        CryptoPP::CRC32 crc;
        byte digest[ CryptoPP::CRC32::DIGESTSIZE ] = { 0 };
        crc.CalculateDigest( digest, (byte*)pBaseAddress, dwSize );
        
        std::string sink;
        CryptoPP::HexEncoder encoder( new CryptoPP::StringSink( sink ), true );
        encoder.Put( digest, CryptoPP::CRC32::DIGESTSIZE );
        encoder.MessageEnd();

        std::cout << sink << std::endl;

    }
    catch(...)
    {
        std::wcerr << "Error computing CRC-32" << std::endl;
    }

    UnmapViewOfFile( hFileMapping );
    CloseHandle( hFile );

    return 0;
}

void Usage( void )
{
    std::tcout << _T("crc32.exe <filename>") << std::endl;
    std::tcout << _T(" filename to have CRC-32 computed.") << std::endl;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jeffrey Walton
Systems / Hardware Administrator
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 8 Mar 2008
Article Copyright 2007 by Jeffrey Walton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid