fbpx

C++: MD5

Bjarne-stroustrup
 


Encode a string using an MD5 algorithm. The algorithm can be found on wikipedia.

Optionally, validate your implementation by running all of the test values in IETF RFC (1321) for MD5. Additional the RFC provides more precise information on the algorithm than the Wikipedia article.

Warning: MD5 has known weaknesses, including collisions and forged signatures. Users may consider a stronger alternative when doing production-grade cryptography, such as SHA-256 (from the SHA-2 family) or the upcoming SHA-3.

If the solution on this page is a library solution, see MD5/Implementation for an implementation from scratch.

#include <string>
#include <iostream>
#include "Poco/MD5Engine.h"
#include "Poco/DigestStream.h"

using Poco::DigestEngine ;
using Poco::MD5Engine ;
using Poco::DigestOutputStream ;

int main( ) {
	std::string myphrase ( "The quick brown fox jumped over the lazy dog's back" ) ;
	MD5Engine md5 ;
	DigestOutputStream outstr( md5 ) ;
	outstr << myphrase ;
	outstr.flush( ) ; //to pass everything to the digest engine
	const DigestEngine::Digest& digest = md5.digest( ) ;
	std::cout << myphrase << " as a MD5 digest :\n" << DigestEngine::digestToHex( digest ) 
	<< " !" << std::endl ;
	return 0 ;
}
Output:
The quick brown fox jumped over the lazy dog's back as a MD5 digest :
e38ca1d920c4b8b8d3946b2c72f01680 !

SOURCE

Content is available under GNU Free Documentation License 1.2.