mirror of
https://github.com/oxen-io/oxen-core.git
synced 2023-12-14 02:22:56 +01:00
1dd98f3dae
Converts all use of boost::filesystem to std::filesystem. For macos and potentially other exotic systems where std::filesystem isn't available, we use ghc::filesystem instead (which is a drop-in replacement for std::filesystem, unlike boost::filesystem). This also greatly changes how we handle filenames internally by holding them in filesystem::path objects as soon as possible (using fs::u8path()), rather than strings, which avoids a ton of issues around unicode filenames. As a result this lets us drop the boost::locale dependency on Windows along with a bunch of messy Windows ifdef code, and avoids the need for doing gross boost locale codecvt calls.
57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
|
|
#include "sha256sum.h"
|
|
#include <fstream>
|
|
#include "crypto/hash.h"
|
|
#include "fs.h"
|
|
|
|
extern "C" {
|
|
#include <openssl/sha.h>
|
|
}
|
|
|
|
namespace tools {
|
|
|
|
bool sha256sum_str(std::string_view data, crypto::hash &hash)
|
|
{
|
|
SHA256_CTX ctx;
|
|
if (!SHA256_Init(&ctx))
|
|
return false;
|
|
if (!SHA256_Update(&ctx, data.data(), data.size()))
|
|
return false;
|
|
if (!SHA256_Final(reinterpret_cast<unsigned char*>(hash.data), &ctx))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
bool sha256sum_file(const fs::path& filename, crypto::hash& hash)
|
|
{
|
|
if (std::error_code ec; !fs::exists(filename, ec) || ec)
|
|
return false;
|
|
fs::ifstream f;
|
|
f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
|
f.open(filename, std::ios_base::binary | std::ios_base::in | std::ios::ate);
|
|
if (!f)
|
|
return false;
|
|
std::ifstream::pos_type file_size = f.tellg();
|
|
SHA256_CTX ctx;
|
|
if (!SHA256_Init(&ctx))
|
|
return false;
|
|
size_t size_left = file_size;
|
|
f.seekg(0, std::ios::beg);
|
|
while (size_left)
|
|
{
|
|
char buf[4096];
|
|
std::ifstream::pos_type read_size = size_left > sizeof(buf) ? sizeof(buf) : size_left;
|
|
f.read(buf, read_size);
|
|
if (!f || !f.good())
|
|
return false;
|
|
if (!SHA256_Update(&ctx, buf, read_size))
|
|
return false;
|
|
size_left -= read_size;
|
|
}
|
|
f.close();
|
|
if (!SHA256_Final((unsigned char*)hash.data, &ctx))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
}
|