hl_md5.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * The hashlib++ MD5 implementation is derivative from the sourcecode
  31. * published in RFC 1321
  32. *
  33. * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  34. * rights reserved.
  35. *
  36. * License to copy and use this software is granted provided that it
  37. * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  38. * Algorithm" in all material mentioning or referencing this software
  39. * or this function.
  40. *
  41. * License is also granted to make and use derivative works provided
  42. * that such works are identified as "derived from the RSA Data
  43. * Security, Inc. MD5 Message-Digest Algorithm" in all material
  44. * mentioning or referencing the derived work.
  45. *
  46. * RSA Data Security, Inc. makes no representations concerning either
  47. * the merchantability of this software or the suitability of this
  48. * software for any particular purpose. It is provided "as is"
  49. * without express or implied warranty of any kind.
  50. *
  51. * These notices must be retained in any copies of any part of this
  52. * documentation and/or software.
  53. */
  54. //----------------------------------------------------------------------
  55. /**
  56. * @file hl_md5.h
  57. * @brief This file contains the declaration of the MD5 class
  58. * @date Mo 17 Sep 2007
  59. */
  60. //----------------------------------------------------------------------
  61. //include protection
  62. #ifndef MD5_H
  63. #define MD5_H
  64. //----------------------------------------------------------------------
  65. //STL includes
  66. #include <string>
  67. //----------------------------------------------------------------------
  68. //hl includes
  69. #include "hl_types.h"
  70. //----------------------------------------------------------------------
  71. //typedefs
  72. typedef hl_uint8 *POINTER;
  73. /**
  74. * @brief this struct represents a MD5-hash context.
  75. */
  76. typedef struct
  77. {
  78. /** state (ABCD) */
  79. unsigned long int state[4];
  80. /** number of bits, modulo 2^64 (lsb first) */
  81. unsigned long int count[2];
  82. /** input buffer */
  83. unsigned char buffer[64];
  84. } HL_MD5_CTX;
  85. //----------------------------------------------------------------------
  86. /**
  87. * @brief This class represents the implementation of
  88. * the md5 message digest algorithm.
  89. *
  90. * Basically the class provides three public member-functions
  91. * to create a hash: MD5Init(), MD5Update() and MD5Final().
  92. * If you want to create a hash based on a string or file quickly
  93. * you should use the md5wrapper class instead of MD5.
  94. */
  95. class MD5
  96. {
  97. private:
  98. /**
  99. * @brief Basic transformation. Transforms state based on block.
  100. * @param state state to transform
  101. * @param block block to transform
  102. */
  103. void MD5Transform (unsigned long int state[4], unsigned char block[64]);
  104. /**
  105. * @brief Encodes input data
  106. * @param output Encoded data as OUT parameter
  107. * @param input Input data
  108. * @param len The length of the input assuming it is a
  109. * multiple of 4
  110. */
  111. void Encode (unsigned char* output,
  112. unsigned long int *input,
  113. unsigned int len);
  114. /**
  115. * @brief Decodes input data into output
  116. * @param output Decoded data as OUT parameter
  117. * @param input Input data
  118. * @param len The length of the input assuming it is a
  119. * multiple of 4
  120. */
  121. void Decode (unsigned long int *output,
  122. unsigned char *input,
  123. unsigned int len);
  124. /**
  125. * @brief internal memory management
  126. * @param output OUT parameter where POINTER is an unsigned
  127. * char*
  128. * @param input Data to copy where POINTER is a unsigned char*
  129. * @param len The length of the data
  130. */
  131. void MD5_memcpy (POINTER output, POINTER input, unsigned int len);
  132. /**
  133. * @brief internal memory management
  134. * @param output OUT parameter where POINTER is an unsigned
  135. * char*
  136. * @param value Value to fill the memory with
  137. * @param len The length of the data
  138. *
  139. */
  140. void MD5_memset (POINTER output, int value, unsigned int len);
  141. public:
  142. /**
  143. * @brief Initialization begins an operation,
  144. * writing a new context
  145. * @param context The HL_MD5_CTX context to initialize
  146. */
  147. void MD5Init (HL_MD5_CTX* context);
  148. /**
  149. * @brief Block update operation. Continues an md5
  150. * message-digest operation, processing another
  151. * message block, and updating the context.
  152. * @param context The HL_MD5_CTX context to update
  153. * @param input The data to write into the context
  154. * @param inputLen The length of the input data
  155. */
  156. void MD5Update (HL_MD5_CTX* context,
  157. unsigned char *input,
  158. unsigned int inputLen);
  159. /**
  160. * @brief Finalization ends the md5 message-digest
  161. * operation, writing the the message digest and
  162. * zeroizing the context.
  163. * @param digest This is an OUT parameter which contains
  164. * the created hash after the method returns
  165. * @param context The context to finalize
  166. */
  167. void MD5Final (unsigned char digest[16], HL_MD5_CTX* context);
  168. /**
  169. * @brief default constructor
  170. */
  171. MD5(){};
  172. };
  173. //----------------------------------------------------------------------
  174. //End of include protection
  175. #endif
  176. //----------------------------------------------------------------------
  177. //EOF