1 /* 2 * coap_hashkey.h -- definition of hash key type and helper functions 3 * 4 * Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org> 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 * 8 * This file is part of the CoAP library libcoap. Please see README for terms 9 * of use. 10 */ 11 12 /** 13 * @file coap_hashkey.h 14 * @brief definition of hash key type and helper functions 15 */ 16 17 #ifndef COAP_HASHKEY_H_ 18 #define COAP_HASHKEY_H_ 19 20 #include "libcoap.h" 21 #include "uthash.h" 22 #include "str.h" 23 24 typedef unsigned char coap_key_t[4]; 25 26 #ifndef coap_hash 27 /** 28 * Calculates a fast hash over the given string @p s of length @p len and stores 29 * the result into @p h. Depending on the exact implementation, this function 30 * cannot be used as one-way function to check message integrity or simlar. 31 * 32 * @param s The string used for hash calculation. 33 * @param len The length of @p s. 34 * @param h The result buffer to store the calculated hash key. 35 */ 36 void coap_hash_impl(const unsigned char *s, size_t len, coap_key_t h); 37 38 #define coap_hash(String,Length,Result) \ 39 coap_hash_impl((String),(Length),(Result)) 40 41 /* This is used to control the pre-set hash-keys for resources. */ 42 #define COAP_DEFAULT_HASH 43 #else 44 #undef COAP_DEFAULT_HASH 45 #endif /* coap_hash */ 46 47 /** 48 * Calls coap_hash() with given @c coap_string_t object as parameter. 49 * 50 * @param Str Must contain a pointer to a coap string object. 51 * @param H A coap_key_t object to store the result. 52 * 53 * @hideinitializer 54 */ 55 #define coap_str_hash(Str,H) { \ 56 assert(Str); \ 57 memset((H), 0, sizeof(coap_key_t)); \ 58 coap_hash((Str)->s, (Str)->length, (H)); \ 59 } 60 61 #endif /* COAP_HASHKEY_H_ */ 62