blocxx
MD5.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
42 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
43 rights reserved.
44 License to copy and use this software is granted provided that it
45 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
46 Algorithm" in all material mentioning or referencing this software
47 or this function.
48 License is also granted to make and use derivative works provided
49 that such works are identified as "derived from the RSA Data
50 Security, Inc. MD5 Message-Digest Algorithm" in all material
51 mentioning or referencing the derived work.
52 RSA Data Security, Inc. makes no representations concerning either
53 the merchantability of this software or the suitability of this
54 software for any particular purpose. It is provided "as is"
55 without express or implied warranty of any kind.
56 These notices must be retained in any copies of any part of this
57 documentation and/or software.
58  */
59 #ifndef BLOCXX_MD5_HPP_INCLUDE_GUARD_
60 #define BLOCXX_MD5_HPP_INCLUDE_GUARD_
61 #include "blocxx/BLOCXX_config.h"
62 #include "blocxx/Types.hpp"
63 #include "blocxx/Exception.hpp"
64 #include "blocxx/CommonFwd.hpp"
65 #ifdef BLOCXX_HAVE_STREAMBUF
66 #include <streambuf>
67 #else
68 #include <streambuf.h>
69 #endif
70 #ifdef BLOCXX_HAVE_OSTREAM
71 #include <ostream>
72 #elif defined(BLOCXX_HAVE_OSTREAM_H)
73 #include <ostream.h>
74 #else
75 #include <iostream>
76 #endif
77 
78 namespace BLOCXX_NAMESPACE
79 {
80 
81 BLOCXX_DECLARE_APIEXCEPTION(MD5, BLOCXX_COMMON_API)
82 
83 const int MD5HASHLEN = 16;
85 class BLOCXX_COMMON_API MD5StreamBuffer : public std::streambuf
86 {
87 public:
88  MD5StreamBuffer(MD5* md5);
89 protected:
91  virtual int overflow(int c);
92  virtual std::streamsize xsputn(const char* s, std::streamsize num);
93 };
95 class BLOCXX_COMMON_API MD5OStreamBase
96 {
97 public:
99  MD5OStreamBase(MD5* md5);
100 };
102 class BLOCXX_COMMON_API MD5 : private MD5OStreamBase, public std::ostream
103 {
104 /* MD5 context. */
105 public:
106  MD5();
111  MD5(const String& input);
112  void init(const String& input);
113  ~MD5() {};
114  typedef struct
115  {
116  UInt32 state[4]; /* state (ABCD) */
117  UInt32 count[2]; /* number of bits, modulo 2^64 (lsb first) */
118  unsigned char buffer[64]; /* input buffer */
119  } MD5_CTX;
124  void update(const String& input);
129  String toString();
130  unsigned char* getDigest();
131  static String convertBinToHex( const unsigned char* sBin);
132 private:
134  unsigned char m_digest[16];
136  static void MD5Init(MD5_CTX * md5ctx);
137  static void MD5Update(MD5_CTX *md5ctx, const unsigned char* input,
138  UInt32 inputLen);
139  static void MD5Final(unsigned char*, MD5_CTX *);
140  friend class MD5StreamBuffer;
141 };
142 
143 } // end namespace BLOCXX_NAMESPACE
144 
145 #endif