fb035fef9ddeede6c74d363fd9ac9013bca9692f
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / crypto / qxthash.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCrypto module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24
25 #include "qxthash.h"
26 #include "qxthash_p.h"
27
28 /**
29 \class QxtHash QxtHash
30
31 \ingroup QxtCrypto
32
33 \brief Hashing funcions
34
35 \code
36 qDebug()<<QxtHash(QxtHash::Md5,"foo").hash();
37 \endcode
38 */
39 /**
40 \fn QxtHash::QxtHash (Algorithm algo)
41 constructs a new hash with Algorithm \p algo
42 */
43 QxtHash::QxtHash (Algorithm algo)
44 {
45     QXT_INIT_PRIVATE(QxtHash);
46     qxt_d().algo=algo;
47     reset();
48 }
49 /**
50 \fn QxtHash::QxtHash (Algorithm algo, const QByteArray & data)
51 constructs a new hash with Algorithm \p algo and reads the data \p data
52 */
53 QxtHash::QxtHash (Algorithm algo, const QByteArray & data)
54 {
55     QXT_INIT_PRIVATE(QxtHash);
56     qxt_d().algo=algo;
57     reset();
58     append(data);
59 }
60 /**
61 \fn void QxtHash::append ( const QByteArray & dt)
62 appends the data \p dt to the current data
63 */
64
65 void QxtHash::append ( const QByteArray & dt)
66 {
67     if (qxt_d().algo==Md5)
68     {
69         MD5Update(&qxt_d().md5ctx, (const unsigned char *)dt.constData(), dt.length());
70     }
71     else if (qxt_d().algo==Md4)
72     {
73         md4_update(&qxt_d().md4ctx, (const unsigned char *)dt.constData(), dt.length());
74     }
75
76 }
77 /**
78 \fn void QxtHash::operator+= ( const QByteArray & dt)
79 appends the data \p dt to the current data
80 */
81 void  QxtHash::operator+= ( const QByteArray & dt)
82 {
83     append(dt);
84 }
85 /**
86 \fn void QxtHash::reset()
87 resets teh hash, deletes all data
88 */
89 void QxtHash::reset()
90 {
91     if (qxt_d().algo==Md5)
92     {
93         MD5Init(&qxt_d().md5ctx);
94     }
95     else if (qxt_d().algo==Md4)
96     {
97         md4_init(&qxt_d().md4ctx);
98     }
99 }
100 /**
101 \fn QByteArray QxtHash::hash()
102 returns the resulting hash
103 */
104
105 QByteArray QxtHash::hash()
106 {
107     QByteArray hs;
108
109     if (qxt_d().algo==Md5)
110     {
111         hs.resize(16);
112         MD5Final(&qxt_d().md5ctx, (unsigned char *)hs.data());
113     }
114     else if (qxt_d().algo==Md4)
115     {
116         hs.resize(MD4_RESULTLEN);
117         md4_final(&qxt_d().md4ctx, (unsigned char *)hs.data());
118     }
119
120     return hs;
121 }
122
123
124 ///uh oh
125 #include "thirdparty/md5.cpp"
126 #include "thirdparty/md4.cpp"
127
128