tools/qcryptographichash.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include <qcryptographichash.h> -
43 -
44#include "../../3rdparty/md5/md5.h" -
45#include "../../3rdparty/md5/md5.cpp" -
46#include "../../3rdparty/md4/md4.h" -
47#include "../../3rdparty/md4/md4.cpp" -
48#include "../../3rdparty/sha1/sha1.cpp" -
49 -
50/* -
51 These #defines replace the typedefs needed by the RFC6234 code. Normally -
52 the typedefs would come from from stdint.h, but since this header is not -
53 available on all platforms (MSVC 2008, for example), we #define them to the -
54 Qt equivalents. -
55*/ -
56#define uint64_t QT_PREPEND_NAMESPACE(quint64) -
57#define uint32_t QT_PREPEND_NAMESPACE(quint32) -
58#define uint8_t QT_PREPEND_NAMESPACE(quint8) -
59#define int_least16_t QT_PREPEND_NAMESPACE(qint16) -
60 -
61// Header from rfc6234 with 1 modification: -
62// sha1.h - commented out '#include <stdint.h>' on line 74 -
63#include "../../3rdparty/rfc6234/sha.h" -
64 -
65/* -
66 These 2 functions replace macros of the same name in sha224-256.c and -
67 sha384-512.c. Originally, these macros relied on a global static 'addTemp' -
68 variable. We do not want this for 2 reasons: -
69 -
70 1. since we are including the sources directly, the declaration of the 2 conflict -
71 -
72 2. static variables are not thread-safe, we do not want multiple threads -
73 computing a hash to corrupt one another -
74*/ -
75static int SHA224_256AddLength(SHA256Context *context, unsigned int length); -
76static int SHA384_512AddLength(SHA512Context *context, unsigned int length); -
77 -
78// Sources from rfc6234, with 4 modifications: -
79// sha224-256.c - commented out 'static uint32_t addTemp;' on line 68 -
80// sha224-256.c - appended 'M' to the SHA224_256AddLength macro on line 70 -
81#include "../../3rdparty/rfc6234/sha224-256.c" -
82// sha384-512.c - commented out 'static uint64_t addTemp;' on line 302 -
83// sha384-512.c - appended 'M' to the SHA224_256AddLength macro on line 304 -
84#include "../../3rdparty/rfc6234/sha384-512.c" -
85 -
86#undef uint64_t -
87#undef uint32_t -
88#undef uint68_t -
89#undef int_least16_t -
90 -
91#include <qiodevice.h> -
92 -
93static inline int SHA224_256AddLength(SHA256Context *context, unsigned int length) -
94{ -
95 QT_PREPEND_NAMESPACE(quint32) addTemp;
executed (the execution status of this line is deduced): ::quint32 addTemp;
-
96 return SHA224_256AddLengthM(context, length);
executed: return (addTemp = (context)->Length_Low, (context)->Corrupted = (((context)->Length_Low += (length)) < addTemp) && (++(context)->Length_High == 0) ? shaInputTooLong : (context)->Corrupted );
Execution Count:24
24
97} -
98static inline int SHA384_512AddLength(SHA512Context *context, unsigned int length) -
99{ -
100 QT_PREPEND_NAMESPACE(quint64) addTemp;
executed (the execution status of this line is deduced): ::quint64 addTemp;
-
101 return SHA384_512AddLengthM(context, length);
executed: return (addTemp = context->Length_Low, context->Corrupted = ((context->Length_Low += length) < addTemp) && (++context->Length_High == 0) ? shaInputTooLong : (context)->Corrupted);
Execution Count:24
24
102} -
103 -
104QT_BEGIN_NAMESPACE -
105 -
106class QCryptographicHashPrivate -
107{ -
108public: -
109 QCryptographicHash::Algorithm method; -
110 union { -
111 MD5Context md5Context; -
112 md4_context md4Context; -
113 Sha1State sha1Context; -
114 SHA224Context sha224Context; -
115 SHA256Context sha256Context; -
116 SHA384Context sha384Context; -
117 SHA512Context sha512Context; -
118 }; -
119 QByteArray result; -
120}; -
121 -
122/*! -
123 \class QCryptographicHash -
124 \inmodule QtCore -
125 -
126 \brief The QCryptographicHash class provides a way to generate cryptographic hashes. -
127 -
128 \since 4.3 -
129 -
130 \ingroup tools -
131 \reentrant -
132 -
133 QCryptographicHash can be used to generate cryptographic hashes of binary or text data. -
134 -
135 Currently MD4, MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 are supported. -
136*/ -
137 -
138/*! -
139 \enum QCryptographicHash::Algorithm -
140 -
141 \value Md4 Generate an MD4 hash sum -
142 \value Md5 Generate an MD5 hash sum -
143 \value Sha1 Generate an SHA-1 hash sum -
144 \value Sha224 Generate an SHA-224 hash sum. Introduced in Qt 5.0 -
145 \value Sha256 Generate an SHA-256 hash sum. Introduced in Qt 5.0 -
146 \value Sha384 Generate an SHA-384 hash sum. Introduced in Qt 5.0 -
147 \value Sha512 Generate an SHA-512 hash sum. Introduced in Qt 5.0 -
148*/ -
149 -
150/*! -
151 Constructs an object that can be used to create a cryptographic hash from data using \a method. -
152*/ -
153QCryptographicHash::QCryptographicHash(Algorithm method) -
154 : d(new QCryptographicHashPrivate) -
155{ -
156 d->method = method;
executed (the execution status of this line is deduced): d->method = method;
-
157 reset();
executed (the execution status of this line is deduced): reset();
-
158}
executed: }
Execution Count:2421
2421
159 -
160/*! -
161 Destroys the object. -
162*/ -
163QCryptographicHash::~QCryptographicHash() -
164{ -
165 delete d;
executed (the execution status of this line is deduced): delete d;
-
166}
executed: }
Execution Count:2421
2421
167 -
168/*! -
169 Resets the object. -
170*/ -
171void QCryptographicHash::reset() -
172{ -
173 switch (d->method) { -
174 case Md4: -
175 md4_init(&d->md4Context);
executed (the execution status of this line is deduced): md4_init(&d->md4Context);
-
176 break;
executed: break;
Execution Count:4
4
177 case Md5: -
178 MD5Init(&d->md5Context);
executed (the execution status of this line is deduced): MD5Init(&d->md5Context);
-
179 break;
executed: break;
Execution Count:2120
2120
180 case Sha1: -
181 sha1InitState(&d->sha1Context);
executed (the execution status of this line is deduced): sha1InitState(&d->sha1Context);
-
182 break;
executed: break;
Execution Count:361
361
183 case Sha224: -
184 SHA224Reset(&d->sha224Context);
executed (the execution status of this line is deduced): SHA224Reset(&d->sha224Context);
-
185 break;
executed: break;
Execution Count:4
4
186 case Sha256: -
187 SHA256Reset(&d->sha256Context);
executed (the execution status of this line is deduced): SHA256Reset(&d->sha256Context);
-
188 break;
executed: break;
Execution Count:4
4
189 case Sha384: -
190 SHA384Reset(&d->sha384Context);
executed (the execution status of this line is deduced): SHA384Reset(&d->sha384Context);
-
191 break;
executed: break;
Execution Count:4
4
192 case Sha512: -
193 SHA512Reset(&d->sha512Context);
executed (the execution status of this line is deduced): SHA512Reset(&d->sha512Context);
-
194 break;
executed: break;
Execution Count:4
4
195 } -
196 d->result.clear();
executed (the execution status of this line is deduced): d->result.clear();
-
197}
executed: }
Execution Count:2501
2501
198 -
199/*! -
200 Adds the first \a length chars of \a data to the cryptographic -
201 hash. -
202*/ -
203void QCryptographicHash::addData(const char *data, int length) -
204{ -
205 switch (d->method) { -
206 case Md4: -
207 md4_update(&d->md4Context, (const unsigned char *)data, length);
executed (the execution status of this line is deduced): md4_update(&d->md4Context, (const unsigned char *)data, length);
-
208 break;
executed: break;
Execution Count:4
4
209 case Md5: -
210 MD5Update(&d->md5Context, (const unsigned char *)data, length);
executed (the execution status of this line is deduced): MD5Update(&d->md5Context, (const unsigned char *)data, length);
-
211 break;
executed: break;
Execution Count:2657
2657
212 case Sha1: -
213 sha1Update(&d->sha1Context, (const unsigned char *)data, length);
executed (the execution status of this line is deduced): sha1Update(&d->sha1Context, (const unsigned char *)data, length);
-
214 break;
executed: break;
Execution Count:363
363
215 case Sha224: -
216 SHA224Input(&d->sha224Context, reinterpret_cast<const unsigned char *>(data), length);
executed (the execution status of this line is deduced): SHA224Input(&d->sha224Context, reinterpret_cast<const unsigned char *>(data), length);
-
217 break;
executed: break;
Execution Count:4
4
218 case Sha256: -
219 SHA256Input(&d->sha256Context, reinterpret_cast<const unsigned char *>(data), length);
executed (the execution status of this line is deduced): SHA256Input(&d->sha256Context, reinterpret_cast<const unsigned char *>(data), length);
-
220 break;
executed: break;
Execution Count:4
4
221 case Sha384: -
222 SHA384Input(&d->sha384Context, reinterpret_cast<const unsigned char *>(data), length);
executed (the execution status of this line is deduced): SHA384Input(&d->sha384Context, reinterpret_cast<const unsigned char *>(data), length);
-
223 break;
executed: break;
Execution Count:4
4
224 case Sha512: -
225 SHA512Input(&d->sha512Context, reinterpret_cast<const unsigned char *>(data), length);
executed (the execution status of this line is deduced): SHA512Input(&d->sha512Context, reinterpret_cast<const unsigned char *>(data), length);
-
226 break;
executed: break;
Execution Count:4
4
227 } -
228 d->result.clear();
executed (the execution status of this line is deduced): d->result.clear();
-
229}
executed: }
Execution Count:3040
3040
230 -
231/*! -
232 \overload addData() -
233*/ -
234void QCryptographicHash::addData(const QByteArray &data) -
235{ -
236 addData(data.constData(), data.length());
executed (the execution status of this line is deduced): addData(data.constData(), data.length());
-
237}
executed: }
Execution Count:2767
2767
238 -
239/*! -
240 Reads the data from the open QIODevice \a device until it ends -
241 and hashes it. Returns true if reading was successful. -
242 \since 5.0 -
243 */ -
244bool QCryptographicHash::addData(QIODevice* device) -
245{ -
246 if (!device->isReadable())
evaluated: !device->isReadable()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
247 return false;
executed: return false;
Execution Count:2
2
248 -
249 if (!device->isOpen())
partially evaluated: !device->isOpen()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
250 return false;
never executed: return false;
0
251 -
252 char buffer[1024];
executed (the execution status of this line is deduced): char buffer[1024];
-
253 int length;
executed (the execution status of this line is deduced): int length;
-
254 -
255 while ((length = device->read(buffer,sizeof(buffer))) > 0)
evaluated: (length = device->read(buffer,sizeof(buffer))) > 0
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:2
2-9
256 addData(buffer,length);
executed: addData(buffer,length);
Execution Count:9
9
257 -
258 return device->atEnd();
executed: return device->atEnd();
Execution Count:2
2
259} -
260 -
261 -
262/*! -
263 Returns the final hash value. -
264 -
265 \sa QByteArray::toHex() -
266*/ -
267QByteArray QCryptographicHash::result() const -
268{ -
269 if (!d->result.isEmpty())
evaluated: !d->result.isEmpty()
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:2501
14-2501
270 return d->result;
executed: return d->result;
Execution Count:14
14
271 -
272 switch (d->method) { -
273 case Md4: { -
274 md4_context copy = d->md4Context;
executed (the execution status of this line is deduced): md4_context copy = d->md4Context;
-
275 d->result.resize(MD4_RESULTLEN);
executed (the execution status of this line is deduced): d->result.resize((128/8));
-
276 md4_final(&copy, (unsigned char *)d->result.data());
executed (the execution status of this line is deduced): md4_final(&copy, (unsigned char *)d->result.data());
-
277 break;
executed: break;
Execution Count:4
4
278 } -
279 case Md5: { -
280 MD5Context copy = d->md5Context;
executed (the execution status of this line is deduced): MD5Context copy = d->md5Context;
-
281 d->result.resize(16);
executed (the execution status of this line is deduced): d->result.resize(16);
-
282 MD5Final(&copy, (unsigned char *)d->result.data());
executed (the execution status of this line is deduced): MD5Final(&copy, (unsigned char *)d->result.data());
-
283 break;
executed: break;
Execution Count:2120
2120
284 } -
285 case Sha1: { -
286 Sha1State copy = d->sha1Context;
executed (the execution status of this line is deduced): Sha1State copy = d->sha1Context;
-
287 d->result.resize(20);
executed (the execution status of this line is deduced): d->result.resize(20);
-
288 sha1FinalizeState(&copy);
executed (the execution status of this line is deduced): sha1FinalizeState(&copy);
-
289 sha1ToHash(&copy, (unsigned char *)d->result.data());
executed (the execution status of this line is deduced): sha1ToHash(&copy, (unsigned char *)d->result.data());
-
290 break;
executed: break;
Execution Count:361
361
291 } -
292 case Sha224: { -
293 SHA224Context copy = d->sha224Context;
executed (the execution status of this line is deduced): SHA224Context copy = d->sha224Context;
-
294 d->result.resize(SHA224HashSize);
executed (the execution status of this line is deduced): d->result.resize(SHA224HashSize);
-
295 SHA224Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
executed (the execution status of this line is deduced): SHA224Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
-
296 break;
executed: break;
Execution Count:4
4
297 } -
298 case Sha256:{ -
299 SHA256Context copy = d->sha256Context;
executed (the execution status of this line is deduced): SHA256Context copy = d->sha256Context;
-
300 d->result.resize(SHA256HashSize);
executed (the execution status of this line is deduced): d->result.resize(SHA256HashSize);
-
301 SHA256Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
executed (the execution status of this line is deduced): SHA256Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
-
302 break;
executed: break;
Execution Count:4
4
303 } -
304 case Sha384:{ -
305 SHA384Context copy = d->sha384Context;
executed (the execution status of this line is deduced): SHA384Context copy = d->sha384Context;
-
306 d->result.resize(SHA384HashSize);
executed (the execution status of this line is deduced): d->result.resize(SHA384HashSize);
-
307 SHA384Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
executed (the execution status of this line is deduced): SHA384Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
-
308 break;
executed: break;
Execution Count:4
4
309 } -
310 case Sha512:{ -
311 SHA512Context copy = d->sha512Context;
executed (the execution status of this line is deduced): SHA512Context copy = d->sha512Context;
-
312 d->result.resize(SHA512HashSize);
executed (the execution status of this line is deduced): d->result.resize(SHA512HashSize);
-
313 SHA512Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
executed (the execution status of this line is deduced): SHA512Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
-
314 break;
executed: break;
Execution Count:4
4
315 } -
316 } -
317 return d->result;
executed: return d->result;
Execution Count:2501
2501
318} -
319 -
320/*! -
321 Returns the hash of \a data using \a method. -
322*/ -
323QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method) -
324{ -
325 QCryptographicHash hash(method);
executed (the execution status of this line is deduced): QCryptographicHash hash(method);
-
326 hash.addData(data);
executed (the execution status of this line is deduced): hash.addData(data);
-
327 return hash.result();
executed: return hash.result();
Execution Count:2066
2066
328} -
329 -
330QT_END_NAMESPACE -
331 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial