hash.c 869 B

123456789101112131415161718192021222324252627282930
  1. #include "hash.h"
  2. #include "c_string.h"
  3. /* Caution: When changing this, update COAP_DEFAULT_WKC_HASHKEY
  4. * accordingly (see int coap_hash_path());
  5. */
  6. void coap_hash(const unsigned char *s, unsigned int len, coap_key_t h) {
  7. size_t j;
  8. while (len--) {
  9. j = sizeof(coap_key_t)-1;
  10. while (j) {
  11. h[j] = ((h[j] << 7) | (h[j-1] >> 1)) + h[j];
  12. --j;
  13. }
  14. h[0] = (h[0] << 7) + h[0] + *s++;
  15. }
  16. }
  17. void coap_transaction_id(const uint32_t ip, const uint32_t port, const coap_packet_t *pkt, coap_tid_t *id) {
  18. coap_key_t h;
  19. c_memset(h, 0, sizeof(coap_key_t));
  20. /* Compare the transport address. */
  21. coap_hash((const unsigned char *)&(port), sizeof(port), h);
  22. coap_hash((const unsigned char *)&(ip), sizeof(ip), h);
  23. coap_hash((const unsigned char *)(pkt->hdr.id), sizeof(pkt->hdr.id), h);
  24. *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
  25. }