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"
49 #include <qiodevice.h>
54 class QCryptographicHashPrivate
57 QCryptographicHash::Algorithm method;
59 MD5Context md5Context;
60 md4_context md4Context;
61 Sha1State sha1Context;
67 \class QCryptographicHash
69 \brief The QCryptographicHash class provides a way to generate cryptographic hashes.
76 QCryptographicHash can be used to generate cryptographic hashes of binary or text data.
78 Currently MD4, MD5, and SHA-1 are supported.
82 \enum QCryptographicHash::Algorithm
84 \value Md4 Generate an MD4 hash sum
85 \value Md5 Generate an MD5 hash sum
86 \value Sha1 Generate an SHA1 hash sum
90 Constructs an object that can be used to create a cryptographic hash from data using \a method.
92 QCryptographicHash::QCryptographicHash(Algorithm method)
93 : d(new QCryptographicHashPrivate)
102 QCryptographicHash::~QCryptographicHash()
110 void QCryptographicHash::reset()
114 md4_init(&d->md4Context);
117 MD5Init(&d->md5Context);
120 sha1InitState(&d->sha1Context);
127 Adds the first \a length chars of \a data to the cryptographic
130 void QCryptographicHash::addData(const char *data, int length)
134 md4_update(&d->md4Context, (const unsigned char *)data, length);
137 MD5Update(&d->md5Context, (const unsigned char *)data, length);
140 sha1Update(&d->sha1Context, (const unsigned char *)data, length);
149 void QCryptographicHash::addData(const QByteArray &data)
151 addData(data.constData(), data.length());
155 Reads the data from the open QIODevice \a device until it ends
156 and hashes it. Returns true if reading was successful.
158 bool QCryptographicHash::addData(QIODevice* device)
160 if (!device->isReadable())
163 if (!device->isOpen())
169 while ((length = device->read(buffer,sizeof(buffer))) > 0)
170 addData(buffer,length);
172 return device->atEnd();
177 Returns the final hash value.
179 \sa QByteArray::toHex()
181 QByteArray QCryptographicHash::result() const
183 if (!d->result.isEmpty())
188 md4_context copy = d->md4Context;
189 d->result.resize(MD4_RESULTLEN);
190 md4_final(©, (unsigned char *)d->result.data());
194 MD5Context copy = d->md5Context;
195 d->result.resize(16);
196 MD5Final(©, (unsigned char *)d->result.data());
200 Sha1State copy = d->sha1Context;
201 d->result.resize(20);
202 sha1FinalizeState(©);
203 sha1ToHash(©, (unsigned char *)d->result.data());
210 Returns the hash of \a data using \a method.
212 QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
214 QCryptographicHash hash(method);
216 return hash.result();