hash_joaat.c 2.8 KB

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