1 // //////////////////////////////////////////////////////////
3 // Copyright (c) 2014 Stephan Brumme. All rights reserved.
4 // see http://create.stephan-brumme.com/disclaimer.html
12 // define fixed size integer types
15 typedef unsigned __int8 uint8_t;
16 typedef unsigned __int32 uint32_t;
17 typedef unsigned __int64 uint64_t;
27 std::string myHash = md5("Hello World"); // std::string
28 std::string myHash2 = md5("How are you", 11); // arbitrary data, 11 bytes
30 // or in a streaming fashion:
33 while (more data available)
34 md5.add(pointer to fresh data, number of new bytes);
35 std::string myHash3 = md5.getHash();
37 class MD5 //: public Hash
40 /// split into 64 byte blocks (=> 512 bits), hash is 16 bytes long
41 enum { BlockSize = 512 / 8, HashBytes = 16 };
46 /// compute MD5 of a memory block
47 std::string operator()(const void* data, size_t numBytes);
48 /// compute MD5 of a string, excluding final zero
49 std::string operator()(const std::string& text);
51 /// add arbitrary number of bytes
52 void add(const void* data, size_t numBytes);
54 /// return latest hash as 32 hex characters
55 std::string getHash();
56 /// return latest hash as bytes
57 void getHash(unsigned char buffer[HashBytes]);
64 void processBlock(const void* data);
65 /// process everything left in the internal buffer
68 /// size of processed data in bytes
70 /// valid bytes in m_buffer
72 /// bytes not processed yet
73 uint8_t m_buffer[BlockSize];
75 enum { HashValues = HashBytes / 4 };
76 /// hash, stored as integers
77 uint32_t m_hash[HashValues];