InfoKriegerX66

Change MD5 Hash with Boost C++ Libraries & OpenSSL

Aug 15th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. std::string GetMD5Hash(const boost::filesystem::path& path)
  2. {
  3.     byte result[MD5_DIGEST_LENGTH];
  4.     boost::iostreams::mapped_file_source source(path);
  5.     MD5((byte*)source.data(), source.size(), result); // OpenSSL
  6.  
  7.     std::ostringstream s;
  8.     s << std::hex << std::setfill('0');
  9.     for (byte c : result)
  10.         s << std::setw(2) << (int)c;
  11.     return s.str();
  12. }
  13.  
  14. bool ChangeMD5Hash(const boost::filesystem::path& path)
  15. {
  16.     boost::filesystem::path md5(path.string() + ".md5");
  17.     if (boost::filesystem::exists(md5))
  18.         boost::filesystem::remove(md5);
  19.     if (boost::filesystem::exists(path))
  20.         boost::filesystem::rename(path, md5);
  21.     else
  22.         return false;
  23.  
  24.     boost::random::mt19937 generator;
  25.     boost::random::uniform_int_distribution<> random(1, 1024);
  26.  
  27.     boost::filesystem::ifstream istream(md5, std::ios::in | std::ios::binary);
  28.     boost::filesystem::ofstream ostream(path, std::ios::out | std::ios::binary);
  29.     if (!istream.is_open())
  30.         return false;
  31.     if (!ostream.is_open())
  32.         return false;
  33.     ostream << istream.rdbuf();
  34.     ostream.seekp(istream.tellg());
  35.     ostream.write("\0", random(generator));
  36.     istream.close();
  37.     ostream.close();
  38.     return boost::filesystem::remove(md5);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment