hl_md5wrapper.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_md5wrapper.cpp
  31. * @brief This file contains the implementation of the
  32. * md5wrapper class
  33. * @date Mo 17 Sep 2007
  34. */
  35. //----------------------------------------------------------------------
  36. //STL includes
  37. #include <string>
  38. #include <fstream>
  39. #include <iostream>
  40. #include <sstream>
  41. //----------------------------------------------------------------------
  42. //hashlib++ includes
  43. #include "hl_md5wrapper.h"
  44. //----------------------------------------------------------------------
  45. //private member functions
  46. /**
  47. * @brief This method ends the hash process
  48. * and returns the hash as string.
  49. *
  50. * @return the hash as std::string
  51. */
  52. std::string md5wrapper::hashIt(void)
  53. {
  54. //create the hash
  55. unsigned char buff[16] = "";
  56. md5->MD5Final((unsigned char*)buff,&ctx);
  57. //converte the hash to a string and return it
  58. return convToString(buff);
  59. }
  60. /**
  61. * @brief This internal member-function
  62. * convertes the hash-data to a
  63. * std::string (HEX).
  64. *
  65. * @param data The hash-data to covert into HEX
  66. * @return the converted data as std::string
  67. */
  68. std::string md5wrapper::convToString(unsigned char *data)
  69. {
  70. /*
  71. * using a ostringstream to convert the hash in a
  72. * hex string
  73. */
  74. std::ostringstream os;
  75. for(int i=0; i<16; ++i)
  76. {
  77. /*
  78. * set the width to 2
  79. */
  80. os.width(2);
  81. /*
  82. * fill with 0
  83. */
  84. os.fill('0');
  85. /*
  86. * conv to hex
  87. */
  88. os << std::hex << static_cast<unsigned int>(data[i]);
  89. }
  90. /*
  91. * return as std::string
  92. */
  93. return os.str();
  94. }
  95. /**
  96. * @brief This method adds the given data to the
  97. * current hash context.
  98. *
  99. * @param data The data to add to the current context
  100. * @param len The length of the data to add
  101. */
  102. void md5wrapper::updateContext(unsigned char *data, unsigned int len)
  103. {
  104. //update
  105. md5->MD5Update(&ctx, data, len);
  106. }
  107. /**
  108. * @brief This method resets the current hash context.
  109. * In other words: It starts a new hash process.
  110. */
  111. void md5wrapper::resetContext(void)
  112. {
  113. //init md5
  114. md5->MD5Init(&ctx);
  115. }
  116. /**
  117. * @brief This method should return the hash of the
  118. * test-string "The quick brown fox jumps over the lazy
  119. * dog"
  120. */
  121. std::string md5wrapper::getTestHash(void)
  122. {
  123. return "9e107d9d372bb6826bd81d3542a419d6";
  124. }
  125. //----------------------------------------------------------------------
  126. //public member functions
  127. /**
  128. * @brief default constructor
  129. */
  130. md5wrapper::md5wrapper()
  131. {
  132. md5 = new MD5();
  133. }
  134. /**
  135. * @brief default destructor
  136. */
  137. md5wrapper::~md5wrapper()
  138. {
  139. delete md5;
  140. }
  141. //----------------------------------------------------------------------
  142. //EOF