Guest User

Untitled

a guest
Jan 14th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <sys/types.h>
  3.  
  4. /* antirez hash function one (AHF1)
  5. *
  6. * FIXME: make it byte ordering agnostic.
  7. *
  8. * TEST VECTOR:
  9. * "123456789" => 2934016909
  10. * "Message Digest" => 1454815842
  11. */
  12. uint32_t ahf1(unsigned char *s, size_t len) {
  13. uint32_t hash;
  14. unsigned char *h = (unsigned char*) &hash;
  15. int idx = 0;
  16.  
  17. h[0] = 0x8D;
  18. h[1] = 0xCA;
  19. h[2] = 0x2E;
  20. h[3] = 0x35;
  21. while(len) {
  22. int target = ((*s) ^ h[idx]) & 3;
  23. int rot = *s & 0x1F;
  24.  
  25. h[target] ^= *s; /* step 1: XOR with target byte. */
  26. hash = (hash << rot) | (hash >> (32-rot)); /* step 2: Circular shifting. */
  27. len--;
  28. s++;
  29. idx = (idx+1) & 3;
  30. }
  31. return hash;
  32. }
  33.  
  34.  
  35. #include <stdio.h>
  36. #include <string.h>
  37.  
  38. int main(void) {
  39. printf("%lu\n", (unsigned long) ahf1((unsigned char*)"123456789",9));
  40. return 0;
  41. }
Add Comment
Please, Sign In to add comment