source: trunk/src/corelib/tools/qcryptographichash.cpp@ 79

Last change on this file since 79 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qcryptographichash.h>
43
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
50
51QT_BEGIN_NAMESPACE
52
53class QCryptographicHashPrivate
54{
55public:
56 QCryptographicHash::Algorithm method;
57 union {
58 MD5Context md5Context;
59 md4_context md4Context;
60 Sha1State sha1Context;
61 };
62 QByteArray result;
63};
64
65/*!
66 \class QCryptographicHash
67
68 \brief The QCryptographicHash class provides a way to generate cryptographic hashes.
69
70 \since 4.3
71
72 \ingroup tools
73 \reentrant
74
75 QCryptographicHash can be used to generate cryptographic hashes of binary or text data.
76
77 Currently MD4, MD5, and SHA1 are supported.
78*/
79
80/*!
81 \enum QCryptographicHash::Algorithm
82
83 \value Md4 Generate an MD4 hash sum
84 \value Md5 Generate an MD5 hash sum
85 \value Sha1 Generate an SHA1 hash sum
86*/
87
88/*!
89 Constructs an object that can be used to create a cryptographic hash from data using \a method.
90*/
91QCryptographicHash::QCryptographicHash(Algorithm method)
92 : d(new QCryptographicHashPrivate)
93{
94 d->method = method;
95 reset();
96}
97
98/*!
99 Destroys the object.
100*/
101QCryptographicHash::~QCryptographicHash()
102{
103 delete d;
104}
105
106/*!
107 Resets the object.
108*/
109void QCryptographicHash::reset()
110{
111 switch (d->method) {
112 case Md4:
113 md4_init(&d->md4Context);
114 break;
115 case Md5:
116 MD5Init(&d->md5Context);
117 break;
118 case Sha1:
119 sha1InitState(&d->sha1Context);
120 break;
121 }
122 d->result.clear();
123}
124
125/*!
126 Adds the first \a length chars of \a data to the cryptographic
127 hash.
128*/
129void QCryptographicHash::addData(const char *data, int length)
130{
131 switch (d->method) {
132 case Md4:
133 md4_update(&d->md4Context, (const unsigned char *)data, length);
134 break;
135 case Md5:
136 MD5Update(&d->md5Context, (const unsigned char *)data, length);
137 break;
138 case Sha1:
139 sha1Update(&d->sha1Context, (const unsigned char *)data, length);
140 break;
141 }
142 d->result.clear();
143}
144
145/*!
146 /overload
147*/
148void QCryptographicHash::addData(const QByteArray &data)
149{
150 addData(data.constData(), data.length());
151}
152
153/*!
154 Returns the final hash value.
155
156 \sa QByteArray::toHex()
157*/
158QByteArray QCryptographicHash::result() const
159{
160 if (!d->result.isEmpty())
161 return d->result;
162
163 switch (d->method) {
164 case Md4: {
165 md4_context copy = d->md4Context;
166 d->result.resize(MD4_RESULTLEN);
167 md4_final(&copy, (unsigned char *)d->result.data());
168 break;
169 }
170 case Md5: {
171 MD5Context copy = d->md5Context;
172 d->result.resize(16);
173 MD5Final(&copy, (unsigned char *)d->result.data());
174 break;
175 }
176 case Sha1: {
177 Sha1State copy = d->sha1Context;
178 d->result.resize(20);
179 sha1FinalizeState(&copy);
180 sha1ToHash(&copy, (unsigned char *)d->result.data());
181 }
182 }
183 return d->result;
184}
185
186/*!
187 Returns the hash of \a data using \a method.
188*/
189QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
190{
191 QCryptographicHash hash(method);
192 hash.addData(data);
193 return hash.result();
194}
195
196QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.