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