1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <qcryptographichash.h>
45 #define _MD5_H_ // Needed to disable system header
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 #include <qiodevice.h>
58 class QCryptographicHashPrivate
61 QCryptographicHash::Algorithm method;
63 MD5Context md5Context;
64 md4_context md4Context;
65 Sha1State sha1Context;
71 \class QCryptographicHash
73 \brief The QCryptographicHash class provides a way to generate cryptographic hashes.
80 QCryptographicHash can be used to generate cryptographic hashes of binary or text data.
82 Currently MD4, MD5, and SHA-1 are supported.
86 \enum QCryptographicHash::Algorithm
88 \value Md4 Generate an MD4 hash sum
89 \value Md5 Generate an MD5 hash sum
90 \value Sha1 Generate an SHA1 hash sum
94 Constructs an object that can be used to create a cryptographic hash from data using \a method.
96 QCryptographicHash::QCryptographicHash(Algorithm method)
97 : d(new QCryptographicHashPrivate)
106 QCryptographicHash::~QCryptographicHash()
114 void QCryptographicHash::reset()
118 md4_init(&d->md4Context);
121 MD5Init(&d->md5Context);
124 sha1InitState(&d->sha1Context);
131 Adds the first \a length chars of \a data to the cryptographic
134 void QCryptographicHash::addData(const char *data, int length)
138 md4_update(&d->md4Context, (const unsigned char *)data, length);
141 MD5Update(&d->md5Context, (const unsigned char *)data, length);
144 sha1Update(&d->sha1Context, (const unsigned char *)data, length);
153 void QCryptographicHash::addData(const QByteArray &data)
155 addData(data.constData(), data.length());
159 Reads the data from the open QIODevice \a device until it ends
160 and hashes it. Returns true if reading was successful.
162 bool QCryptographicHash::addData(QIODevice* device)
164 if (!device->isReadable())
167 if (!device->isOpen())
173 while ((length = device->read(buffer,sizeof(buffer))) > 0)
174 addData(buffer,length);
176 return device->atEnd();
181 Returns the final hash value.
183 \sa QByteArray::toHex()
185 QByteArray QCryptographicHash::result() const
187 if (!d->result.isEmpty())
192 md4_context copy = d->md4Context;
193 d->result.resize(MD4_RESULTLEN);
194 md4_final(©, (unsigned char *)d->result.data());
198 MD5Context copy = d->md5Context;
199 d->result.resize(16);
200 MD5Final(©, (unsigned char *)d->result.data());
204 Sha1State copy = d->sha1Context;
205 d->result.resize(20);
206 sha1FinalizeState(©);
207 sha1ToHash(©, (unsigned char *)d->result.data());
214 Returns the hash of \a data using \a method.
216 QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
218 QCryptographicHash hash(method);
220 return hash.result();