CopperSpice API  1.9.1
QMessageAuthenticationCode Class Reference

Provides a way to generate hash-based message authentication codes. More...

Public Methods

 QMessageAuthenticationCode (QCryptographicHash::Algorithm method, const QByteArray &key=QByteArray ())
 
 ~QMessageAuthenticationCode ()
 
void addData (const char *data, int length)
 
void addData (const QByteArray &data)
 
bool addData (QIODevice *device)
 
void reset ()
 
QByteArray result () const
 
void setKey (const QByteArray &key)
 

Static Public Methods

static QByteArray hash (const QByteArray &message, const QByteArray &key, QCryptographicHash::Algorithm method)
 

Detailed Description

QMessageAuthenticationCode supports all cryptographic hashes which are supported by QCryptographicHash.

To generate a message authentication code pass a hash algorithm to the constructor of QMessageAuthenticationCode then set the key and message using the setKey() and addData() methods. The result() methods returns the message authentication code.

QByteArray key = "key";
QByteArray message = "The quick brown fox jumps over the lazy dog";
QMessageAuthenticationCode code(QCryptographicHash::Sha1);
code.setKey(key);
code.addData(message);
code.result().toHex(); // returns "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"

Alternatively this effect can be achieved by providing message key and method to the hash() method.

QMessageAuthenticationCode::hash(message, key, QCryptographicHash::Sha1).toHex();
See also
QCryptographicHash

Constructor & Destructor Documentation

QMessageAuthenticationCode::QMessageAuthenticationCode ( QCryptographicHash::Algorithm  method,
const QByteArray key = QByteArray() 
)
explicit

Constructs an object which can be used to create a cryptographic hash from data using the given method and key.

QMessageAuthenticationCode::~QMessageAuthenticationCode ( )

Destroys the QMessageAuthenticationCode object.

Method Documentation

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

Adds the first length chars of data to the message.

void QMessageAuthenticationCode::addData ( const QByteArray data)

Adds the data to the message.

bool QMessageAuthenticationCode::addData ( QIODevice device)

Reads data from device until the end of the file is reached. Adds the data read from the file to this message. Returns true if reading was successful. The I/O device must be already opened.

QByteArray QMessageAuthenticationCode::hash ( const QByteArray message,
const QByteArray key,
QCryptographicHash::Algorithm  method 
)
static

Returns the authentication code for the message using the key and the method.

void QMessageAuthenticationCode::reset ( )

Resets message data. Calling this method does not affect the key.

QByteArray QMessageAuthenticationCode::result ( ) const

Returns the final authentication code.

See also
QByteArray::toHex()
void QMessageAuthenticationCode::setKey ( const QByteArray key)

Sets secret key. Calling this method automatically resets the object state.