A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

Checksums & hashes

A set of algorithms for producing checksums and hashes from data.

The hashes defined are:-

Checksum            checksum.h
BSD Checksum        bsd_checksum.h
XOR Checksum        xor_checksum.h
XOR/Rotate Checksum xor_rotate_checksum.h

CRC8 CCITT          crc8_ccitt.h
CRC16               crc16.h
CRC16 CCITT         crc16_ccitt.h
CRC16 Kermit        crc_kermit.h
CRC32               crc32.h
CRC32-C             crc32_c.h     (Castagnoli)
CRC64 ECMA          crc64_ecma.h

FNV-1 32 bit        fnv1.h
FNV-1 64 bit        fnv1.h
FNV-1a 32 bit       fnv1.h
FNV-1a 64 bit       fnv1.h

Jenkins             jenkins.h

Pearson             pearson.h

Murmur3             murmur3.h (32 & 64 bit)

The library also includes etl::hash which is a reverse engineered version of std::hash in hash.h

All hashing templates have the same API, apart from the members returning the hash value, which return the appropriate

A hash that requires finalising before the value is read should do it in the conversion operator and value() member
functions. Trying to add values after finalisation should result in an etl::hash_finalised error being emitted.
Construction or reset should clear any finalisation flag.

Hash API

All hashing algorithms supply the following API.

Default constructor
Initialises the hash value to its appropriate starting value.

Constructor from a range. (Any iterator type)
Creates a hash from the supplied range.

void reset();
Resets the hash to the initial condition.

void add(uint8_t value);
Adds a uint8_t to the hash.

template <typename TIterator>
void add(TIterator begin, Titerator end);
Adds a range of values to the hash.
If the type pointed to must be default castable to uint8_t.

value_type value() const;
Returns the hash value.
The hash will be finalised if required.

operator value_type() const;
Returns the hash value.
The hash will be finalised if required.

Creating a new frame check type.

The frame_check_sequence class may be customised with new policies that contain the expected typedef and  member


struct special_16
  typedef uint16_t value_type;

  inline uint16_t initial() const
    return 0xFFFF; // The initial value.

  inline uint16_t add(T sum, uint8_t value) const
    return etl::rotate_left(sum, 3) ^ value; // How to add each value.

  inline uint16_t final(T sum) const
    return ~sum; // How to read the final value.

The class would be declared as follows.

/// Special checksum
class special_checksum : public etl::frame_check_sequence<special_16>

  /// Default constructor.

  /// Constructor from range.
  /// \param begin Start of the range.
  /// \param end   End of the range.
  template<typename TIterator>
  special_checksum(TIterator begin, const TIterator end)
    add(begin, end);