Qt 4.8
qcryptographichash.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qcryptographichash.h>
43 
44 #ifdef Q_OS_SYMBIAN
45 #define _MD5_H_ // Needed to disable system header
46 #endif
47 
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 
54 
56 
58 {
59 public:
61  union {
62  MD5Context md5Context;
63  md4_context md4Context;
64  Sha1State sha1Context;
65  };
67 };
68 
100 {
101  d->method = method;
102  reset();
103 }
104 
109 {
110  delete d;
111 }
112 
117 {
118  switch (d->method) {
119  case Md4:
120  md4_init(&d->md4Context);
121  break;
122  case Md5:
123  MD5Init(&d->md5Context);
124  break;
125  case Sha1:
126  sha1InitState(&d->sha1Context);
127  break;
128  }
129  d->result.clear();
130 }
131 
136 void QCryptographicHash::addData(const char *data, int length)
137 {
138  switch (d->method) {
139  case Md4:
140  md4_update(&d->md4Context, (const unsigned char *)data, length);
141  break;
142  case Md5:
143  MD5Update(&d->md5Context, (const unsigned char *)data, length);
144  break;
145  case Sha1:
146  sha1Update(&d->sha1Context, (const unsigned char *)data, length);
147  break;
148  }
149  d->result.clear();
150 }
151 
156 {
157  addData(data.constData(), data.length());
158 }
159 
166 {
167  if (!d->result.isEmpty())
168  return d->result;
169 
170  switch (d->method) {
171  case Md4: {
172  md4_context copy = d->md4Context;
173  d->result.resize(MD4_RESULTLEN);
174  md4_final(&copy, (unsigned char *)d->result.data());
175  break;
176  }
177  case Md5: {
178  MD5Context copy = d->md5Context;
179  d->result.resize(16);
180  MD5Final(&copy, (unsigned char *)d->result.data());
181  break;
182  }
183  case Sha1: {
184  Sha1State copy = d->sha1Context;
185  d->result.resize(20);
186  sha1FinalizeState(&copy);
187  sha1ToHash(&copy, (unsigned char *)d->result.data());
188  }
189  }
190  return d->result;
191 }
192 
197 {
198  QCryptographicHash hash(method);
199  hash.addData(data);
200  return hash.result();
201 }
202 
QCryptographicHash::Algorithm method
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QCryptographicHash(Algorithm method)
Constructs an object that can be used to create a cryptographic hash from data using method...
QCryptographicHashPrivate * d
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static const char * data(const QByteArray &arr)
int length() const
Same as size().
Definition: qbytearray.h:356
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void addData(const char *data, int length)
Adds the first length chars of data to the cryptographic hash.
void resize(int size)
Sets the size of the byte array to size bytes.
QByteArray result() const
Returns the final hash value.
~QCryptographicHash()
Destroys the object.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static QByteArray hash(const QByteArray &data, Algorithm method)
Returns the hash of data using method.
void clear()
Clears the contents of the byte array and makes it empty.
void reset()
Resets the object.
The QCryptographicHash class provides a way to generate cryptographic hashes.