1 /* 2 * Copyright (c) 1991, Daniel J. Bernstein 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /* 8 * This software is in the PD as of approximately 2007. The story 9 * behind it (and other DJB software) is quite amazing. Thanks Dan!! 10 * 11 * Many distributors have relicensed it under e.g. BSD, MIT, and others. 12 * 13 * It is not clear what the original license name is or how to declare it 14 * using SPDX terms, since it explicitly has no license. I think it makes 15 * sense to use the default Zephyr licensing. 16 * 17 * Note: this is not a cryptographically strong hash algorithm. 18 * 19 * For details, please see 20 * https://cr.yp.to/rights.html 21 * https://cr.yp.to/distributors.html 22 * http://thedjbway.b0llix.net/license_free.html 23 * 24 * https://groups.google.com/g/comp.lang.c/c/lSKWXiuNOAk 25 * https://theartincode.stanis.me/008-djb2/ 26 * http://www.cse.yorku.ca/~oz/hash.html 27 * https://bit.ly/3IxUVvC 28 */ 29 30 #include <stddef.h> 31 #include <stdint.h> 32 33 #include <zephyr/sys/hash_function.h> 34 sys_hash32_djb2(const void * str,size_t n)35uint32_t sys_hash32_djb2(const void *str, size_t n) 36 { 37 uint32_t hash; 38 const uint8_t *d; 39 40 /* The number 5381 is the initializer for the djb2 hash */ 41 for (hash = 5381, d = str; n > 0; --n, ++d) { 42 /* The djb2 hash multiplier is 33 (i.e. 2^5 + 1) */ 43 hash = (hash << 5) + hash; 44 hash ^= *d; 45 } 46 47 return hash; 48 } 49