blocxx

MD5.hpp

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