Search code examples
c++poco-libraries

Counting MD5 of part of a file with Poco Library


Having problems with getting the right md5 hash for part of a file, my code looks like this. Whenever I read the whole file (without seek and read) it counts fine. Where is the error?

long length = (offset_end - offset_start) + 1;
char* buffer = new char[length];
std::ifstream ifs(downloadFile->url_hdd.c_str(), std::ios::binary);
ifs.seekg(offset_start, std::ios::beg);
ifs.read(buffer, length);
ifs.close();
std::stringstream stringStream(buffer);

Poco::MD5Engine md5;
Poco::DigestOutputStream outstr(md5);
outstr << stringStream.str();
outstr.flush(); //to pass everything to the digest engine
const Poco::DigestEngine::Digest& digest = md5.digest();
std::string md5string = Poco::DigestEngine::digestToHex(digest);

Solution

  • This seems to be working.

    unsigned long long length = (downloadChunk->offset_end - downloadChunk->offset_start) + 1;
    char *data = new char[length];
    fseek(file, downloadChunk->offset_start, SEEK_SET);
    fread(data, 1, length, file);
                                            
    fclose(file);
                                            
    std::istringstream iss(std::string(data, length));
    
    delete []data;