hl_hashwrapper.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * hashlib++ - a simple hash library for C++
  3. *
  4. * Copyright (c) 2007-2011 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. //doxygen mainpage
  30. /**
  31. * @mainpage hashlib++ source documentation
  32. *
  33. * <div align="center"><b>Version 0.3.4</b></div>
  34. *
  35. *
  36. * @section intro Introduction
  37. * hashlib++ a simple hash library for C++ \n
  38. * Copyright (c) 2007-2011 Benjamin Gr&uuml;delbach
  39. *
  40. *
  41. *
  42. * hashlib++ is a simple and very easy to use library to create a
  43. * cryptographic checksum called "hash". hashlib++ is written in
  44. * plain C++ and should work with every compiler and platform.
  45. * hashlib++ is released under the BSD-license and
  46. * therefore free software.
  47. *
  48. * @section about About this document
  49. *
  50. * This is the documentation about the hashlib++ sourcecode.
  51. * Since it contains some internal information it its helpfull
  52. * but not necessary to read for the average user.
  53. * If you are new to hashlib++ you should start with reading
  54. * the README.TXT file which contains all relevant information
  55. * to start using this library.
  56. *
  57. */
  58. //----------------------------------------------------------------------
  59. /**
  60. * @file hl_hashwrapper.h
  61. * @brief This file contains the hashwrapper base class
  62. * @date Mo 17 Sep 2007
  63. */
  64. //----------------------------------------------------------------------
  65. //include protection
  66. #ifndef HASHWRAPPER_H
  67. #define HASHWRAPPER_H
  68. //----------------------------------------------------------------------
  69. //STL includes
  70. #include <string>
  71. //----------------------------------------------------------------------
  72. //C includes
  73. //#include <stdio.h>
  74. #include <fstream>
  75. //----------------------------------------------------------------------
  76. //hashlib++ includes
  77. #include "hl_exception.h"
  78. //----------------------------------------------------------------------
  79. /**
  80. * @brief This class represents the baseclass for all subwrappers
  81. *
  82. * hashwrapper is the abstract base class of all subwrappers like md5wrapper
  83. * or sha1wrapper. This class implements two simple and easy to use
  84. * memberfunctions to create a hash based on a string or a file.
  85. * ( getHashFromString() and getHashFromFile() )
  86. *
  87. * getHashFromString() calls resetContext(), updateContext() and hashIt()
  88. * in this order. These three memberfunctions are pure virtual and have to
  89. * be implemented by the subclasses.
  90. *
  91. * getHashFromFile() calls resetContext() before reading the specified file
  92. * in 1024 byte blocks which are forwarded to the hash context by calling
  93. * updateContext(). Finaly hashIt() is called to return the hash.
  94. */
  95. class hashwrapper
  96. {
  97. private:
  98. const std::string teststring;
  99. protected:
  100. /**
  101. * @brief This method finalizes the hash process
  102. * and returns the hash as std::string
  103. *
  104. * This memberfunction is pure virtual and
  105. * has to be implemented by the subclass
  106. *
  107. * @return the created hash as std::string
  108. */
  109. virtual std::string hashIt(void) = 0;
  110. /**
  111. * @brief This internal member-function
  112. * convertes the hash-data to a
  113. * std::string (HEX)
  114. *
  115. * This memberfunction is pure virtual and
  116. * has to be implemented by the subclass
  117. *
  118. * @param data The hash-data to covert into HEX
  119. * @return The converted data as std::string
  120. */
  121. virtual std::string convToString(unsigned char *data) = 0;
  122. /**
  123. * @brief This method adds the given data to the
  124. * current hash context
  125. *
  126. * This memberfunction is pure virtual and
  127. * has to be implemented by the subclass
  128. *
  129. * @param data The data to add to the current context
  130. * @param len The length of the data to add
  131. */
  132. virtual void updateContext(unsigned char *data, unsigned int len) = 0;
  133. /**
  134. * @brief This method resets the current hash context.
  135. * In other words: It starts a new hash process.
  136. *
  137. * This memberfunction is pure virtual and
  138. * has to be implemented by the subclass
  139. */
  140. virtual void resetContext(void) = 0;
  141. /**
  142. * @brief This method should return the hash of the
  143. * test-string "The quick brown fox jumps over the lazy
  144. * dog"
  145. */
  146. virtual std::string getTestHash(void) = 0;
  147. public:
  148. /**
  149. * @brief Default Konstruktor
  150. */
  151. hashwrapper( void )
  152. : teststring("The quick brown fox jumps over the lazy dog")
  153. {
  154. }
  155. /**
  156. * @brief Default destructor
  157. */
  158. virtual ~hashwrapper ( void ) { };
  159. /**
  160. * @brief Method for testing the concrete implementation
  161. */
  162. virtual void test( void )
  163. {
  164. std::string hash = this->getHashFromString(teststring);
  165. std::string verify = this->getTestHash();
  166. if(hash != verify)
  167. {
  168. throw hlException(HL_VERIFY_TEST_FAILED,
  169. "hashlib test-error: \"" +
  170. hash +
  171. "\" is not \"" +
  172. verify +
  173. "\" as supposed to be.");
  174. }
  175. }
  176. /**
  177. * @brief This method creates a hash based on the
  178. * given string
  179. *
  180. * This memberfunctions calls resetContext(),
  181. * updateContext() and hashIt() in this order.
  182. * These three memberfunctions are pure virtual and have to
  183. * be implemented by the subclasses.
  184. *
  185. * @param text The text to create a hash from. This
  186. * parameter is forwarded to updateContext()
  187. * @return the created hash as std::string
  188. */
  189. virtual std::string getHashFromString(std::string text)
  190. {
  191. /*
  192. * reset the context so that we can start
  193. * with a new hash process
  194. */
  195. resetContext();
  196. /*
  197. * we update the context with the given text
  198. */
  199. updateContext((unsigned char*) text.c_str(),text.length());
  200. /*
  201. * now we can close the hash process
  202. * and return the created hash
  203. */
  204. return this->hashIt();
  205. }
  206. /**
  207. * @brief This method creates a hash from a given file
  208. *
  209. * First of all resetContext() is called before reading the
  210. * specified file in 1024 byte blocks which are forwarded
  211. * to the hash context by calling updateContext().
  212. * Finaly hashIt() is called to return the hash.
  213. * These three memberfunctions are pure virtual and have to
  214. * be implemented by the subclasses.
  215. *
  216. * @param filename The file to created a hash from
  217. *
  218. * @return The created hash of the file or "-1" in case
  219. * the file could not be opened
  220. * @throw Throws a hlException if the specified file could not
  221. * be opened.
  222. */
  223. virtual std::string getHashFromFile(std::string filename)
  224. {
  225. FILE *file;
  226. int len;
  227. unsigned char buffer[1024];
  228. /*
  229. * reset the current hash context
  230. */
  231. resetContext();
  232. /*
  233. * open the specified file
  234. */
  235. if((file = fopen(filename.c_str(), "rb")) == NULL)
  236. {
  237. throw hlException(HL_FILE_READ_ERROR,
  238. "Cannot read file \"" +
  239. filename +
  240. "\".");
  241. }
  242. /*
  243. * read the file in 1024b blocks and
  244. * update the context for every block
  245. */
  246. while( (len = fread(buffer,1,1024,file)) )
  247. {
  248. updateContext(buffer, len);
  249. }
  250. //close the file and create the hash
  251. fclose(file);
  252. return(hashIt());
  253. }
  254. };
  255. //----------------------------------------------------------------------
  256. //end of include protection
  257. #endif
  258. //----------------------------------------------------------------------
  259. //EOF