A Checksum Algorithm

Checksum.cpp

#include "stdafx.h"

#include "Checksum.h"

/****************************************************************************
*                                  checksum::add
* Inputs:
*        DWORD d: word to add
* Result: void
* 
* Effect: 
*        Adds the bytes of the DWORD to the checksum
****************************************************************************/

void checksum::add(DWORD value)
{
 union { DWORD value; BYTE bytes[4]; } data;
 data.value = value;
 for(UINT i = 0; i < sizeof(data.bytes); i++)
    add(data.bytes[i]);
} // checksum::add(DWORD)

/****************************************************************************
*                                 checksum::add
* Inputs:
*        WORD value:
* Result: void
* 
* Effect: 
*        Adds the bytes of the WORD value to the checksum
****************************************************************************/

void checksum::add(WORD value)
{
 union { DWORD value; BYTE bytes[2]; } data;
 data.value = value;
 for(UINT i = 0; i < sizeof(data.bytes); i++)
   add(data.bytes[i]);
} // checksum::add(WORD)

/****************************************************************************
*                                 checksum::add
* Inputs:
*        BYTE value:
* Result: void
* 
* Effect: 
*        Adds the byte to the checksum
****************************************************************************/

void checksum::add(BYTE value)
{
 BYTE cipher = (value ^ (r >> 8));
 r = (cipher + r) * c1 + c2;
 sum += cipher;
} // checksum::add(BYTE)

/**************************************************************************** * checksum::add * Inputs: * const CString & s: String to add * Result: void * * Effect: * Adds each character of the string to the checksum ****************************************************************************/ void checksum::add(const CString & s) { for(int i = 0; i < s.GetLength(); i++) add((BYTE)s.GetAt(i)); } // checksum::add(CString) /**************************************************************************** * checksum::add * Inputs: * LPBYTE b: pointer to byte array * UINT length: count * Result: void * * Effect: * Adds the bytes to the checksum ****************************************************************************/ void checksum::add(LPBYTE b, UINT length) { for(UINT i = 0; i < length; i++) add(b[i]); } // checksum::add(LPBYTE, UINT)

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup