An adapted C++ implementation of RSA Data Securities MD5 algorithm. More...
#include <mcodecs.h>
Public Types | |
| typedef unsigned char | Digest [16] |
Public Member Functions | |
| QMD5 () | |
| ~QMD5 () | |
| QMD5 (const char *in, int len=-1) | |
| Constructor that updates the digest for the given string. | |
| QMD5 (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. Same as above except it accepts a QByteArray as its argument. | |
| void | update (const char *in, int len=-1) |
| Updates the message to be digested. | |
| void | update (const unsigned char *in, 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. | |
| void | update (const QByteArray &in) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| bool | 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. reads the data from an I/O device, i.e. | |
| void | reset () |
| Calling this function will reset the calculated message digest. | |
| const Digest & | rawDigest () |
| void | rawDigest (QMD5::Digest &bin) |
| Fills the given array with the binary representation of the message digest. | |
| QByteArray | hexDigest () |
| Returns the value of the calculated message digest in a hexadecimal representation. | |
| void | hexDigest (QByteArray &) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| QByteArray | base64Digest () |
| Returns the value of the calculated message digest in a base64-encoded representation. | |
| bool | verify (const QMD5::Digest &digest) |
| returns true if the calculated digest for the given message matches the given one. | |
| bool | verify (const QByteArray &) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
Protected Member Functions | |
| void | transform (const unsigned char buffer[64]) |
| Performs the real update work. | |
| void | finalize () |
| finalizes the digest | |
Private Member Functions | |
| QMD5 (const QMD5 &u) | |
| QMD5 & | operator= (const QMD5 &md) |
| void | init () |
| void | encode (unsigned char *output, quint32 *in, quint32 len) |
| void | decode (quint32 *output, const unsigned char *in, quint32 len) |
Static Private Member Functions | |
| static quint32 | rotate_left (quint32 x, quint32 n) |
| static quint32 | F (quint32 x, quint32 y, quint32 z) |
| static quint32 | G (quint32 x, quint32 y, quint32 z) |
| static quint32 | H (quint32 x, quint32 y, quint32 z) |
| static quint32 | I (quint32 x, quint32 y, quint32 z) |
| static void | FF (quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, quint32 s, quint32 ac) |
| static void | GG (quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, quint32 s, quint32 ac) |
| static void | HH (quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, quint32 s, quint32 ac) |
| static void | II (quint32 &a, quint32 b, quint32 c, quint32 d, quint32 x, quint32 s, quint32 ac) |
Private Attributes | |
| quint32 | m_state [4] |
| quint32 | m_count [2] |
| quint8 | m_buffer [64] |
| Digest | m_digest |
| bool | m_finalized |
| QMD5Private * | d |
An adapted C++ implementation of RSA Data Securities MD5 algorithm.
The default constructor is designed to provide much the same functionality as the most commonly used C-implementation, while the other three constructors are meant to further simplify the process of obtaining a digest by calculating the result in a single step.
QMD5 is state-based, that means you can add new contents with update() as long as you didn't request the digest value yet. After the digest value was requested, the object is "finalized" and you have to call reset() to be able to do another calculation with it. The reason for this behavior is that upon requesting the message digest QMD5 has to pad the received contents up to a 64 byte boundary to calculate its value. After this operation it is not possible to resume consuming data.
Usage:
A common usage of this class:
const char* test1; QMD5::Digest rawResult; test1 = "This is a simple test."; QMD5 context (test1); cout << "Hex Digest output: " << context.hexDigest().data() << endl;
To cut down on the unnecessary overhead of creating multiple QMD5 objects, you can simply invoke reset() to reuse the same object in making another calculation:
context.reset (); context.update ("TWO"); context.update ("THREE"); cout << "Hex Digest output: " << context.hexDigest().data() << endl;
Definition at line 318 of file mcodecs.h.
| typedef unsigned char QMD5::Digest[16] |
| QMD5::QMD5 | ( | ) |
Definition at line 690 of file mcodecs.cpp.
| QMD5::~QMD5 | ( | ) |
Definition at line 707 of file mcodecs.cpp.
| QMD5::QMD5 | ( | const char * | in, | |
| int | len = -1 | |||
| ) | [explicit] |
Constructor that updates the digest for the given string.
| in | C string or binary data | |
| len | if negative, calculates the length by using strlen on the first parameter, otherwise it trusts the given length (does not stop on NUL byte). |
Definition at line 695 of file mcodecs.cpp.
| QMD5::QMD5 | ( | const QByteArray & | a | ) | [explicit] |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Same as above except it accepts a QByteArray as its argument.
Definition at line 701 of file mcodecs.cpp.
| QMD5::QMD5 | ( | const QMD5 & | u | ) | [private] |
| void QMD5::update | ( | const char * | in, | |
| int | len = -1 | |||
| ) |
Updates the message to be digested.
Be sure to add all data before you read the digest. After reading the digest, you can not add more data!
| in | message to be added to digest | |
| len | the length of the given message. |
Definition at line 716 of file mcodecs.cpp.
Referenced by HttpComms::calculateDigestResponse(), finalize(), QMD5(), and update().
| void QMD5::update | ( | const unsigned char * | in, | |
| 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.
Definition at line 721 of file mcodecs.cpp.
| void QMD5::update | ( | const QByteArray & | in | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| in | message to be added to the digest (QByteArray). |
Definition at line 711 of file mcodecs.cpp.
| bool QMD5::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. reads the data from an I/O device, i.e.
from a file (QFile).
NOTE that the file must be open for reading.
| file | a pointer to FILE as returned by calls like f{d,re}open |
Definition at line 764 of file mcodecs.cpp.
| void QMD5::reset | ( | ) |
Calling this function will reset the calculated message digest.
Use this method to perform another message digest calculation without recreating the QMD5 object.
Definition at line 871 of file mcodecs.cpp.
Referenced by HttpComms::calculateDigestResponse(), and init().
| const QMD5::Digest & QMD5::rawDigest | ( | ) |
Definition at line 823 of file mcodecs.cpp.
Referenced by verify().
| void QMD5::rawDigest | ( | QMD5::Digest & | bin | ) |
Fills the given array with the binary representation of the message digest.
Use this method if you do not want to worry about making copy of the digest once you obtain it.
| bin | an array of 16 characters ( char[16] ) |
Definition at line 829 of file mcodecs.cpp.
| QByteArray QMD5::hexDigest | ( | ) |
Returns the value of the calculated message digest in a hexadecimal representation.
Definition at line 836 of file mcodecs.cpp.
Referenced by HttpComms::calculateDigestResponse(), and verify().
| void QMD5::hexDigest | ( | QByteArray & | s | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 849 of file mcodecs.cpp.
| QByteArray QMD5::base64Digest | ( | ) |
Returns the value of the calculated message digest in a base64-encoded representation.
Definition at line 859 of file mcodecs.cpp.
| bool QMD5::verify | ( | const QMD5::Digest & | digest | ) |
returns true if the calculated digest for the given message matches the given one.
Definition at line 811 of file mcodecs.cpp.
| bool QMD5::verify | ( | const QByteArray & | hexdigest | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 817 of file mcodecs.cpp.
| void QMD5::transform | ( | const unsigned char | buffer[64] | ) | [protected] |
Performs the real update work.
Note that length is implied to be 64.
Definition at line 887 of file mcodecs.cpp.
Referenced by update().
| void QMD5::finalize | ( | ) | [protected] |
finalizes the digest
Definition at line 775 of file mcodecs.cpp.
Referenced by base64Digest(), hexDigest(), rawDigest(), and verify().
| void QMD5::init | ( | ) | [private] |
Definition at line 865 of file mcodecs.cpp.
Referenced by QMD5().
| void QMD5::encode | ( | unsigned char * | output, | |
| quint32 * | in, | |||
| quint32 | len | |||
| ) | [private] |
Definition at line 1031 of file mcodecs.cpp.
Referenced by finalize().
| void QMD5::decode | ( | quint32 * | output, | |
| const unsigned char * | in, | |||
| quint32 | len | |||
| ) | [private] |
Definition at line 1049 of file mcodecs.cpp.
Referenced by transform().
| quint32 QMD5::rotate_left | ( | quint32 | x, | |
| quint32 | n | |||
| ) | [inline, static, private] |
| quint32 QMD5::F | ( | quint32 | x, | |
| quint32 | y, | |||
| quint32 | z | |||
| ) | [inline, static, private] |
Definition at line 982 of file mcodecs.cpp.
Referenced by FF().
| quint32 QMD5::G | ( | quint32 | x, | |
| quint32 | y, | |||
| quint32 | z | |||
| ) | [inline, static, private] |
Definition at line 987 of file mcodecs.cpp.
Referenced by GG().
| quint32 QMD5::H | ( | quint32 | x, | |
| quint32 | y, | |||
| quint32 | z | |||
| ) | [inline, static, private] |
Definition at line 992 of file mcodecs.cpp.
Referenced by HH().
| quint32 QMD5::I | ( | quint32 | x, | |
| quint32 | y, | |||
| quint32 | z | |||
| ) | [inline, static, private] |
Definition at line 997 of file mcodecs.cpp.
Referenced by II().
| void QMD5::FF | ( | quint32 & | a, | |
| quint32 | b, | |||
| quint32 | c, | |||
| quint32 | d, | |||
| quint32 | x, | |||
| quint32 | s, | |||
| quint32 | ac | |||
| ) | [static, private] |
Definition at line 1002 of file mcodecs.cpp.
Referenced by transform().
| void QMD5::GG | ( | quint32 & | a, | |
| quint32 | b, | |||
| quint32 | c, | |||
| quint32 | d, | |||
| quint32 | x, | |||
| quint32 | s, | |||
| quint32 | ac | |||
| ) | [static, private] |
Definition at line 1009 of file mcodecs.cpp.
Referenced by transform().
| void QMD5::HH | ( | quint32 & | a, | |
| quint32 | b, | |||
| quint32 | c, | |||
| quint32 | d, | |||
| quint32 | x, | |||
| quint32 | s, | |||
| quint32 | ac | |||
| ) | [static, private] |
Definition at line 1016 of file mcodecs.cpp.
Referenced by transform().
| void QMD5::II | ( | quint32 & | a, | |
| quint32 | b, | |||
| quint32 | c, | |||
| quint32 | d, | |||
| quint32 | x, | |||
| quint32 | s, | |||
| quint32 | ac | |||
| ) | [static, private] |
Definition at line 1023 of file mcodecs.cpp.
Referenced by transform().
quint32 QMD5::m_state[4] [private] |
Definition at line 465 of file mcodecs.h.
Referenced by finalize(), reset(), and transform().
quint32 QMD5::m_count[2] [private] |
Definition at line 466 of file mcodecs.h.
Referenced by finalize(), reset(), and update().
quint8 QMD5::m_buffer[64] [private] |
Definition at line 467 of file mcodecs.h.
Referenced by finalize(), reset(), and update().
Digest QMD5::m_digest [private] |
Definition at line 468 of file mcodecs.h.
Referenced by base64Digest(), finalize(), hexDigest(), rawDigest(), and reset().
bool QMD5::m_finalized [private] |
Definition at line 469 of file mcodecs.h.
Referenced by finalize(), reset(), transform(), and update().
QMD5Private* QMD5::d [private] |
Definition at line 471 of file mcodecs.h.
Referenced by init(), and transform().
1.6.3