void DigestFilePair::calculate( const fs::path file ) { fs::ifstream file_stream( file ); CryptoPP::FileSource fsource( file_stream, true ); CryptoPP::SHA512 sha; byte digest[sha.DigestSize()]; byte *data = new byte[ fsource.MaxRetrievable() ]; size_t size = fsource.Get( data, fsource.MaxRetrievable() ); sha.CalculateDigest( digest, data, size ); std::stringstream ss; for(int i = 0; i < sha.DigestSize(); i++) ss << std::hex << std::setw(2) << std::setfill('0') << (int) digest[i]; delete[] data; _digest = ss.str(); _file = file; }
std::string hash_sha512(std::string input) { CryptoPP::SHA512 sha; unsigned char* buffer = new unsigned char[sha.DigestSize()]; unsigned char* inputbytes = new unsigned char[input.size()]; memcpy(inputbytes, input.c_str(), input.size()); sha.CalculateDigest(buffer, inputbytes, input.size()); char* hashedbytes = new char[sha.DigestSize()]; memcpy(hashedbytes, buffer, sha.DigestSize()); std::string hashed(hashedbytes, sha.DigestSize()); delete[] buffer; delete[] inputbytes; delete[] hashedbytes; return hashed; }