a firstworks project
Rudiments
About Documentation Download Licensing News

Using the md5 class

The md5 class provides an MD5 implementation which produces a 128-bit (16-byte) hash.

The md5 class inherits from the hash class and shares a common interface with the other hash classes: append() to feed in data, getHash() to retrieve the hash, getHashSize() to get the hash length in bytes, and clear() to reset. The isSupported() method can be used to determine if the hash algorithm is available on the current platform.

The following example hashes a string, prints the result in hexadecimal, clears the hash, and hashes a different string.

#include <rudiments/md5.h>
#include <rudiments/charstring.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

	md5	h;

	// check whether md5 is supported
	if (!h.isSupported()) {
		stdoutput.write("md5 is not supported\n");
		return 1;
	}


	// hash some data
	const char	*data="hello world";
	h.append((const byte_t *)data,charstring::getLength(data));

	// print the hash
	const byte_t	*hashval=h.getHash();
	uint64_t	hashsize=h.getHashSize();

	stdoutput.printf("md5(\"%s\") = ",data);
	for (uint64_t i=0; i<hashsize; i++) {
		stdoutput.printf("%02x",hashval[i]);
	}
	stdoutput.write('\n');


	// clear and hash different data
	h.clear();

	data="goodbye world";
	h.append((const byte_t *)data,charstring::getLength(data));

	hashval=h.getHash();
	hashsize=h.getHashSize();

	stdoutput.printf("md5(\"%s\") = ",data);
	for (uint64_t i=0; i<hashsize; i++) {
		stdoutput.printf("%02x",hashval[i]);
	}
	stdoutput.write('\n');
}
Copyright 2017 - David Muse - Contact