Fix md5 to hex conversion: Output a leading zero for values smaller than ten
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 8 Nov 2010 15:29:22 +0000 (16:29 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 8 Nov 2010 15:29:22 +0000 (16:29 +0100)
src/util.cpp

index fe11814..6b7cbda 100644 (file)
@@ -10,6 +10,7 @@
 #include "util.hpp"
 
 #include <sstream>
+#include <iomanip>
 #include <openssl/evp.h>
 #include <boost/algorithm/string.hpp>
 
@@ -92,7 +93,8 @@ std::string compute_md5_digest(std::string data)
     {
         // We have to do a static cast to an decimal representation, cause otherwise ostringstream would interpret
         // the stream as a character and output the character representation of the hex value.
-        oss_digest_md5_hex << std::hex << static_cast<unsigned short>(md_value[i]);
+        oss_digest_md5_hex << std::nouppercase << std::setw(2) << std::setfill('0')
+                           << std::hex << static_cast<int>(static_cast<unsigned char>(md_value[i]));
     }
 
     return oss_digest_md5_hex.str();