1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <qcryptographichash.h>
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"
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
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)
61 // Header from rfc6234 with 1 modification:
62 // sha1.h - commented out '#include <stdint.h>' on line 74
63 #include "../../3rdparty/rfc6234/sha.h"
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:
70 1. since we are including the sources directly, the declaration of the 2 conflict
72 2. static variables are not thread-safe, we do not want multiple threads
73 computing a hash to corrupt one another
75 static int SHA224_256AddLength(SHA256Context *context, unsigned int length);
76 static int SHA384_512AddLength(SHA512Context *context, unsigned int length);
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"
91 #include <qiodevice.h>
93 static inline int SHA224_256AddLength(SHA256Context *context, unsigned int length)
95 QT_PREPEND_NAMESPACE(quint32) addTemp;
96 return SHA224_256AddLengthM(context, length);
98 static inline int SHA384_512AddLength(SHA512Context *context, unsigned int length)
100 QT_PREPEND_NAMESPACE(quint64) addTemp;
101 return SHA384_512AddLengthM(context, length);
106 class QCryptographicHashPrivate
109 QCryptographicHash::Algorithm method;
111 MD5Context md5Context;
112 md4_context md4Context;
113 Sha1State sha1Context;
114 SHA224Context sha224Context;
115 SHA256Context sha256Context;
116 SHA384Context sha384Context;
117 SHA512Context sha512Context;
123 \class QCryptographicHash
125 \brief The QCryptographicHash class provides a way to generate cryptographic hashes.
132 QCryptographicHash can be used to generate cryptographic hashes of binary or text data.
134 Currently MD4, MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 are supported.
138 \enum QCryptographicHash::Algorithm
140 \value Md4 Generate an MD4 hash sum
141 \value Md5 Generate an MD5 hash sum
142 \value Sha1 Generate an SHA-1 hash sum
143 \value Sha224 Generate an SHA-224 hash sum
144 \value Sha256 Generate an SHA-256 hash sum
145 \value Sha384 Generate an SHA-384 hash sum
146 \value Sha512 Generate an SHA-512 hash sum
150 Constructs an object that can be used to create a cryptographic hash from data using \a method.
152 QCryptographicHash::QCryptographicHash(Algorithm method)
153 : d(new QCryptographicHashPrivate)
162 QCryptographicHash::~QCryptographicHash()
170 void QCryptographicHash::reset()
174 md4_init(&d->md4Context);
177 MD5Init(&d->md5Context);
180 sha1InitState(&d->sha1Context);
183 SHA224Reset(&d->sha224Context);
186 SHA256Reset(&d->sha256Context);
189 SHA384Reset(&d->sha384Context);
192 SHA512Reset(&d->sha512Context);
199 Adds the first \a length chars of \a data to the cryptographic
202 void QCryptographicHash::addData(const char *data, int length)
206 md4_update(&d->md4Context, (const unsigned char *)data, length);
209 MD5Update(&d->md5Context, (const unsigned char *)data, length);
212 sha1Update(&d->sha1Context, (const unsigned char *)data, length);
215 SHA224Input(&d->sha224Context, reinterpret_cast<const unsigned char *>(data), length);
218 SHA256Input(&d->sha256Context, reinterpret_cast<const unsigned char *>(data), length);
221 SHA384Input(&d->sha384Context, reinterpret_cast<const unsigned char *>(data), length);
224 SHA512Input(&d->sha512Context, reinterpret_cast<const unsigned char *>(data), length);
233 void QCryptographicHash::addData(const QByteArray &data)
235 addData(data.constData(), data.length());
239 Reads the data from the open QIODevice \a device until it ends
240 and hashes it. Returns true if reading was successful.
242 bool QCryptographicHash::addData(QIODevice* device)
244 if (!device->isReadable())
247 if (!device->isOpen())
253 while ((length = device->read(buffer,sizeof(buffer))) > 0)
254 addData(buffer,length);
256 return device->atEnd();
261 Returns the final hash value.
263 \sa QByteArray::toHex()
265 QByteArray QCryptographicHash::result() const
267 if (!d->result.isEmpty())
272 md4_context copy = d->md4Context;
273 d->result.resize(MD4_RESULTLEN);
274 md4_final(©, (unsigned char *)d->result.data());
278 MD5Context copy = d->md5Context;
279 d->result.resize(16);
280 MD5Final(©, (unsigned char *)d->result.data());
284 Sha1State copy = d->sha1Context;
285 d->result.resize(20);
286 sha1FinalizeState(©);
287 sha1ToHash(©, (unsigned char *)d->result.data());
291 SHA224Context copy = d->sha224Context;
292 d->result.resize(SHA224HashSize);
293 SHA224Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
297 SHA256Context copy = d->sha256Context;
298 d->result.resize(SHA256HashSize);
299 SHA256Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
303 SHA384Context copy = d->sha384Context;
304 d->result.resize(SHA384HashSize);
305 SHA384Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
309 SHA512Context copy = d->sha512Context;
310 d->result.resize(SHA512HashSize);
311 SHA512Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
319 Returns the hash of \a data using \a method.
321 QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
323 QCryptographicHash hash(method);
325 return hash.result();