| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qcryptographichash.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Copyright (C) 2013 Richard J. Moore <rich@kde.org>. | - | ||||||||||||
| 5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 6 | ** | - | ||||||||||||
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 8 | ** | - | ||||||||||||
| 9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 10 | ** Commercial License Usage | - | ||||||||||||
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 15 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 16 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
| 17 | ** | - | ||||||||||||
| 18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 20 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
| 21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 26 | ** | - | ||||||||||||
| 27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 30 | ** | - | ||||||||||||
| 31 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 32 | ** | - | ||||||||||||
| 33 | ****************************************************************************/ | - | ||||||||||||
| 34 | - | |||||||||||||
| 35 | #include <qcryptographichash.h> | - | ||||||||||||
| 36 | #include <qiodevice.h> | - | ||||||||||||
| 37 | - | |||||||||||||
| 38 | #include "../../3rdparty/sha1/sha1.cpp" | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | #if defined(QT_BOOTSTRAPPED) && !defined(QT_CRYPTOGRAPHICHASH_ONLY_SHA1) | - | ||||||||||||
| 41 | # error "Are you sure you need the other hashing algorithms besides SHA-1?" | - | ||||||||||||
| 42 | #endif | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | #ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 | - | ||||||||||||
| 45 | // qdoc and qmake only need SHA-1 | - | ||||||||||||
| 46 | #include "../../3rdparty/md5/md5.h" | - | ||||||||||||
| 47 | #include "../../3rdparty/md5/md5.cpp" | - | ||||||||||||
| 48 | #include "../../3rdparty/md4/md4.h" | - | ||||||||||||
| 49 | #include "../../3rdparty/md4/md4.cpp" | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | typedef unsigned char BitSequence; | - | ||||||||||||
| 52 | typedef unsigned long long DataLength; | - | ||||||||||||
| 53 | typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn; | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | #include "../../3rdparty/sha3/KeccakSponge.c" | - | ||||||||||||
| 56 | typedef spongeState hashState; | - | ||||||||||||
| 57 | - | |||||||||||||
| 58 | #include "../../3rdparty/sha3/KeccakNISTInterface.c" | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | /* | - | ||||||||||||
| 61 | This lets us choose between SHA3 implementations at build time. | - | ||||||||||||
| 62 | */ | - | ||||||||||||
| 63 | typedef spongeState SHA3Context; | - | ||||||||||||
| 64 | typedef HashReturn (SHA3Init)(hashState *state, int hashbitlen); | - | ||||||||||||
| 65 | typedef HashReturn (SHA3Update)(hashState *state, const BitSequence *data, DataLength databitlen); | - | ||||||||||||
| 66 | typedef HashReturn (SHA3Final)(hashState *state, BitSequence *hashval); | - | ||||||||||||
| 67 | - | |||||||||||||
| 68 | #if Q_PROCESSOR_WORDSIZE == 8 // 64 bit version | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | #include "../../3rdparty/sha3/KeccakF-1600-opt64.c" | - | ||||||||||||
| 71 | - | |||||||||||||
| 72 | static SHA3Init * const sha3Init = Init; | - | ||||||||||||
| 73 | static SHA3Update * const sha3Update = Update; | - | ||||||||||||
| 74 | static SHA3Final * const sha3Final = Final; | - | ||||||||||||
| 75 | - | |||||||||||||
| 76 | #else // 32 bit optimised fallback | - | ||||||||||||
| 77 | - | |||||||||||||
| 78 | #include "../../3rdparty/sha3/KeccakF-1600-opt32.c" | - | ||||||||||||
| 79 | - | |||||||||||||
| 80 | static SHA3Init * const sha3Init = Init; | - | ||||||||||||
| 81 | static SHA3Update * const sha3Update = Update; | - | ||||||||||||
| 82 | static SHA3Final * const sha3Final = Final; | - | ||||||||||||
| 83 | - | |||||||||||||
| 84 | #endif | - | ||||||||||||
| 85 | - | |||||||||||||
| 86 | /* | - | ||||||||||||
| 87 | These #defines replace the typedefs needed by the RFC6234 code. Normally | - | ||||||||||||
| 88 | the typedefs would come from from stdint.h, but since this header is not | - | ||||||||||||
| 89 | available on all platforms (MSVC 2008, for example), we #define them to the | - | ||||||||||||
| 90 | Qt equivalents. | - | ||||||||||||
| 91 | */ | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | #ifdef uint64_t | - | ||||||||||||
| 94 | #undef uint64_t | - | ||||||||||||
| 95 | #endif | - | ||||||||||||
| 96 | - | |||||||||||||
| 97 | #define uint64_t QT_PREPEND_NAMESPACE(quint64) | - | ||||||||||||
| 98 | - | |||||||||||||
| 99 | #ifdef uint32_t | - | ||||||||||||
| 100 | #undef uint32_t | - | ||||||||||||
| 101 | #endif | - | ||||||||||||
| 102 | - | |||||||||||||
| 103 | #define uint32_t QT_PREPEND_NAMESPACE(quint32) | - | ||||||||||||
| 104 | - | |||||||||||||
| 105 | #ifdef uint8_t | - | ||||||||||||
| 106 | #undef uint8_t | - | ||||||||||||
| 107 | #endif | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | #define uint8_t QT_PREPEND_NAMESPACE(quint8) | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | #ifdef int_least16_t | - | ||||||||||||
| 112 | #undef int_least16_t | - | ||||||||||||
| 113 | #endif | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | #define int_least16_t QT_PREPEND_NAMESPACE(qint16) | - | ||||||||||||
| 116 | - | |||||||||||||
| 117 | // Header from rfc6234 with 1 modification: | - | ||||||||||||
| 118 | // sha1.h - commented out '#include <stdint.h>' on line 74 | - | ||||||||||||
| 119 | #include "../../3rdparty/rfc6234/sha.h" | - | ||||||||||||
| 120 | - | |||||||||||||
| 121 | /* | - | ||||||||||||
| 122 | These 2 functions replace macros of the same name in sha224-256.c and | - | ||||||||||||
| 123 | sha384-512.c. Originally, these macros relied on a global static 'addTemp' | - | ||||||||||||
| 124 | variable. We do not want this for 2 reasons: | - | ||||||||||||
| 125 | - | |||||||||||||
| 126 | 1. since we are including the sources directly, the declaration of the 2 conflict | - | ||||||||||||
| 127 | - | |||||||||||||
| 128 | 2. static variables are not thread-safe, we do not want multiple threads | - | ||||||||||||
| 129 | computing a hash to corrupt one another | - | ||||||||||||
| 130 | */ | - | ||||||||||||
| 131 | static int SHA224_256AddLength(SHA256Context *context, unsigned int length); | - | ||||||||||||
| 132 | static int SHA384_512AddLength(SHA512Context *context, unsigned int length); | - | ||||||||||||
| 133 | - | |||||||||||||
| 134 | // Sources from rfc6234, with 4 modifications: | - | ||||||||||||
| 135 | // sha224-256.c - commented out 'static uint32_t addTemp;' on line 68 | - | ||||||||||||
| 136 | // sha224-256.c - appended 'M' to the SHA224_256AddLength macro on line 70 | - | ||||||||||||
| 137 | #include "../../3rdparty/rfc6234/sha224-256.c" | - | ||||||||||||
| 138 | // sha384-512.c - commented out 'static uint64_t addTemp;' on line 302 | - | ||||||||||||
| 139 | // sha384-512.c - appended 'M' to the SHA224_256AddLength macro on line 304 | - | ||||||||||||
| 140 | #include "../../3rdparty/rfc6234/sha384-512.c" | - | ||||||||||||
| 141 | - | |||||||||||||
| 142 | #undef uint64_t | - | ||||||||||||
| 143 | #undef uint32_t | - | ||||||||||||
| 144 | #undef uint68_t | - | ||||||||||||
| 145 | #undef int_least16_t | - | ||||||||||||
| 146 | - | |||||||||||||
| 147 | static inline int SHA224_256AddLength(SHA256Context *context, unsigned int length) | - | ||||||||||||
| 148 | { | - | ||||||||||||
| 149 | QT_PREPEND_NAMESPACE(quint32) addTemp; | - | ||||||||||||
| 150 | return SHA224_256AddLengthM(context, length); executed 750 times by 2 tests: return (addTemp = (context)->Length_Low, (context)->Corrupted = (((context)->Length_Low += (length)) < addTemp) && (++(context)->Length_High == 0) ? shaInputTooLong : (context)->Corrupted );Executed by:
| 0-750 | ||||||||||||
| 151 | } | - | ||||||||||||
| 152 | static inline int SHA384_512AddLength(SHA512Context *context, unsigned int length) | - | ||||||||||||
| 153 | { | - | ||||||||||||
| 154 | QT_PREPEND_NAMESPACE(quint64) addTemp; | - | ||||||||||||
| 155 | return SHA384_512AddLengthM(context, length); executed 24 times by 1 test: return (addTemp = context->Length_Low, context->Corrupted = ((context->Length_Low += length) < addTemp) && (++context->Length_High == 0) ? shaInputTooLong : (context)->Corrupted);Executed by:
| 0-24 | ||||||||||||
| 156 | } | - | ||||||||||||
| 157 | #endif // QT_CRYPTOGRAPHICHASH_ONLY_SHA1 | - | ||||||||||||
| 158 | - | |||||||||||||
| 159 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 160 | - | |||||||||||||
| 161 | class QCryptographicHashPrivate | - | ||||||||||||
| 162 | { | - | ||||||||||||
| 163 | public: | - | ||||||||||||
| 164 | QCryptographicHash::Algorithm method; | - | ||||||||||||
| 165 | union { | - | ||||||||||||
| 166 | Sha1State sha1Context; | - | ||||||||||||
| 167 | #ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 | - | ||||||||||||
| 168 | MD5Context md5Context; | - | ||||||||||||
| 169 | md4_context md4Context; | - | ||||||||||||
| 170 | SHA224Context sha224Context; | - | ||||||||||||
| 171 | SHA256Context sha256Context; | - | ||||||||||||
| 172 | SHA384Context sha384Context; | - | ||||||||||||
| 173 | SHA512Context sha512Context; | - | ||||||||||||
| 174 | SHA3Context sha3Context; | - | ||||||||||||
| 175 | #endif | - | ||||||||||||
| 176 | }; | - | ||||||||||||
| 177 | QByteArray result; | - | ||||||||||||
| 178 | }; | - | ||||||||||||
| 179 | - | |||||||||||||
| 180 | /*! | - | ||||||||||||
| 181 | \class QCryptographicHash | - | ||||||||||||
| 182 | \inmodule QtCore | - | ||||||||||||
| 183 | - | |||||||||||||
| 184 | \brief The QCryptographicHash class provides a way to generate cryptographic hashes. | - | ||||||||||||
| 185 | - | |||||||||||||
| 186 | \since 4.3 | - | ||||||||||||
| 187 | - | |||||||||||||
| 188 | \ingroup tools | - | ||||||||||||
| 189 | \reentrant | - | ||||||||||||
| 190 | - | |||||||||||||
| 191 | QCryptographicHash can be used to generate cryptographic hashes of binary or text data. | - | ||||||||||||
| 192 | - | |||||||||||||
| 193 | Refer to the documentation of the \l QCryptographicHash::Algorithm enum for a | - | ||||||||||||
| 194 | list of the supported algorithms. | - | ||||||||||||
| 195 | */ | - | ||||||||||||
| 196 | - | |||||||||||||
| 197 | /*! | - | ||||||||||||
| 198 | \enum QCryptographicHash::Algorithm | - | ||||||||||||
| 199 | - | |||||||||||||
| 200 | \value Md4 Generate an MD4 hash sum | - | ||||||||||||
| 201 | \value Md5 Generate an MD5 hash sum | - | ||||||||||||
| 202 | \value Sha1 Generate an SHA-1 hash sum | - | ||||||||||||
| 203 | \value Sha224 Generate an SHA-224 hash sum (SHA-2). Introduced in Qt 5.0 | - | ||||||||||||
| 204 | \value Sha256 Generate an SHA-256 hash sum (SHA-2). Introduced in Qt 5.0 | - | ||||||||||||
| 205 | \value Sha384 Generate an SHA-384 hash sum (SHA-2). Introduced in Qt 5.0 | - | ||||||||||||
| 206 | \value Sha512 Generate an SHA-512 hash sum (SHA-2). Introduced in Qt 5.0 | - | ||||||||||||
| 207 | \value Sha3_224 Generate an SHA3-224 hash sum. Introduced in Qt 5.1 | - | ||||||||||||
| 208 | \value Sha3_256 Generate an SHA3-256 hash sum. Introduced in Qt 5.1 | - | ||||||||||||
| 209 | \value Sha3_384 Generate an SHA3-384 hash sum. Introduced in Qt 5.1 | - | ||||||||||||
| 210 | \value Sha3_512 Generate an SHA3-512 hash sum. Introduced in Qt 5.1 | - | ||||||||||||
| 211 | */ | - | ||||||||||||
| 212 | - | |||||||||||||
| 213 | /*! | - | ||||||||||||
| 214 | Constructs an object that can be used to create a cryptographic hash from data using \a method. | - | ||||||||||||
| 215 | */ | - | ||||||||||||
| 216 | QCryptographicHash::QCryptographicHash(Algorithm method) | - | ||||||||||||
| 217 | : d(new QCryptographicHashPrivate) | - | ||||||||||||
| 218 | { | - | ||||||||||||
| 219 | d->method = method; | - | ||||||||||||
| 220 | reset(); | - | ||||||||||||
| 221 | } executed 10727 times by 24 tests: end of blockExecuted by:
| 10727 | ||||||||||||
| 222 | - | |||||||||||||
| 223 | /*! | - | ||||||||||||
| 224 | Destroys the object. | - | ||||||||||||
| 225 | */ | - | ||||||||||||
| 226 | QCryptographicHash::~QCryptographicHash() | - | ||||||||||||
| 227 | { | - | ||||||||||||
| 228 | delete d; | - | ||||||||||||
| 229 | } executed 10727 times by 24 tests: end of blockExecuted by:
| 10727 | ||||||||||||
| 230 | - | |||||||||||||
| 231 | /*! | - | ||||||||||||
| 232 | Resets the object. | - | ||||||||||||
| 233 | */ | - | ||||||||||||
| 234 | void QCryptographicHash::reset() | - | ||||||||||||
| 235 | { | - | ||||||||||||
| 236 | switch (d->method) { | - | ||||||||||||
| 237 | case Sha1: executed 7726 times by 12 tests: case Sha1:Executed by:
| 7726 | ||||||||||||
| 238 | sha1InitState(&d->sha1Context); | - | ||||||||||||
| 239 | break; executed 7726 times by 12 tests: break;Executed by:
| 7726 | ||||||||||||
| 240 | #ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 | - | ||||||||||||
| 241 | default: | - | ||||||||||||
| 242 | Q_ASSERT_X(false, "QCryptographicHash", "Method not compiled in"); | - | ||||||||||||
| 243 | Q_UNREACHABLE(); | - | ||||||||||||
| 244 | break; | - | ||||||||||||
| 245 | #else | - | ||||||||||||
| 246 | case Md4: executed 8 times by 2 tests: case Md4:Executed by:
| 8 | ||||||||||||
| 247 | md4_init(&d->md4Context); | - | ||||||||||||
| 248 | break; executed 8 times by 2 tests: break;Executed by:
| 8 | ||||||||||||
| 249 | case Md5: executed 3075 times by 19 tests: case Md5:Executed by:
| 3075 | ||||||||||||
| 250 | MD5Init(&d->md5Context); | - | ||||||||||||
| 251 | break; executed 3075 times by 19 tests: break;Executed by:
| 3075 | ||||||||||||
| 252 | case Sha224: executed 4 times by 1 test: case Sha224:Executed by:
| 4 | ||||||||||||
| 253 | SHA224Reset(&d->sha224Context); | - | ||||||||||||
| 254 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 255 | case Sha256: executed 16 times by 2 tests: case Sha256:Executed by:
| 16 | ||||||||||||
| 256 | SHA256Reset(&d->sha256Context); | - | ||||||||||||
| 257 | break; executed 16 times by 2 tests: break;Executed by:
| 16 | ||||||||||||
| 258 | case Sha384: executed 4 times by 1 test: case Sha384:Executed by:
| 4 | ||||||||||||
| 259 | SHA384Reset(&d->sha384Context); | - | ||||||||||||
| 260 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 261 | case Sha512: executed 4 times by 1 test: case Sha512:Executed by:
| 4 | ||||||||||||
| 262 | SHA512Reset(&d->sha512Context); | - | ||||||||||||
| 263 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 264 | case Sha3_224: executed 6 times by 1 test: case Sha3_224:Executed by:
| 6 | ||||||||||||
| 265 | sha3Init(&d->sha3Context, 224); | - | ||||||||||||
| 266 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 267 | case Sha3_256: executed 6 times by 1 test: case Sha3_256:Executed by:
| 6 | ||||||||||||
| 268 | sha3Init(&d->sha3Context, 256); | - | ||||||||||||
| 269 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 270 | case Sha3_384: executed 6 times by 1 test: case Sha3_384:Executed by:
| 6 | ||||||||||||
| 271 | sha3Init(&d->sha3Context, 384); | - | ||||||||||||
| 272 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 273 | case Sha3_512: executed 6 times by 1 test: case Sha3_512:Executed by:
| 6 | ||||||||||||
| 274 | sha3Init(&d->sha3Context, 512); | - | ||||||||||||
| 275 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 276 | #endif | - | ||||||||||||
| 277 | } | - | ||||||||||||
| 278 | d->result.clear(); | - | ||||||||||||
| 279 | } executed 10861 times by 24 tests: end of blockExecuted by:
| 10861 | ||||||||||||
| 280 | - | |||||||||||||
| 281 | /*! | - | ||||||||||||
| 282 | Adds the first \a length chars of \a data to the cryptographic | - | ||||||||||||
| 283 | hash. | - | ||||||||||||
| 284 | */ | - | ||||||||||||
| 285 | void QCryptographicHash::addData(const char *data, int length) | - | ||||||||||||
| 286 | { | - | ||||||||||||
| 287 | switch (d->method) { | - | ||||||||||||
| 288 | case Sha1: executed 7734 times by 12 tests: case Sha1:Executed by:
| 7734 | ||||||||||||
| 289 | sha1Update(&d->sha1Context, (const unsigned char *)data, length); | - | ||||||||||||
| 290 | break; executed 7734 times by 12 tests: break;Executed by:
| 7734 | ||||||||||||
| 291 | #ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 | - | ||||||||||||
| 292 | default: | - | ||||||||||||
| 293 | Q_ASSERT_X(false, "QCryptographicHash", "Method not compiled in"); | - | ||||||||||||
| 294 | Q_UNREACHABLE(); | - | ||||||||||||
| 295 | break; | - | ||||||||||||
| 296 | #else | - | ||||||||||||
| 297 | case Md4: executed 8 times by 2 tests: case Md4:Executed by:
| 8 | ||||||||||||
| 298 | md4_update(&d->md4Context, (const unsigned char *)data, length); | - | ||||||||||||
| 299 | break; executed 8 times by 2 tests: break;Executed by:
| 8 | ||||||||||||
| 300 | case Md5: executed 3643 times by 19 tests: case Md5:Executed by:
| 3643 | ||||||||||||
| 301 | MD5Update(&d->md5Context, (const unsigned char *)data, length); | - | ||||||||||||
| 302 | break; executed 3643 times by 19 tests: break;Executed by:
| 3643 | ||||||||||||
| 303 | case Sha224: executed 4 times by 1 test: case Sha224:Executed by:
| 4 | ||||||||||||
| 304 | SHA224Input(&d->sha224Context, reinterpret_cast<const unsigned char *>(data), length); | - | ||||||||||||
| 305 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 306 | case Sha256: executed 22 times by 2 tests: case Sha256:Executed by:
| 22 | ||||||||||||
| 307 | SHA256Input(&d->sha256Context, reinterpret_cast<const unsigned char *>(data), length); | - | ||||||||||||
| 308 | break; executed 22 times by 2 tests: break;Executed by:
| 22 | ||||||||||||
| 309 | case Sha384: executed 4 times by 1 test: case Sha384:Executed by:
| 4 | ||||||||||||
| 310 | SHA384Input(&d->sha384Context, reinterpret_cast<const unsigned char *>(data), length); | - | ||||||||||||
| 311 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 312 | case Sha512: executed 4 times by 1 test: case Sha512:Executed by:
| 4 | ||||||||||||
| 313 | SHA512Input(&d->sha512Context, reinterpret_cast<const unsigned char *>(data), length); | - | ||||||||||||
| 314 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 315 | case Sha3_224: executed 6 times by 1 test: case Sha3_224:Executed by:
| 6 | ||||||||||||
| 316 | sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8); | - | ||||||||||||
| 317 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 318 | case Sha3_256: executed 6 times by 1 test: case Sha3_256:Executed by:
| 6 | ||||||||||||
| 319 | sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8); | - | ||||||||||||
| 320 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 321 | case Sha3_384: executed 6 times by 1 test: case Sha3_384:Executed by:
| 6 | ||||||||||||
| 322 | sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8); | - | ||||||||||||
| 323 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 324 | case Sha3_512: executed 6 times by 1 test: case Sha3_512:Executed by:
| 6 | ||||||||||||
| 325 | sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8); | - | ||||||||||||
| 326 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 327 | #endif | - | ||||||||||||
| 328 | } | - | ||||||||||||
| 329 | d->result.clear(); | - | ||||||||||||
| 330 | } executed 11443 times by 24 tests: end of blockExecuted by:
| 11443 | ||||||||||||
| 331 | - | |||||||||||||
| 332 | /*! | - | ||||||||||||
| 333 | \overload addData() | - | ||||||||||||
| 334 | */ | - | ||||||||||||
| 335 | void QCryptographicHash::addData(const QByteArray &data) | - | ||||||||||||
| 336 | { | - | ||||||||||||
| 337 | addData(data.constData(), data.length()); | - | ||||||||||||
| 338 | } executed 11114 times by 24 tests: end of blockExecuted by:
| 11114 | ||||||||||||
| 339 | - | |||||||||||||
| 340 | /*! | - | ||||||||||||
| 341 | Reads the data from the open QIODevice \a device until it ends | - | ||||||||||||
| 342 | and hashes it. Returns \c true if reading was successful. | - | ||||||||||||
| 343 | \since 5.0 | - | ||||||||||||
| 344 | */ | - | ||||||||||||
| 345 | bool QCryptographicHash::addData(QIODevice* device) | - | ||||||||||||
| 346 | { | - | ||||||||||||
| 347 | if (!device->isReadable())
| 2 | ||||||||||||
| 348 | return false; executed 2 times by 1 test: return false;Executed by:
| 2 | ||||||||||||
| 349 | - | |||||||||||||
| 350 | if (!device->isOpen())
| 0-2 | ||||||||||||
| 351 | return false; never executed: return false; | 0 | ||||||||||||
| 352 | - | |||||||||||||
| 353 | char buffer[1024]; | - | ||||||||||||
| 354 | int length; | - | ||||||||||||
| 355 | - | |||||||||||||
| 356 | while ((length = device->read(buffer,sizeof(buffer))) > 0)
| 2-9 | ||||||||||||
| 357 | addData(buffer,length); executed 9 times by 1 test: addData(buffer,length);Executed by:
| 9 | ||||||||||||
| 358 | - | |||||||||||||
| 359 | return device->atEnd(); executed 2 times by 1 test: return device->atEnd();Executed by:
| 2 | ||||||||||||
| 360 | } | - | ||||||||||||
| 361 | - | |||||||||||||
| 362 | - | |||||||||||||
| 363 | /*! | - | ||||||||||||
| 364 | Returns the final hash value. | - | ||||||||||||
| 365 | - | |||||||||||||
| 366 | \sa QByteArray::toHex() | - | ||||||||||||
| 367 | */ | - | ||||||||||||
| 368 | QByteArray QCryptographicHash::result() const | - | ||||||||||||
| 369 | { | - | ||||||||||||
| 370 | if (!d->result.isEmpty())
| 22-10827 | ||||||||||||
| 371 | return d->result; executed 22 times by 1 test: return d->result;Executed by:
| 22 | ||||||||||||
| 372 | - | |||||||||||||
| 373 | switch (d->method) { | - | ||||||||||||
| 374 | case Sha1: { executed 7722 times by 12 tests: case Sha1:Executed by:
| 7722 | ||||||||||||
| 375 | Sha1State copy = d->sha1Context; | - | ||||||||||||
| 376 | d->result.resize(20); | - | ||||||||||||
| 377 | sha1FinalizeState(©); | - | ||||||||||||
| 378 | sha1ToHash(©, (unsigned char *)d->result.data()); | - | ||||||||||||
| 379 | break; executed 7722 times by 12 tests: break;Executed by:
| 7722 | ||||||||||||
| 380 | } | - | ||||||||||||
| 381 | #ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 | - | ||||||||||||
| 382 | default: | - | ||||||||||||
| 383 | Q_ASSERT_X(false, "QCryptographicHash", "Method not compiled in"); | - | ||||||||||||
| 384 | Q_UNREACHABLE(); | - | ||||||||||||
| 385 | break; | - | ||||||||||||
| 386 | #else | - | ||||||||||||
| 387 | case Md4: { executed 8 times by 2 tests: case Md4:Executed by:
| 8 | ||||||||||||
| 388 | md4_context copy = d->md4Context; | - | ||||||||||||
| 389 | d->result.resize(MD4_RESULTLEN); | - | ||||||||||||
| 390 | md4_final(©, (unsigned char *)d->result.data()); | - | ||||||||||||
| 391 | break; executed 8 times by 2 tests: break;Executed by:
| 8 | ||||||||||||
| 392 | } | - | ||||||||||||
| 393 | case Md5: { executed 3049 times by 19 tests: case Md5:Executed by:
| 3049 | ||||||||||||
| 394 | MD5Context copy = d->md5Context; | - | ||||||||||||
| 395 | d->result.resize(16); | - | ||||||||||||
| 396 | MD5Final(©, (unsigned char *)d->result.data()); | - | ||||||||||||
| 397 | break; executed 3049 times by 19 tests: break;Executed by:
| 3049 | ||||||||||||
| 398 | } | - | ||||||||||||
| 399 | case Sha224: { executed 4 times by 1 test: case Sha224:Executed by:
| 4 | ||||||||||||
| 400 | SHA224Context copy = d->sha224Context; | - | ||||||||||||
| 401 | d->result.resize(SHA224HashSize); | - | ||||||||||||
| 402 | SHA224Result(©, reinterpret_cast<unsigned char *>(d->result.data())); | - | ||||||||||||
| 403 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 404 | } | - | ||||||||||||
| 405 | case Sha256:{ executed 12 times by 2 tests: case Sha256:Executed by:
| 12 | ||||||||||||
| 406 | SHA256Context copy = d->sha256Context; | - | ||||||||||||
| 407 | d->result.resize(SHA256HashSize); | - | ||||||||||||
| 408 | SHA256Result(©, reinterpret_cast<unsigned char *>(d->result.data())); | - | ||||||||||||
| 409 | break; executed 12 times by 2 tests: break;Executed by:
| 12 | ||||||||||||
| 410 | } | - | ||||||||||||
| 411 | case Sha384:{ executed 4 times by 1 test: case Sha384:Executed by:
| 4 | ||||||||||||
| 412 | SHA384Context copy = d->sha384Context; | - | ||||||||||||
| 413 | d->result.resize(SHA384HashSize); | - | ||||||||||||
| 414 | SHA384Result(©, reinterpret_cast<unsigned char *>(d->result.data())); | - | ||||||||||||
| 415 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 416 | } | - | ||||||||||||
| 417 | case Sha512:{ executed 4 times by 1 test: case Sha512:Executed by:
| 4 | ||||||||||||
| 418 | SHA512Context copy = d->sha512Context; | - | ||||||||||||
| 419 | d->result.resize(SHA512HashSize); | - | ||||||||||||
| 420 | SHA512Result(©, reinterpret_cast<unsigned char *>(d->result.data())); | - | ||||||||||||
| 421 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 422 | } | - | ||||||||||||
| 423 | case Sha3_224: { executed 6 times by 1 test: case Sha3_224:Executed by:
| 6 | ||||||||||||
| 424 | SHA3Context copy = d->sha3Context; | - | ||||||||||||
| 425 | d->result.resize(224/8); | - | ||||||||||||
| 426 | sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); | - | ||||||||||||
| 427 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 428 | } | - | ||||||||||||
| 429 | case Sha3_256: { executed 6 times by 1 test: case Sha3_256:Executed by:
| 6 | ||||||||||||
| 430 | SHA3Context copy = d->sha3Context; | - | ||||||||||||
| 431 | d->result.resize(256/8); | - | ||||||||||||
| 432 | sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); | - | ||||||||||||
| 433 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 434 | } | - | ||||||||||||
| 435 | case Sha3_384: { executed 6 times by 1 test: case Sha3_384:Executed by:
| 6 | ||||||||||||
| 436 | SHA3Context copy = d->sha3Context; | - | ||||||||||||
| 437 | d->result.resize(384/8); | - | ||||||||||||
| 438 | sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); | - | ||||||||||||
| 439 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 440 | } | - | ||||||||||||
| 441 | case Sha3_512: { executed 6 times by 1 test: case Sha3_512:Executed by:
| 6 | ||||||||||||
| 442 | SHA3Context copy = d->sha3Context; | - | ||||||||||||
| 443 | d->result.resize(512/8); | - | ||||||||||||
| 444 | sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); | - | ||||||||||||
| 445 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||||||||
| 446 | } | - | ||||||||||||
| 447 | #endif | - | ||||||||||||
| 448 | } | - | ||||||||||||
| 449 | return d->result; executed 10827 times by 24 tests: return d->result;Executed by:
| 10827 | ||||||||||||
| 450 | } | - | ||||||||||||
| 451 | - | |||||||||||||
| 452 | /*! | - | ||||||||||||
| 453 | Returns the hash of \a data using \a method. | - | ||||||||||||
| 454 | */ | - | ||||||||||||
| 455 | QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method) | - | ||||||||||||
| 456 | { | - | ||||||||||||
| 457 | QCryptographicHash hash(method); | - | ||||||||||||
| 458 | hash.addData(data); | - | ||||||||||||
| 459 | return hash.result(); executed 10306 times by 22 tests: return hash.result();Executed by:
| 10306 | ||||||||||||
| 460 | } | - | ||||||||||||
| 461 | - | |||||||||||||
| 462 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |