• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

qca

QCA::Hash

QCA::Hash Class Reference
[QCA user API]

#include <QtCrypto>

Inheritance diagram for QCA::Hash:

Inheritance graph
[legend]

List of all members.


Detailed Description

General class for hashing algorithms.

Hash is the class for the various hashing algorithms within QCA. SHA256, SHA1 or RIPEMD160 are recommended for new applications, although MD2, MD4, MD5 or SHA0 may be applicable (for interoperability reasons) for some applications.

To perform a hash, you create a Hash object, call update() with the data that needs to be hashed, and then call final(), which returns a QByteArray of the hash result. An example (using the SHA1 hash, with 1000 updates of a 1000 byte string) is shown below:

if(!QCA::isSupported("sha1"))
    printf("SHA1 not supported!\n");
else
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    QCA::Hash shaHash("sha1");
    for (int i=0; i<1000; i++)
        shaHash.update(fillerString);
    QByteArray hashResult = shaHash.final();
    if ( "34aa973cd4c4daa4f61eeb2bdbad27316534016f" == QCA::arrayToHex(hashResult) )
    {
        printf("big SHA1 is OK\n");
    }
    else
    {
        printf("big SHA1 failed\n");
    }
}

If you only have a simple hash requirement - a single string that is fully available in memory at one time - then you may be better off with one of the convenience methods. So, for example, instead of creating a QCA::Hash object, then doing a single update() and the final() call; you could simply call QCA::Hash("algoName").hash() with the data that you would otherwise have provided to the update() call.

For more information on hashing algorithms, see Hashing Algorithms.

Definition at line 205 of file qca_basic.h.


Public Member Functions

virtual void clear ()
virtual MemoryRegion final ()
MemoryRegion hash (const MemoryRegion &array)
 Hash (const Hash &from)
 Hash (const QString &type, const QString &provider=QString())
QString hashToString (const MemoryRegion &array)
Hash & operator= (const Hash &from)
QString type () const
void update (QIODevice *file)
void update (const char *data, int len=-1)
void update (const QByteArray &a)
virtual void update (const MemoryRegion &a)
 ~Hash ()

Static Public Member Functions

static QStringList supportedTypes (const QString &provider=QString())

Constructor & Destructor Documentation

QCA::Hash::Hash ( const QString &  type,
const QString &  provider = QString() 
) [explicit]

Constructor.

Parameters:
type label for the type of hash to be created (for example, "sha1" or "md2")
provider the name of the provider plugin for the subclass (eg "qca-ossl")

Definition at line 172 of file qca_basic.cpp.

QCA::Hash::Hash ( const Hash &  from  ) 

Copy constructor.

Parameters:
from the Hash object to copy from

Definition at line 177 of file qca_basic.cpp.

QCA::Hash::~Hash (  ) 

Definition at line 182 of file qca_basic.cpp.


Member Function Documentation

void QCA::Hash::clear (  )  [virtual]

Reset a hash, dumping all previous parts of the message.

This method clears (or resets) the hash algorithm, effectively undoing any previous update() calls. You should use this call if you are re-using a Hash sub-class object to calculate additional hashes.

Implements QCA::BufferedComputation.

Definition at line 203 of file qca_basic.cpp.

MemoryRegion QCA::Hash::final (  )  [virtual]

Finalises input and returns the hash result.

After calling update() with the required data, the hash results are finalised and produced.

Note that it is not possible to add further data (with update()) after calling final(), because of the way the hashing works - null bytes are inserted to pad the results up to a fixed size. If you want to reuse the Hash object, you should call clear() and start to update() again.

Implements QCA::BufferedComputation.

Definition at line 238 of file qca_basic.cpp.

MemoryRegion QCA::Hash::hash ( const MemoryRegion &  array  ) 

Hash a byte array, returning it as another byte array

This is a convenience method that returns the hash of a SecureArray.

SecureArray sampleArray(3);
sampleArray.fill('a');
SecureArray outputArray = QCA::Hash("md2")::hash(sampleArray);

Parameters:
array the QByteArray to hash
If you need more flexibility (e.g. you are constructing a large byte array object just to pass it to hash(), then consider creating an Hash object, and then calling update() and final().

Definition at line 243 of file qca_basic.cpp.

QString QCA::Hash::hashToString ( const MemoryRegion &  array  ) 

Hash a byte array, returning it as a printable string

This is a convenience method that returns the hash of a QSeecureArray as a hexadecimal representation encoded in a QString.

Parameters:
array the QByteArray to hash
If you need more flexibility, you can create a Hash object, call Hash::update() as required, then call Hash::final(), before using the static arrayToHex() method.

Definition at line 248 of file qca_basic.cpp.

Hash & QCA::Hash::operator= ( const Hash &  from  ) 

Assignment operator.

Parameters:
from the Hash object to copy state from

Definition at line 186 of file qca_basic.cpp.

QStringList QCA::Hash::supportedTypes ( const QString &  provider = QString()  )  [static]

Returns a list of all of the hash types available.

Parameters:
provider the name of the provider to get a list from, if one provider is required. If not specified, available hash types from all providers will be returned.

Definition at line 192 of file qca_basic.cpp.

QString QCA::Hash::type (  )  const

Return the hash type.

Reimplemented from QCA::Algorithm.

Definition at line 197 of file qca_basic.cpp.

void QCA::Hash::update ( QIODevice *  file  ) 

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This allows you to read from a file or other I/O device.

Note that the device must be already open for reading

Parameters:
file an I/O device
If you are trying to calculate the hash of a whole file (and it isn't already open), you might want to use code like this:
QFile f( "file.dat" );
if ( f1.open( IO_ReadOnly ) )
{
    QCA::Hash hashObj("sha1");
    hashObj.update( &f1 );
    QString output = hashObj.final() ) ),
}

Definition at line 229 of file qca_basic.cpp.

void QCA::Hash::update ( const char *  data,
int  len = -1 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This method is provided to assist with code that already exists, and is being ported to QCA.

You are better off passing a SecureArray (as shown above) if you are writing new code.

Parameters:
data pointer to a char array
len the length of the array. If not specified (or specified as a negative number), the length will be determined with strlen(), which may not be what you want if the array contains a null (0x00) character.

Definition at line 218 of file qca_basic.cpp.

void QCA::Hash::update ( const QByteArray &  a  ) 

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters:
a the QByteArray to add to the hash

Definition at line 213 of file qca_basic.cpp.

void QCA::Hash::update ( const MemoryRegion &  a  )  [virtual]

Update a hash, adding more of the message contents to the digest.

The whole message needs to be added using this method before you call final().

If you find yourself only calling update() once, you may be better off using a convenience method such as hash() or hashToString() instead.

Parameters:
a the byte array to add to the hash

Implements QCA::BufferedComputation.

Definition at line 208 of file qca_basic.cpp.


The documentation for this class was generated from the following files:
  • qca_basic.h
  • qca_basic.cpp

qca

Skip menu "qca"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • akonadi
  • Decibel
  • eigen
  • Eigen2
  • kdewin32
  • Phonon
  •     Backend
  • qca
  • qimageblitz
  • soprano
  • strigi
  •     searchclient
  •     streamanalyzer
  •     streams
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal