ghash.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2003, Dominik Reichl <dominik.reichl@t-online.de>, Germany.
  4. All rights reserved.
  5. Distributed under the terms of the GNU General Public License v2.
  6. This software is provided 'as is' with no explicit or implied warranties
  7. in respect of its properties, including, but not limited to, correctness
  8. and/or fitness for purpose.
  9. ---------------------------------------------------------------------------
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "ghash.h"
  15. CGHash::CGHash()
  16. {
  17. Init();
  18. }
  19. CGHash::~CGHash()
  20. {
  21. Init();
  22. }
  23. void CGHash::Init()
  24. {
  25. m_hash3 = 0;
  26. m_hash5 = 0;
  27. }
  28. void CGHash::Update(const unsigned char *pData, unsigned long uSize)
  29. {
  30. unsigned long i = 0;
  31. for(i = 0; i < uSize; i++)
  32. {
  33. m_hash3 = (m_hash3 << 3) + m_hash3 + pData[i];
  34. m_hash5 = (m_hash5 << 5) + m_hash5 + pData[i];
  35. }
  36. }
  37. void CGHash::FinalToStr(char *strOutput, int nHash)
  38. {
  39. // Do NOT destroy internal hash states here!
  40. if(nHash == 3)
  41. {
  42. sprintf(strOutput, "%02X%02X%02X%02X",
  43. (m_hash3 & 0xFF000000) >> 24,
  44. (m_hash3 & 0x00FF0000) >> 16,
  45. (m_hash3 & 0x0000FF00) >> 8,
  46. m_hash3 & 0x000000FF);
  47. }
  48. if(nHash == 5)
  49. {
  50. sprintf(strOutput, "%02X%02X%02X%02X",
  51. (m_hash5 & 0xFF000000) >> 24,
  52. (m_hash5 & 0x00FF0000) >> 16,
  53. (m_hash5 & 0x0000FF00) >> 8,
  54. m_hash5 & 0x000000FF);
  55. }
  56. }