1

在为文件的一部分获取正确的 md5 哈希时遇到问题,我的代码如下所示。每当我阅读整个文件(没有搜索和阅读)时,它都很好。错误在哪里?

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);
4

1 回答 1

0

这似乎奏效了。

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;
于 2013-11-04T10:33:17.993 回答