#include "base/base64.h"
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/hash/md5.h"
//md5分段计算,针对大文件
const int kMd5BufferSize = 512 * 1024; // 512 kB.
std::string GetMd5Test(const base::FilePath& file_path)
base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (!file.IsValid())
return std::string();
base::MD5Context context;
base::MD5Init(&context);
int64_t offset = 0;
std::unique_ptr<char[]> buffer(new char[kMd5BufferSize ]);
while (true) {
int result = file.Read(offset, buffer.get(), kMd5BufferSize );
if (result < 0) {
// Found an error.
return std::string();
}
if (result == 0) {
//文件结尾
break;
}
offset += result;
base::MD5Update(&context, base::StringPiece(buffer.get(), result));
}
base::MD5Digest digest;
base::MD5Final(&digest, &context);
//md5转base16
return base::MD5DigestToBase16(digest);
}
Chromium base库 分段计算md5 c++
于 2024-09-30 14:13:54 首次发布