hl_sha512wrapper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * hashlib++ - a simple hash library for C++
  3. *
  4. * Copyright (c) 2007-2010 Benjamin Grüdelbach
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1) Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2) Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. //----------------------------------------------------------------------
  29. /**
  30. * @file hl_sha512wrapper.cpp
  31. * @brief This file contains the implementation of the sha512wrapper
  32. * class.
  33. * @date Mo 12 Nov 2007
  34. */
  35. //----------------------------------------------------------------------
  36. //hashlib++ includes
  37. #include "hl_sha512wrapper.h"
  38. //----------------------------------------------------------------------
  39. //STL includes
  40. #include <string>
  41. //----------------------------------------------------------------------
  42. //private memberfunctions
  43. /**
  44. * @brief This method ends the hash process
  45. * and returns the hash as string.
  46. *
  47. * @return a hash as std::string
  48. */
  49. std::string sha512wrapper::hashIt(void)
  50. {
  51. sha2_byte buff[SHA512_DIGEST_STRING_LENGTH];
  52. sha512->SHA512_End(&context,(char*)buff);
  53. return convToString(buff);
  54. }
  55. /**
  56. * @brief This internal member-function
  57. * convertes the hash-data to a
  58. * std::string (HEX).
  59. *
  60. * @param data The hash-data to covert into HEX
  61. * @return the converted data as std::string
  62. */
  63. std::string sha512wrapper::convToString(unsigned char *data)
  64. {
  65. /*
  66. * we can just copy data to a string, because
  67. * the transforming to hash is already done
  68. * within the sha512 implementation
  69. */
  70. return std::string((const char*)data);
  71. }
  72. /**
  73. * @brief This method adds the given data to the
  74. * current hash context
  75. *
  76. * @param data The data to add to the current context
  77. * @param len The length of the data to add
  78. */
  79. void sha512wrapper::updateContext(unsigned char *data, unsigned int len)
  80. {
  81. this->sha512->SHA512_Update(&context,data,len);
  82. }
  83. /**
  84. * @brief This method resets the current hash context.
  85. * In other words: It starts a new hash process.
  86. */
  87. void sha512wrapper::resetContext(void)
  88. {
  89. sha512->SHA512_Init(&context);
  90. }
  91. /**
  92. * @brief This method should return the hash of the
  93. * test-string "The quick brown fox jumps over the lazy
  94. * dog"
  95. */
  96. std::string sha512wrapper::getTestHash(void)
  97. {
  98. return "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6";
  99. }
  100. //----------------------------------------------------------------------
  101. //public memberfunctions
  102. /**
  103. * @brief default constructor
  104. */
  105. sha512wrapper::sha512wrapper()
  106. {
  107. this->sha512 = new SHA2ext();
  108. }
  109. /**
  110. * @brief default destructor
  111. */
  112. sha512wrapper::~sha512wrapper()
  113. {
  114. delete sha512;
  115. }