hash_joaat.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* $Id$ */
  19. /* Implements Jenkins's one-at-a-time hashing algorithm as presented on
  20. * http://www.burtleburtle.net/bob/hash/doobs.html.
  21. */
  22. #include "php_hash.h"
  23. #include "php_hash_joaat.h"
  24. const php_hash_ops php_hash_joaat_ops = {
  25. (php_hash_init_func_t) PHP_JOAATInit,
  26. (php_hash_update_func_t) PHP_JOAATUpdate,
  27. (php_hash_final_func_t) PHP_JOAATFinal,
  28. (php_hash_copy_func_t) php_hash_copy,
  29. 4,
  30. 4,
  31. sizeof(PHP_JOAAT_CTX)
  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 php_hash_uint32
  65. joaat_buf(void *buf, size_t len, php_hash_uint32 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. */