1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <qcryptographichash.h>
|
---|
43 |
|
---|
44 | #ifdef Q_OS_SYMBIAN
|
---|
45 | #define _MD5_H_ // Needed to disable system header
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include "../../3rdparty/md5/md5.h"
|
---|
49 | #include "../../3rdparty/md5/md5.cpp"
|
---|
50 | #include "../../3rdparty/md4/md4.h"
|
---|
51 | #include "../../3rdparty/md4/md4.cpp"
|
---|
52 | #include "../../3rdparty/sha1/sha1.cpp"
|
---|
53 |
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | class QCryptographicHashPrivate
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | QCryptographicHash::Algorithm method;
|
---|
61 | union {
|
---|
62 | MD5Context md5Context;
|
---|
63 | md4_context md4Context;
|
---|
64 | Sha1State sha1Context;
|
---|
65 | };
|
---|
66 | QByteArray result;
|
---|
67 | };
|
---|
68 |
|
---|
69 | /*!
|
---|
70 | \class QCryptographicHash
|
---|
71 |
|
---|
72 | \brief The QCryptographicHash class provides a way to generate cryptographic hashes.
|
---|
73 |
|
---|
74 | \since 4.3
|
---|
75 |
|
---|
76 | \ingroup tools
|
---|
77 | \reentrant
|
---|
78 |
|
---|
79 | QCryptographicHash can be used to generate cryptographic hashes of binary or text data.
|
---|
80 |
|
---|
81 | Currently MD4, MD5, and SHA-1 are supported.
|
---|
82 | */
|
---|
83 |
|
---|
84 | /*!
|
---|
85 | \enum QCryptographicHash::Algorithm
|
---|
86 |
|
---|
87 | \value Md4 Generate an MD4 hash sum
|
---|
88 | \value Md5 Generate an MD5 hash sum
|
---|
89 | \value Sha1 Generate an SHA1 hash sum
|
---|
90 | */
|
---|
91 |
|
---|
92 | /*!
|
---|
93 | Constructs an object that can be used to create a cryptographic hash from data using \a method.
|
---|
94 | */
|
---|
95 | QCryptographicHash::QCryptographicHash(Algorithm method)
|
---|
96 | : d(new QCryptographicHashPrivate)
|
---|
97 | {
|
---|
98 | d->method = method;
|
---|
99 | reset();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*!
|
---|
103 | Destroys the object.
|
---|
104 | */
|
---|
105 | QCryptographicHash::~QCryptographicHash()
|
---|
106 | {
|
---|
107 | delete d;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*!
|
---|
111 | Resets the object.
|
---|
112 | */
|
---|
113 | void QCryptographicHash::reset()
|
---|
114 | {
|
---|
115 | switch (d->method) {
|
---|
116 | case Md4:
|
---|
117 | md4_init(&d->md4Context);
|
---|
118 | break;
|
---|
119 | case Md5:
|
---|
120 | MD5Init(&d->md5Context);
|
---|
121 | break;
|
---|
122 | case Sha1:
|
---|
123 | sha1InitState(&d->sha1Context);
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | d->result.clear();
|
---|
127 | }
|
---|
128 |
|
---|
129 | /*!
|
---|
130 | Adds the first \a length chars of \a data to the cryptographic
|
---|
131 | hash.
|
---|
132 | */
|
---|
133 | void QCryptographicHash::addData(const char *data, int length)
|
---|
134 | {
|
---|
135 | switch (d->method) {
|
---|
136 | case Md4:
|
---|
137 | md4_update(&d->md4Context, (const unsigned char *)data, length);
|
---|
138 | break;
|
---|
139 | case Md5:
|
---|
140 | MD5Update(&d->md5Context, (const unsigned char *)data, length);
|
---|
141 | break;
|
---|
142 | case Sha1:
|
---|
143 | sha1Update(&d->sha1Context, (const unsigned char *)data, length);
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | d->result.clear();
|
---|
147 | }
|
---|
148 |
|
---|
149 | /*!
|
---|
150 | /overload
|
---|
151 | */
|
---|
152 | void QCryptographicHash::addData(const QByteArray &data)
|
---|
153 | {
|
---|
154 | addData(data.constData(), data.length());
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*!
|
---|
158 | Returns the final hash value.
|
---|
159 |
|
---|
160 | \sa QByteArray::toHex()
|
---|
161 | */
|
---|
162 | QByteArray QCryptographicHash::result() const
|
---|
163 | {
|
---|
164 | if (!d->result.isEmpty())
|
---|
165 | return d->result;
|
---|
166 |
|
---|
167 | switch (d->method) {
|
---|
168 | case Md4: {
|
---|
169 | md4_context copy = d->md4Context;
|
---|
170 | d->result.resize(MD4_RESULTLEN);
|
---|
171 | md4_final(©, (unsigned char *)d->result.data());
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | case Md5: {
|
---|
175 | MD5Context copy = d->md5Context;
|
---|
176 | d->result.resize(16);
|
---|
177 | MD5Final(©, (unsigned char *)d->result.data());
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | case Sha1: {
|
---|
181 | Sha1State copy = d->sha1Context;
|
---|
182 | d->result.resize(20);
|
---|
183 | sha1FinalizeState(©);
|
---|
184 | sha1ToHash(©, (unsigned char *)d->result.data());
|
---|
185 | }
|
---|
186 | }
|
---|
187 | return d->result;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*!
|
---|
191 | Returns the hash of \a data using \a method.
|
---|
192 | */
|
---|
193 | QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
|
---|
194 | {
|
---|
195 | QCryptographicHash hash(method);
|
---|
196 | hash.addData(data);
|
---|
197 | return hash.result();
|
---|
198 | }
|
---|
199 |
|
---|
200 | QT_END_NAMESPACE
|
---|