chibipub

experimental activitypub node in C
git clone git://jb55.com/chibipub
Log | Files | Refs | README | LICENSE

hash.h (661B)


      1 #pragma once
      2 
      3 #include "blake3/blake3.h"
      4 
      5 static inline uint32_t fnv1a_byte(unsigned char b, uint32_t hash)
      6 {
      7 	return (b ^ hash) * 0x01000193;
      8 }
      9 
     10 
     11 static inline uint32_t fnv1a(unsigned char *bytes, int len)
     12 {
     13 	uint32_t hash = 0x811C9DC5;
     14 	unsigned char *p = bytes;
     15 	while (len--)
     16 		hash = fnv1a_byte(*p++, hash);
     17 
     18 	return hash;
     19 }
     20 
     21 static inline int hashdata32(unsigned char *data, int data_len,
     22 		unsigned char *dest, int dest_len)
     23 {
     24 	if (dest_len < BLAKE3_OUT_LEN)
     25 		return 0;
     26 	blake3_hasher hasher;
     27 	blake3_hasher_init(&hasher);
     28 	blake3_hasher_update(&hasher, data, data_len);
     29 	blake3_hasher_finalize(&hasher, dest, BLAKE3_OUT_LEN);
     30 	return BLAKE3_OUT_LEN;
     31 }