hash_joaat.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Martin Jansen <mj@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /* Implements Jenkins's one-at-a-time hashing algorithm as presented on
  17. * http://www.burtleburtle.net/bob/hash/doobs.html.
  18. */
  19. #include "php_hash.h"
  20. #include "php_hash_joaat.h"
  21. const php_hash_ops php_hash_joaat_ops = {
  22. "joaat",
  23. (php_hash_init_func_t) PHP_JOAATInit,
  24. (php_hash_update_func_t) PHP_JOAATUpdate,
  25. (php_hash_final_func_t) PHP_JOAATFinal,
  26. php_hash_copy,
  27. php_hash_serialize,
  28. php_hash_unserialize,
  29. PHP_JOAAT_SPEC,
  30. 4,
  31. 4,
  32. sizeof(PHP_JOAAT_CTX),
  33. 0
  34. };
  35. PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  36. {
  37. context->state = 0;
  38. }
  39. PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen)
  40. {
  41. context->state = joaat_buf((void *)input, inputLen, context->state);
  42. }
  43. PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context)
  44. {
  45. uint32_t hval = context->state;
  46. hval += (hval << 3);
  47. hval ^= (hval >> 11);
  48. hval += (hval << 15);
  49. #ifdef WORDS_BIGENDIAN
  50. memcpy(digest, &hval, 4);
  51. #else
  52. int i = 0;
  53. unsigned char *c = (unsigned char *) &hval;
  54. for (i = 0; i < 4; i++) {
  55. digest[i] = c[3 - i];
  56. }
  57. #endif
  58. context->state = 0;
  59. }
  60. /*
  61. * joaat_buf - perform a Jenkins's one-at-a-time hash on a buffer
  62. *
  63. * input:
  64. * buf - start of buffer to hash
  65. * len - length of buffer in octets
  66. *
  67. * returns:
  68. * 32 bit hash as a static hash type
  69. */
  70. static uint32_t
  71. joaat_buf(void *buf, size_t len, uint32_t hval)
  72. {
  73. size_t i;
  74. unsigned char *input = (unsigned char *)buf;
  75. for (i = 0; i < len; i++) {
  76. hval += input[i];
  77. hval += (hval << 10);
  78. hval ^= (hval >> 6);
  79. }
  80. return hval;
  81. }