00001 /******************************************************************************* 00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * - Redistributions of source code must retain the above copyright notice, 00008 * this list of conditions and the following disclaimer. 00009 * 00010 * - Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * - Neither the name of Vintela, Inc. nor the names of its 00015 * contributors may be used to endorse or promote products derived from this 00016 * software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS 00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 *******************************************************************************/ 00030 00039 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 00040 rights reserved. 00041 License to copy and use this software is granted provided that it 00042 is identified as the "RSA Data Security, Inc. MD5 Message-Digest 00043 Algorithm" in all material mentioning or referencing this software 00044 or this function. 00045 License is also granted to make and use derivative works provided 00046 that such works are identified as "derived from the RSA Data 00047 Security, Inc. MD5 Message-Digest Algorithm" in all material 00048 mentioning or referencing the derived work. 00049 RSA Data Security, Inc. makes no representations concerning either 00050 the merchantability of this software or the suitability of this 00051 software for any particular purpose. It is provided "as is" 00052 without express or implied warranty of any kind. 00053 These notices must be retained in any copies of any part of this 00054 documentation and/or software. 00055 */ 00056 #ifndef OW_MD5_HPP_INCLUDE_GUARD_ 00057 #define OW_MD5_HPP_INCLUDE_GUARD_ 00058 #include "OW_config.h" 00059 #include "OW_Types.hpp" 00060 #include "OW_Exception.hpp" 00061 #include "OW_CommonFwd.hpp" 00062 #ifdef OW_HAVE_STREAMBUF 00063 #include <streambuf> 00064 #else 00065 #include <streambuf.h> 00066 #endif 00067 #ifdef OW_HAVE_OSTREAM 00068 #include <ostream> 00069 #elif defined(OW_HAVE_OSTREAM_H) 00070 #include <ostream.h> 00071 #else 00072 #include <iostream> 00073 #endif 00074 00075 namespace OW_NAMESPACE 00076 { 00077 00078 OW_DECLARE_APIEXCEPTION(MD5, OW_COMMON_API) 00079 00080 const int MD5HASHLEN = 16; 00082 class OW_COMMON_API MD5StreamBuffer : public std::streambuf 00083 { 00084 public: 00085 MD5StreamBuffer(MD5* md5); 00086 protected: 00087 MD5* _md5; 00088 virtual int overflow(int c); 00089 virtual std::streamsize xsputn(const char* s, std::streamsize num); 00090 }; 00092 class OW_COMMON_API MD5OStreamBase 00093 { 00094 public: 00095 MD5StreamBuffer _buf; 00096 MD5OStreamBase(MD5* md5); 00097 }; 00099 class OW_COMMON_API MD5 : private MD5OStreamBase, public std::ostream 00100 { 00101 /* MD5 context. */ 00102 public: 00103 MD5(); 00108 MD5(const String& input); 00109 void init(const String& input); 00110 ~MD5() {}; 00111 typedef struct 00112 { 00113 UInt32 state[4]; /* state (ABCD) */ 00114 UInt32 count[2]; /* number of bits, modulo 2^64 (lsb first) */ 00115 unsigned char buffer[64]; /* input buffer */ 00116 } MD5_CTX; 00121 void update(const String& input); 00126 String toString(); 00127 unsigned char* getDigest(); 00128 static String convertBinToHex( const unsigned char* sBin); 00129 private: 00130 MD5_CTX m_ctx; 00131 unsigned char m_digest[16]; 00132 bool m_finished; 00133 static void MD5Init(MD5_CTX * md5ctx); 00134 static void MD5Update(MD5_CTX *md5ctx, const unsigned char* input, 00135 UInt32 inputLen); 00136 static void MD5Final(unsigned char*, MD5_CTX *); 00137 friend class MD5StreamBuffer; 00138 }; 00139 00140 } // end namespace OW_NAMESPACE 00141 00142 #endif