Qt 4.8
Public Types | Public Functions | Static Public Functions | Properties | List of all members
QCryptographicHash Class Reference

The QCryptographicHash class provides a way to generate cryptographic hashes. More...

#include <qcryptographichash.h>

Public Types

enum  Algorithm { Md4, Md5, Sha1 }
 

Public Functions

void addData (const char *data, int length)
 Adds the first length chars of data to the cryptographic hash. More...
 
void addData (const QByteArray &data)
 
 QCryptographicHash (Algorithm method)
 Constructs an object that can be used to create a cryptographic hash from data using method. More...
 
void reset ()
 Resets the object. More...
 
QByteArray result () const
 Returns the final hash value. More...
 
 ~QCryptographicHash ()
 Destroys the object. More...
 

Static Public Functions

static QByteArray hash (const QByteArray &data, Algorithm method)
 Returns the hash of data using method. More...
 

Properties

QCryptographicHashPrivated
 

Detailed Description

The QCryptographicHash class provides a way to generate cryptographic hashes.

Since
4.3
Note
This class or function is reentrant.

QCryptographicHash can be used to generate cryptographic hashes of binary or text data.

Currently MD4, MD5, and SHA-1 are supported.

Definition at line 55 of file qcryptographichash.h.

Enumerations

◆ Algorithm

  • Md4 Generate an MD4 hash sum
  • Md5 Generate an MD5 hash sum
  • Sha1 Generate an SHA1 hash sum
Enumerator
Md4 
Md5 
Sha1 

Definition at line 58 of file qcryptographichash.h.

Constructors and Destructors

◆ QCryptographicHash()

QCryptographicHash::QCryptographicHash ( Algorithm  method)

Constructs an object that can be used to create a cryptographic hash from data using method.

Definition at line 98 of file qcryptographichash.cpp.

100 {
101  d->method = method;
102  reset();
103 }
QCryptographicHash::Algorithm method
QCryptographicHashPrivate * d
void reset()
Resets the object.

◆ ~QCryptographicHash()

QCryptographicHash::~QCryptographicHash ( )

Destroys the object.

Definition at line 108 of file qcryptographichash.cpp.

109 {
110  delete d;
111 }
QCryptographicHashPrivate * d

Functions

◆ addData() [1/2]

void QCryptographicHash::addData ( const char *  data,
int  length 
)

Adds the first length chars of data to the cryptographic hash.

Definition at line 136 of file qcryptographichash.cpp.

Referenced by addData(), digestMd5ResponseHelper(), hash(), qCreatev2Hash(), qEncodeHmacMd5(), qmlsqldatabase_open_sync(), qStringFromUcs2Le(), and QNetworkDiskCachePrivate::uniqueFileName().

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 }
QCryptographicHash::Algorithm method
QCryptographicHashPrivate * d
static const char * data(const QByteArray &arr)
void clear()
Clears the contents of the byte array and makes it empty.

◆ addData() [2/2]

void QCryptographicHash::addData ( const QByteArray data)

Definition at line 155 of file qcryptographichash.cpp.

156 {
157  addData(data.constData(), data.length());
158 }
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.

◆ hash()

QByteArray QCryptographicHash::hash ( const QByteArray data,
Algorithm  method 
)
static

Returns the hash of data using method.

Definition at line 196 of file qcryptographichash.cpp.

Referenced by QSslCertificate::digest(), QSharedMemoryPrivate::makePlatformSafeKey(), QDeclarativeEnginePrivate::md5(), and QAuthenticatorPrivate::QAuthenticatorPrivate().

197 {
198  QCryptographicHash hash(method);
199  hash.addData(data);
200  return hash.result();
201 }
static QByteArray hash(const QByteArray &data, Algorithm method)
Returns the hash of data using method.
The QCryptographicHash class provides a way to generate cryptographic hashes.

◆ reset()

void QCryptographicHash::reset ( )

Resets the object.

Definition at line 116 of file qcryptographichash.cpp.

Referenced by digestMd5ResponseHelper(), QCryptographicHash(), and qEncodeHmacMd5().

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 }
QCryptographicHash::Algorithm method
QCryptographicHashPrivate * d
void clear()
Clears the contents of the byte array and makes it empty.

◆ result()

QByteArray QCryptographicHash::result ( ) const

Returns the final hash value.

See also
QByteArray::toHex()

Definition at line 165 of file qcryptographichash.cpp.

Referenced by digestMd5ResponseHelper(), hash(), qCreatev2Hash(), qEncodeHmacMd5(), qmlsqldatabase_open_sync(), qStringFromUcs2Le(), and QNetworkDiskCachePrivate::uniqueFileName().

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 }
QCryptographicHash::Algorithm method
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QCryptographicHashPrivate * d
void resize(int size)
Sets the size of the byte array to size bytes.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

Properties

◆ d

QCryptographicHashPrivate* QCryptographicHash::d
private

Definition at line 77 of file qcryptographichash.h.

Referenced by addData(), QCryptographicHash(), reset(), result(), and ~QCryptographicHash().


The documentation for this class was generated from the following files: