README 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #=====================#
  2. # Fowler/Noll/Vo hash #
  3. #=====================#
  4. The basis of this hash algorithm was taken from an idea sent
  5. as reviewer comments to the IEEE POSIX P1003.2 committee by:
  6. Phong Vo (http://www.research.att.com/info/kpv)
  7. Glenn Fowler (http://www.research.att.com/~gsf/)
  8. In a subsequent ballot round:
  9. Landon Curt Noll (http://www.isthe.com/chongo)
  10. improved on their algorithm. Some people tried this hash
  11. and found that it worked rather well. In an EMail message
  12. to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
  13. FNV hashes are designed to be fast while maintaining a low
  14. collision rate. The FNV speed allows one to quickly hash lots
  15. of data while maintaining a reasonable collision rate. See:
  16. http://www.isthe.com/chongo/tech/comp/fnv/index.html
  17. for more details as well as other forms of the FNV hash.
  18. Comments, questions, bug fixes and suggestions welcome at
  19. the address given in the above URL.
  20. #==================#
  21. # FNV hash utility #
  22. #==================#
  23. Two hash utilities (32 bit and 64 bit) are provided:
  24. fnv032 [-b bcnt] [-m] [-s arg] [-t code] [-v] [arg ...]
  25. fnv132 [-b bcnt] [-m] [-s arg] [-t code] [-v] [arg ...]
  26. fnv1a32 [-b bcnt] [-m] [-s arg] [-t code] [-v] [arg ...]
  27. fnv064 [-b bcnt] [-m] [-s arg] [-t code] [-v] [arg ...]
  28. fnv164 [-b bcnt] [-m] [-s arg] [-t code] [-v] [arg ...]
  29. fnv1a64 [-b bcnt] [-m] [-s arg] [-t code] [-v] [arg ...]
  30. -b bcnt mask off all but the lower bcnt bits (default: 32)
  31. -m multiple hashes, one per line for each arg
  32. -s hash arg as a string (ignoring terminating NUL bytes)
  33. -t code 0 ==> generate test vectors, 1 ==> test FNV hash
  34. -v verbose mode, print arg after hash (implies -m)
  35. arg string (if -s was given) or filename (default stdin)
  36. The fnv032, fnv064 implement the historic FNV-0 hash.
  37. The fnv132, fnv164 implement the recommended FNV-1 hash.
  38. The fnv1a32, fnv1a64 implement the recommended FNV-1a hash.
  39. This is the original historic FNV algorithm with a 0 offset basis.
  40. It is recommended that FNV-1, with a non-0 offset basis be used instead.
  41. To test FNV hashes, try:
  42. fnv032 -t 1 -v
  43. fnv132 -t 1 -v
  44. fnv1a32 -t 1 -v
  45. fnv064 -t 1 -v
  46. fnv164 -t 1 -v
  47. fnv1a64 -t 1 -v
  48. If you are compiling, try:
  49. make check
  50. #==================#
  51. # FNV hash library #
  52. #==================#
  53. The libfnv.a library implements both a 32 bit and a 64 bit FNV hash
  54. on collections of bytes, a NUL terminated strings or on an open file
  55. descriptor.
  56. Here is the 32 bit FNV 1 hash:
  57. Fnv32_t fnv_32_buf(void *buf, int len, Fnv32_t hval); /* byte buf */
  58. Fnv32_t fnv_32_str(char *string, Fnv32_t hval); /* string */
  59. Here is the 32 bit FNV 1a hash:
  60. Fnv32_t fnv_32a_buf(void *buf, int len, Fnv32_t hval); /* byte buf */
  61. Fnv32_t fnv_32a_str(char *string, Fnv32_t hval); /* string */
  62. Here is the 64 bit FNV 1 hash:
  63. Fnv64_t fnv_64_buf(void *buf, int len, Fnv64_t hval); /* byte buf */
  64. Fnv64_t fnv_64_str(char *string, Fnv64_t hval); /* string */
  65. Here is the 64 bit FNV 1a hash:
  66. Fnv64_t fnv_64a_buf(void *buf, int len, Fnv64_t hval); /* byte buf */
  67. Fnv64_t fnv_64a_str(char *string, Fnv64_t hval); /* string */
  68. On the first call to a hash function, one must supply the initial basis
  69. that is appropriate for the hash in question:
  70. FNV-0: (not recommended)
  71. FNV0_32_INIT /* 32 bit FNV-0 initial basis */
  72. FNV0_64_INIT /* 64 bit FNV-0 initial basis */
  73. FNV-1:
  74. FNV1_32_INIT /* 32 bit FNV-1 initial basis */
  75. FNV1_64_INIT /* 64 bit FNV-1 initial basis */
  76. FNV-1a:
  77. FNV1A_32_INIT /* 32 bit FNV-1a initial basis */
  78. FNV1A_64_INIT /* 64 bit FNV-1a initial basis */
  79. For example to perform a 64 bit FNV-1 hash:
  80. #include "fnv.h"
  81. Fnv64_t hash_val;
  82. hash_val = fnv_64_str("a string", FNV1_64_INIT);
  83. hash_val = fnv_64_str("more string", hash_val);
  84. produces the same final hash value as:
  85. hash_val = fnv_64_str("a stringmore string", FNV1_64_INIT);
  86. NOTE: If one used 'FNV0_64_INIT' instead of 'FNV1_64_INIT' one would get the
  87. historic FNV-0 hash instead recommended FNV-1 hash.
  88. To perform a 32 bit FNV-1 hash:
  89. #include "fnv.h"
  90. Fnv32_t hash_val;
  91. hash_val = fnv_32_buf(buf, length_of_buf, FNV1_32_INIT);
  92. hash_val = fnv_32_str("more data", hash_val);
  93. To perform a 64 bit FNV-1a hash:
  94. #include "fnv.h"
  95. Fnv64_t hash_val;
  96. hash_val = fnv_64a_buf(buf, length_of_buf, FNV1_64_INIT);
  97. hash_val = fnv_64a_str("more data", hash_val);
  98. =-=
  99. chongo <Landon Curt Noll> /\oo/\
  100. http://www.isthe.com/chongo
  101. Share and Enjoy!