Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Hash.h File Reference
#include <cstddef>
#include <cstdint>
Include dependency graph for cs_Hash.h:

Go to the source code of this file.

Functions

uint32_t Fletcher (const uint8_t *const data, const size_t len, uint32_t previousFletcherHash=0)
 Computes a Fletcher32 hash for the given data, interpreting it as a stream of little endian uint16_t. More...
 
uint16_t Djb2 (const uint8_t *data, const uint16_t size)
 Calculates a djb2 hash of given data. More...
 

Function Documentation

◆ Djb2()

uint16_t Djb2 ( const uint8_t *  data,
const uint16_t  size 
)
inline

Calculates a djb2 hash of given data.

See http://www.cse.yorku.ca/~oz/hash.html for implementation details

Parameters
[in]Pointerto the data.
[in]Sizeof the data.
Return values
Thehash.

◆ Fletcher()

uint32_t Fletcher ( const uint8_t *const  data,
const size_t  len,
uint32_t  previousFletcherHash = 0 
)

Computes a Fletcher32 hash for the given data, interpreting it as a stream of little endian uint16_t.

Note: previousFletcherHash = 0 indicates a fresh new hash

Note: data will be padded with 0x00 if len%2 != 0, so if an array uint8_t dat[]; has last byte equal to 0x00 and len is positive, even then: Fletcher(dat,sizeof(dat)) == Fletcher(dat, sizeof(dat)-1)

As the Fletcher32 hash is effectively a uint16_t sized block stream hash, the computation of a hash of large amounts of data can be split. *

Warning: this only holds as long as the lengths of intermediate data chunks are a multiple of 2, each call to Fletcher(...) will round the len parameter up to a multiple of 2 and padd the data with 0x00 if achieve that.

E.g.

uint8_t part0[] = { data }; size_t len0; // must be 0 % 2 uint8_t part1[] = { data }; size_t len1; // must be 0 % 2 uint8_t part2[] = { data }; size_t len2; // may be 1 % 2

uint32_t fletch; fletch = Fletcher(part0,len0); fletch = Fletcher(part1,len1,fletch); fletch = Fletcher(part2,len2,fletch);