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