|
|
This class provides an easy to use C++ implementation of the MD5 algorithm. It provides an easy way to use the class much like you would the plain C-implementations along with single shot constructors that automatically perform the update and call finalize.
The default constructor is intended to provide much the same functionality as the C-implementation while the other three constructors are used to perform quick digest calculations whenever the message to be does not require subsequent updating. Additionally, you can obtain either the result in a 16-byte binary format or 33-byte hexidecimal by invoking rawDigest and @ hexDigest respectivelly. NOTE the extra byte in the latter format is is a NULL character to terminate the string.
The simplest way of using this class through one of the accessor methods:
KMD5 context( QCString("ONE") ); printf ( "Raw Digest output: %s", static_cast<char*>(context.rawDigest() ); printf ( "Hex Digest output: %s", context.hexDigest() ); |
You can then invoke reset() to re-use the same class to perform another digest.
context.reset(); context.update( QCString("TWO") ); context.update( QCString("THREE") ); printf ( "Raw Digest output: %s", static_cast<char*>(context.rawDigest() ); printf ( "Hex Digest output: %s", context.hexDigest() );
Note that once you invoke reset(), the previouly calculated digest value will be lost. Thus, be sure you copy the result if you need to use it again. Also you cannot invoke update() if you use one of the convienence constructors unless you first invoke reset(). Hence, if you need to update the message to be digested, it is better to use the default constructor to avoid this subtle error.
The conventional (C-implemetation like) method of using this class:
KMD5 context; context.update(QCString("ONE")); context.update(QCString("TWO")); context.update(QCString("THREE")); context.finalize(); printf ( "Raw Digest output: %s", static_cast<char*>(context.rawDigest() ); printf ( "Hex Digest output: %s", context.hexDigest() ); |
enum DigestType { BIN, HEX } | DigestType |
HEX hexidecimal representation of the message digest BIN binary representation of the message digest
enum ErrorType { ERR_NONE, ERR_ALREADY_FINALIZED, ERR_NOT_YET_FINALIZED, ERR_CANNOT_READ_FILE, ERR_CANNOT_CLOSE_FILE } | ErrorType |
ERR_NONE no error occured. [default] ERR_ALREADY_FINALIZED finalize() has already been invoked. ERR_NOT_YET_FINALIZED digest() or rawDigest() invoked before finalize(). ERR_CANNOT_READ_FILE indicates a problem while trying to read the given file. ERR_CANNOT_CLOSE_FILE indicates a problem while trying to close the given file.
KMD5 ()
| KMD5 |
Default constructor that only performs initialization. Unlike the other constructors
KMD5 (FILE *file)
| KMD5 |
Constructor that initializes, computes, and finalizes the message digest for the given file.
NOTE: This is a convience constructor. As such it does not allow the update of the message after it has been invoked. If you need to update the message after creating the constructor,
KMD5 (Q_UINT8 * input)
| KMD5 |
Constructor that initializes, computes, and finalizes the message digest for the given string.
KMD5 (const QCString& input)
| KMD5 |
Same as above except it takes a QCString as input.
KMD5 (const QString& input)
| KMD5 |
Same as above except it takes a QString as input.
void update (Q_UINT8 * input, int len = -1 )
| update |
Updates the message to be digested.
Parameters:
input | message to be added to digest (unsigned char*) |
len | the length of the given message. |
void update ( const QCString& input )
| update |
Same as above except except it takes a QCString as the argument.
void update ( const QString& input )
| update |
Same as above except it takes a QString as the argument.
void update (FILE *file, bool closeFile = false )
| update |
Same as above except it accepts a pointer to FILE.
NOTE that the file must have been already opened. If you
want the file to be automatically closed, set closeFile
to TRUE.
Parameters:
file | a pointer to FILE as returned by calls like f{d,re}open |
closeFile | if true closes the file using fclose. |
void finalize ()
| finalize |
Finalizes the message digest calculation.
If you used the default constructor, you must invoke this function before you can obtain the calculated digest value.
bool verify ( const QCString& input, const char * msg_digest,
DigestType type = HEX )
| verify |
Compares the message digest supplied messaged digest msg_digest
with the one calculated for the input QCString input
.
NOTE: Calling this function will reset any previously calcualted digests. If you want to verify your token with the current digest value, use verify( const char*, DigestType ) instead.
Parameters:
input | the message to be added to the digest value |
msg_digest | the digest to compare the result against |
type | the format of the result for comparison (binary or hexidecimal). |
Returns: true if the digests match, otherwise false.
bool verify ( const QString& input, const char * msg_digest,
DigestType type = HEX )
| verify |
Same as above except the input is a QString instead.
NOTE: Calling this function will reset any previously calcualted digests. If you want to verify your token with the current digest value, use verify( const char*, DigestType ) instead.
bool verify ( FILE* f, const char * msg_digest, DigestType type = HEX )
| verify |
Same as above except the input is a pointer for a FILE instead.
NOTE: Calling this function will reset any previously calcualted digests. If you want to verify your token with the current digest value, use verify( const char*, DigestType ) instead.
bool verify ( const char* msg_digest, DigestType type = HEX )
| verify |
Compares the given string with with the current message digest.
NOTE: Unlike the other verify functions this one does not reset the current message digest if one is already present. It is meant to allow you to compare against the existing message digest. Also note that this function will return false if a digest has yet to be calculated.
Parameters:
msg_digest | the digest to compare the result against |
type | the format of the result for comparison (binary or hexidecimal). |
Returns: true if the digests match, otherwise false.
void reset ()
| reset |
Re-initializes internal paramters.
Note that calling this function will reset all internal variables and hence any calculated digest. Invoke this function only when you have to re-use the same object to perform another message digest calculation.
Q_UINT8* rawDigest ()
| rawDigest |
Returns the raw 16-byte binary value of the message digest.
NOTE: you are responsible for making a copy of this string.
void rawDigest ( HASH bin )
| rawDigest |
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.
Parameters:
bin | an array of 16 characters ( char[16] ) |
Returns: true if the raw digest is ready, otherwise false.
char * hexDigest ()
| hexDigest |
Returns a the value of the calculated message digest. This is a 32 byte hexidecimal value terminated by a NULL character.
NOTE: you are responsible for making a copy of this string.
void hexDigest ( HASHHEX hex )
| hexDigest |
Fills the given array with the hexcidecimal 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. Also note that this method will append a terminating NULL charater.
Parameters:
bin | an array of 33 characters ( char[33] ) |
Returns: true if the digest is ready, otherwise false.
bool hasErrored ()
| hasErrored |
[const]
Indicates whether the message digest calculation failed or succeeded. Use error to determine the error type.
Returns: true if
int error ()
| error |
[const]
Returns the type error that occurred. See ErrorType for descriptions.
void init ()
| init |
[protected]
Initializer called by all constructors
void transform ( Q_UINT8 * buffer )
| transform |
[protected]
Performs the real update work. Note that length is implied to be 64.
bool isDigestMatch ( const char * msg_digest, DigestType type )
| isDigestMatch |
[protected]
Returns true if the current message digest matches msg_digest
.