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.
 
bool qhashmd5_file (const char *filepath, off_t offset, ssize_t nbytes, void *retbuf)
 Get 128-bit MD5 hash of a file contents.
 
uint32_t qhashfnv1_32 (const void *data, size_t nbytes)
 Get 32-bit FNV1 hash.
 
uint64_t qhashfnv1_64 (const void *data, size_t nbytes)
 Get 64-bit FNV1 hash integer.
 
uint32_t qhashmurmur3_32 (const void *data, size_t nbytes)
 Get 32-bit Murmur3 hash.
 
bool qhashmurmur3_128 (const void *data, size_t nbytes, void *retbuf)
 Get 128-bit Murmur3 hash.
 

Detailed Description

Hash APIs.

Definition in file qhash.c.

Function Documentation

◆ qhashmd5()

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);
char * qhex_encode(const void *bin, size_t size)
Encode data to Hexadecimal digit format.
Definition qencode.c:393
bool qhashmd5(const void *data, size_t nbytes, void *retbuf)
Calculate 128-bit(16-bytes) MD5 hash.
Definition qhash.c:67

Definition at line 67 of file qhash.c.

◆ qhashmd5_file()

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);
bool qhashmd5_file(const char *filepath, off_t offset, ssize_t nbytes, void *retbuf)
Get 128-bit MD5 hash of a file contents.
Definition qhash.c:97

Definition at line 97 of file qhash.c.

◆ qhashfnv1_32()

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);
uint32_t qhashfnv1_32(const void *data, size_t nbytes)
Get 32-bit FNV1 hash.
Definition qhash.c:192
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 192 of file qhash.c.

◆ qhashfnv1_64()

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);
uint64_t qhashfnv1_64(const void *data, size_t nbytes)
Get 64-bit FNV1 hash integer.
Definition qhash.c:223

Definition at line 223 of file qhash.c.

◆ qhashmurmur3_32()

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);
uint32_t qhashmurmur3_32(const void *data, size_t nbytes)
Get 32-bit Murmur3 hash.
Definition qhash.c:263
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 263 of file qhash.c.

◆ qhashmurmur3_128()

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);
bool qhashmurmur3_128(const void *data, size_t nbytes, void *retbuf)
Get 128-bit Murmur3 hash.
Definition qhash.c:335

Definition at line 335 of file qhash.c.