FdkBaseLib 2.1.1
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
fdk::Hash Class Reference

#include <Hash.h>

Public Member Functions

 Hash (const Hash &h)
 
 Hash (const uint64_t &h)
 
const Hashoperator= (const Hash &h)
 
void reset ()
 Reset the hash to its initial value, which is not zero.
 
void reset (const uint64_t &v)
 
bool isDefault () const
 
void newValue ()
 Changes the value to the next hash increment.
 
bool operator== (const Hash &h) const
 
bool operator!= (const Hash &h) const
 
bool operator< (const Hash &h) const
 
uint64_t value () const
 
uint64_t getHash () const
 
std::string asString () const
 Return a hex-formatted string version of the hash value.
 
void append (const void *data, size_t length)
 
void append (const char *)
 
void append (const std::string &)
 
void append (bool)
 
void append (float)
 
void append (double)
 
void append (int32_t)
 
void append (uint32_t)
 
void append (uint64_t)
 
void append (const uint64_t &)
 Add another hash to this hash.
 
void append (const Hash &)
 
void appendIfNotDefault (const uint64_t &)
 If other hash is not at default value append it to this hash.
 
void appendIfNotDefault (const Hash &)
 

Static Public Member Functions

static Hash combineIfNotDefault (const Hash &a, const Hash &b)
 

Static Public Attributes

static constexpr const uint64_t DefaultValue = ~0x0ULL
 Default value for hash (0xffffffffffffffff)
 

Protected Attributes

uint64_t _value
 

Detailed Description

Generates and maintains a hash sum of data. If the hashes of two pieces of data are equal it is extremely likely the two pieces of data are equal.

To generate a hash, make one of these objects then call append() with each piece of data you want to include. You can then compare it with other hashes, or extract the numerical value with value().

The algorithim used is a 64-bit CRC checksum, using the CRC-64-ECMA-182 polynominal (lsb representation 0x42F0E1EBA9EA3693).

Some people worry that this is not a cryptographcally sound checksum, but that is not needed when the data is not being produced by something that is actively trying to subvert the checksum. For random data it is equally strong.

Member Function Documentation

◆ append() [1/3]

void fdk::Hash::append ( const void *  data,
size_t  length 
)

Add the n bytes starting at pointer to the hash.

Warning: this call is for appending blocks of data together. A zero-length block will not change the hash, and the hash is the same no matter how the same block is split into calls to this. This will cause the hash to fail if you actually care about this, for instance if you have several variable-sized arrays of data. Swapping a zero-length and non-zero-length one, or moving data from the end of one to the start of the next, will not change the hash. The solution is to append the length as well.

◆ append() [2/3]

void fdk::Hash::append ( const char *  )

Add a null-terminated string or a null pointer to the hash. The zero-length string and the null pointer hash to different values, and those are different than not calling this.

◆ append() [3/3]

void fdk::Hash::append ( bool  )

Add a bool value to the hash. This is different than any other possible append (as it only adds 1 bit to the hash rather than a multiple of 8) and thus it is also useful for marking where zero-length arrays are.

◆ combineIfNotDefault()

Hash fdk::Hash::combineIfNotDefault ( const Hash a,
const Hash b 
)
inlinestatic

If a is not default it's copied then b is appended if it's not default, otherwise if a is default then b is copied and a is ignored.

References appendIfNotDefault().



©2024 The Foundry Visionmongers, Ltd. All Rights Reserved.
www.foundry.com