File indexing completed on 2024-09-29 12:04:14
0001 /* 0002 Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org> 0003 Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org> 0004 0005 This program is free software; you can redistribute it and/or modify 0006 it under the terms of the GNU Lesser General Public License (LGPL) 0007 version 2 as published by the Free Software Foundation. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 GNU Lesser General Public License for more details. 0013 0014 You should have received a copy of the GNU Lesser General Public 0015 License along with this program; if not, write to the Free Software 0016 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 0017 0018 RFC 1321 "MD5 Message-Digest Algorithm" Copyright (C) 1991-1992. // krazy:exclude=copyright 0019 RSA Data Security, Inc. Created 1991. All rights reserved. 0020 0021 The KMD5 class is based on a C++ implementation of 0022 "RSA Data Security, Inc. MD5 Message-Digest Algorithm" by 0023 Mordechai T. Abzug, Copyright (c) 1995. This implementation // krazy:exclude=copyright 0024 passes the test-suite as defined in RFC 1321. 0025 0026 The encoding and decoding utilities in KCodecs with the exception of 0027 quoted-printable are based on the java implementation in HTTPClient 0028 package by Ronald Tschalär Copyright (C) 1996-1999. // krazy:exclude=copyright 0029 0030 The quoted-printable codec as described in RFC 2045, section 6.7. is by 0031 Rik Hemsley (C) 2001. 0032 */ 0033 0034 #ifndef KMD5_H 0035 #define KMD5_H 0036 0037 #include <kdelibs4support_export.h> 0038 #include <qglobal.h> 0039 0040 class QByteArray; 0041 class QIODevice; 0042 0043 class KMD5Private; 0044 /** 0045 * @short An adapted C++ implementation of RSA Data Securities MD5 algorithm. 0046 * 0047 * DEPRECATED. please use QCryptographicHash instead 0048 * 0049 * The default constructor is designed to provide much the same 0050 * functionality as the most commonly used C-implementation, while 0051 * the other three constructors are meant to further simplify the 0052 * process of obtaining a digest by calculating the result in a 0053 * single step. 0054 * 0055 * KMD5 is state-based, that means you can add new contents with 0056 * update() as long as you didn't request the digest value yet. 0057 * After the digest value was requested, the object is "finalized" 0058 * and you have to call reset() to be able to do another calculation 0059 * with it. The reason for this behavior is that upon requesting 0060 * the message digest KMD5 has to pad the received contents up to a 0061 * 64 byte boundary to calculate its value. After this operation it 0062 * is not possible to resume consuming data. 0063 * 0064 * \b Usage: 0065 * 0066 * A common usage of this class: 0067 * 0068 * \code 0069 * const char* test1; 0070 * KMD5::Digest rawResult; 0071 * 0072 * test1 = "This is a simple test."; 0073 * KMD5 context (test1); 0074 * cout << "Hex Digest output: " << context.hexDigest().data() << endl; 0075 * \endcode 0076 * 0077 * To cut down on the unnecessary overhead of creating multiple KMD5 0078 * objects, you can simply invoke reset() to reuse the same object 0079 * in making another calculation: 0080 * 0081 * \code 0082 * context.reset (); 0083 * context.update ("TWO"); 0084 * context.update ("THREE"); 0085 * cout << "Hex Digest output: " << context.hexDigest().data() << endl; 0086 * \endcode 0087 * 0088 * @author Dirk Mueller <mueller@kde.org>, Dawit Alemayehu <adawit@kde.org> 0089 * 0090 * @deprecated 0091 * @see QCryptographicHash 0092 */ 0093 0094 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KMD5 0095 { 0096 public: 0097 0098 typedef unsigned char Digest[16]; 0099 0100 KMD5(); 0101 ~KMD5(); 0102 0103 /** 0104 * Constructor that updates the digest for the given string. 0105 * 0106 * @param in C string or binary data 0107 * @param len if negative, calculates the length by using 0108 * strlen on the first parameter, otherwise 0109 * it trusts the given length (does not stop on NUL byte). 0110 */ 0111 KDELIBS4SUPPORT_DEPRECATED explicit KMD5(const char *in, int len = -1); 0112 0113 /** 0114 * @overload 0115 * 0116 * Same as above except it accepts a QByteArray as its argument. 0117 */ 0118 KDELIBS4SUPPORT_DEPRECATED explicit KMD5(const QByteArray &a); 0119 0120 /** 0121 * Updates the message to be digested. Be sure to add all data 0122 * before you read the digest. After reading the digest, you 0123 * can <b>not</b> add more data! 0124 * 0125 * @param in message to be added to digest 0126 * @param len the length of the given message. 0127 * 0128 * @deprecated please use QCryptographicHash::addData instead 0129 */ 0130 void update(const char *in, int len = -1); 0131 0132 /** 0133 * @overload 0134 * 0135 * please use QCryptographicHash::addData instead 0136 */ 0137 void update(const unsigned char *in, int len = -1); 0138 0139 /** 0140 * @overload 0141 * 0142 * @param in message to be added to the digest (QByteArray). 0143 * 0144 * @deprecated please use QCryptographicHash::addData instead 0145 */ 0146 void update(const QByteArray &in); 0147 0148 /** 0149 * @overload 0150 * 0151 * reads the data from an I/O device, i.e. from a file (QFile). 0152 * 0153 * NOTE that the file must be open for reading. 0154 * 0155 * @param file a pointer to FILE as returned by calls like f{d,re}open 0156 * 0157 * @returns false if an error occurred during reading. 0158 * 0159 * @deprecated please use QCryptographicHash::addData instead 0160 */ 0161 bool update(QIODevice &file); 0162 0163 /** 0164 * Calling this function will reset the calculated message digest. 0165 * Use this method to perform another message digest calculation 0166 * without recreating the KMD5 object. 0167 * 0168 * @deprecated please use QCryptographicHash::reset() instead 0169 */ 0170 void reset(); 0171 0172 /** 0173 * @return the raw representation of the digest 0174 * @deprecated please use QCryptographicHash::result instead 0175 */ 0176 const Digest &rawDigest(); //krazy:exclude=constref (simple array) 0177 0178 /** 0179 * Fills the given array with the binary representation of the 0180 * message digest. 0181 * 0182 * Use this method if you do not want to worry about making 0183 * copy of the digest once you obtain it. 0184 * 0185 * @param bin an array of 16 characters ( char[16] ) 0186 */ 0187 void rawDigest(KMD5::Digest &bin); 0188 0189 /** 0190 * Returns the value of the calculated message digest in 0191 * a hexadecimal representation. 0192 * @deprecated please use QCryptographicHash::result().toHex() instead 0193 */ 0194 QByteArray hexDigest(); 0195 0196 /** 0197 * @overload 0198 */ 0199 void hexDigest(QByteArray &); 0200 0201 /** 0202 * Returns the value of the calculated message digest in 0203 * a base64-encoded representation. 0204 * @deprecated please use QCryptographicHash::result().toBase64() instead 0205 */ 0206 QByteArray base64Digest(); 0207 0208 /** 0209 * returns true if the calculated digest for the given 0210 * message matches the given one. 0211 */ 0212 bool verify(const KMD5::Digest &digest); 0213 0214 /** 0215 * @overload 0216 */ 0217 bool verify(const QByteArray &); 0218 0219 protected: 0220 /** 0221 * Performs the real update work. Note 0222 * that length is implied to be 64. 0223 */ 0224 void transform(const unsigned char buffer[64]); 0225 0226 /** 0227 * finalizes the digest 0228 */ 0229 void finalize(); 0230 0231 private: 0232 KMD5(const KMD5 &u); 0233 KMD5 &operator=(const KMD5 &md); 0234 0235 void init(); 0236 void encode(unsigned char *output, quint32 *in, quint32 len); 0237 void decode(quint32 *output, const unsigned char *in, quint32 len); 0238 0239 quint32 rotate_left(quint32 x, quint32 n); 0240 quint32 F(quint32 x, quint32 y, quint32 z); 0241 quint32 G(quint32 x, quint32 y, quint32 z); 0242 quint32 H(quint32 x, quint32 y, quint32 z); 0243 quint32 I(quint32 x, quint32 y, quint32 z); 0244 void FF(quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, 0245 quint32 s, quint32 ac); 0246 void GG(quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, 0247 quint32 s, quint32 ac); 0248 void HH(quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, 0249 quint32 s, quint32 ac); 0250 void II(quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, 0251 quint32 s, quint32 ac); 0252 0253 private: 0254 quint32 m_state[4]; 0255 quint32 m_count[2]; 0256 quint8 m_buffer[64]; 0257 Digest m_digest; 0258 bool m_finalized; 0259 0260 KMD5Private *d; 0261 }; 0262 0263 #endif // KCODECS_H