/src/core/HashUtils.cpp

http://github.com/imageworks/OpenColorIO · C++ · 71 lines · 32 code · 10 blank · 29 comment · 1 complexity · 88b85c182ff0b579c02226caf69da55e MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <OpenColorIO/OpenColorIO.h>
  28. #include "HashUtils.h"
  29. #include "md5/md5.h"
  30. #include <sstream>
  31. #include <iostream>
  32. OCIO_NAMESPACE_ENTER
  33. {
  34. std::string CacheIDHash(const char * array, int size)
  35. {
  36. md5_state_t state;
  37. md5_byte_t digest[16];
  38. md5_init(&state);
  39. md5_append(&state, (const md5_byte_t *)array, size);
  40. md5_finish(&state, digest);
  41. return GetPrintableHash(digest);
  42. }
  43. std::string GetPrintableHash(const md5_byte_t * digest)
  44. {
  45. static char charmap[] = "0123456789abcdef";
  46. char printableResult[34];
  47. char *ptr = printableResult;
  48. // build a printable string from unprintable chars. first character
  49. // of hashed cache IDs is '$', to later check if it's already been hashed
  50. *ptr++ = '$';
  51. for (int i=0;i<16;++i)
  52. {
  53. *ptr++ = charmap[(digest[i] & 0x0F)];
  54. *ptr++ = charmap[(digest[i] >> 4)];
  55. }
  56. *ptr++ = 0;
  57. return std::string(printableResult);
  58. }
  59. }
  60. OCIO_NAMESPACE_EXIT