qLibc
qhash.c File Reference

Hash APIs. More...

Go to the source code of this file.

Functions

bool qhashmd5 (const void *data, size_t nbytes, void *retbuf)
 Calculate 128-bit(16-bytes) MD5 hash. More...
 
bool qhashmd5_file (const char *filepath, off_t offset, ssize_t nbytes, void *retbuf)
 Get 128-bit MD5 hash of a file contents. More...
 
uint32_t qhashfnv1_32 (const void *data, size_t nbytes)
 Get 32-bit FNV1 hash. More...
 
uint64_t qhashfnv1_64 (const void *data, size_t nbytes)
 Get 64-bit FNV1 hash integer. More...
 
uint32_t qhashmurmur3_32 (const void *data, size_t nbytes)
 Get 32-bit Murmur3 hash. More...
 
bool qhashmurmur3_128 (const void *data, size_t nbytes, void *retbuf)
 Get 128-bit Murmur3 hash. More...
 

Detailed Description

Hash APIs.

Definition in file qhash.c.

Function Documentation

bool qhashmd5 ( const void *  data,
size_t  nbytes,
void *  retbuf 
)

Calculate 128-bit(16-bytes) MD5 hash.

Parameters
datasource object
nbytessize of data
retbufuser buffer. It must be at leat 16-bytes long.
Returns
true if successful, otherwise false.
// get MD5
unsigned char md5hash[16];
qhashmd5((void*)"hello", 5, md5hash);
// hex encode
char *md5ascii = qhex_encode(md5hash, 16);
printf("Hex encoded MD5: %s\n", md5ascii);
free(md5ascii);

Definition at line 67 of file qhash.c.

bool qhashmd5_file ( const char *  filepath,
off_t  offset,
ssize_t  nbytes,
void *  retbuf 
)

Get 128-bit MD5 hash of a file contents.

Parameters
filepathfile path
offsetstart offset. Set to 0 to digest from beginning of file.
nbytesnumber of bytes to digest. Set to 0 to digest until end of file.
retbufuser buffer. It must be at leat 16-bytes long.
Returns
true if successful, otherwise false.
unsigned char md5hash[16];
qhashmd5_file("/tmp/test.dat", 0, 0, md5hash);

Definition at line 97 of file qhash.c.

uint32_t qhashfnv1_32 ( const void *  data,
size_t  nbytes 
)

Get 32-bit FNV1 hash.

Parameters
datasource data
nbytessize of data
Returns
32-bit unsigned hash value.
uint32_t hashval = qhashfnv1_32((void*)"hello", 5);
Fowler/Noll/Vo hash
The basis of this hash algorithm was taken from an idea sent as reviewer
comments to the IEEE POSIX P1003.2 committee by:
Phong Vo (http://www.research.att.com/info/kpv/)
Glenn Fowler (http://www.research.att.com/~gsf/)
In a subsequent ballot round:
Landon Curt Noll (http://www.isthe.com/chongo/)
improved on their algorithm. Some people tried this hash and found that
it worked rather well. In an EMail message to Landon, they named it the
``Fowler/Noll/Vo'' or FNV hash.
FNV hashes are designed to be fast while maintaining a low collision rate.
The FNV speed allows one to quickly hash lots of data while maintaining
a reasonable collision rate. See:
http://www.isthe.com/chongo/tech/comp/fnv/index.html
for more details as well as other forms of the FNV hash.

Definition at line 187 of file qhash.c.

uint64_t qhashfnv1_64 ( const void *  data,
size_t  nbytes 
)

Get 64-bit FNV1 hash integer.

Parameters
datasource data
nbytessize of data
Returns
64-bit unsigned hash value.
uint64_t fnv64 = qhashfnv1_64((void*)"hello", 5);

Definition at line 218 of file qhash.c.

uint32_t qhashmurmur3_32 ( const void *  data,
size_t  nbytes 
)

Get 32-bit Murmur3 hash.

Parameters
datasource data
nbytessize of data
Returns
32-bit unsigned hash value.
uint32_t hashval = qhashmurmur3_32((void*)"hello", 5);
MurmurHash3 was created by Austin Appleby in 2008. The initial
implementation was published in C++ and placed in the public.
https://sites.google.com/site/murmurhash/
Seungyoung Kim has ported its implementation into C language
in 2012 and published it as a part of qLibc component.

Definition at line 258 of file qhash.c.

bool qhashmurmur3_128 ( const void *  data,
size_t  nbytes,
void *  retbuf 
)

Get 128-bit Murmur3 hash.

Parameters
datasource data
nbytessize of data
retbufuser buffer. It must be at leat 16-bytes long.
Returns
true if successful, otherwise false.
// get 128-bit Murmur3 hash.
unsigned char hash[16];
qhashmurmur3_128((void*)"hello", 5, hash);
// hex encode
char *ascii = qhex_encode(hash, 16);
printf("Hex encoded Murmur3: %s\n", ascii);
free(ascii);

Definition at line 330 of file qhash.c.