Hash functions and checksums

Generic interface and free CRC-32 implementation for Symbian OS

Hash Functions

“A hash function is a function that converts an input from a (typically) large domain into an output in a (typically) smaller range (the hash value, often a subset of the integers). Hash functions vary in the domain of their inputs and the range of their outputs and in how patterns and similarities of input data affect output data. Hash functions are used in hash tables, cryptography and data processing. A good hash function is one that experiences few hash collisions in the expected domain of values it will have to deal with; that is, it would be possible to uniquely identify most of these values using this hash.”

“In practice, for most applications other than error correction, cryptographic hashes serve very nicely. The [?MD5] and [?SHA-1] algorithms are two popular but compromised algorithms for generating cryptographic hash functions; the [?SHA-2] algorithm has no known compromises.”

From Wikipedia, the free encyclopedia.

Generic Interface

An interface for hash functions has to provide at least three operations:
-  Add a data block
-  Calculate the hash value
-  Reset the internal state to be ready for a new calculation

This leads to the subsequent Symbian interface:

class MHashFunc {
public:
 /* Appends data. Can be called multiple times. */
 virtual void AppendToHashL(const TDesC8& aData) = 0;
 
 /** Returns hash value. AppendToHash has to be called first.
   * The Caller takes the ownership of the hash buffer.
 */
 virtual HBufC8* GetHashAndResetL() = 0;


 /** Resets internal buffers. */
 virtual void ResetHash() = 0;


public:
 virtual ~MHashFunc();
};

inline MHashFunc::~MHashFunc(){};

Usage

A class which implements MHashFunc can used as follows:

TBuf8<256> data1(_L8("Example data..."));
TBuf8<256> data2(_L8("Some more data..."));

MHashFunc* hf = ConcreteHashFunction::NewLC();

/* Calculate hash of data1 */
hf->ResetHash();
hf->AppendToHashL(data1);
HBufC8* hash_of_data1 = hf->GetHashAndResetL();
delete hash_of_data1;

/* Calculate hash of data1 and data2 */
hf->AppendToHashL(data1);
hf->AppendToHashL(data2);
HBufC8* hash_of_data1_and_data2 = hf->GetHashAndResetL();
delete hash_of_data1_and_data2;

CleanupStack::PopAndDestroy(hf);

Hash values can be easily compared using the Compare() operation of TDesC8.

CRC-32

CRC-32 is a hash function which is used to calculate a 32 bit [?checksum]. It can be utilized to detect errors in transmission or storage of data. My implementation of CRC-32 is based on the Painless Guide to CRC Error Detection Algorithms by Ross N. Williams. Needless to say my CRC class realizes the MHashFunc interface.

crc-32_symbianos_2005-07-04.zip
crc-32_symbianos_2005-07-04.zip



Have fun,

Philipp

Software Engineer at bit-side [1]

[1] bit-side is a world wide operating creative engineering company providing mobile enterprise solutions, innovative mobile media applications and mobile games to clients from the Americas to Asia.