Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 32acbcc6 authored by Alessio Balsini's avatar Alessio Balsini Committed by android-build-merger
Browse files

Merge "Use ReadFileToString() in GetHash()"

am: 9b9854a8

Change-Id: Ib45c803da5280d17fd176aceb7f802e8bd6ee5ef
parents 1b9bb8e7 9b9854a8
Loading
Loading
Loading
Loading
+6 −13
Original line number Diff line number Diff line
@@ -92,21 +92,14 @@ std::string ToHexString(const uint8_t* buf, size_t len) {
}

std::optional<std::string> GetHash(const std::string& path) {
    unique_fd fd(open(path.c_str(), O_RDONLY));
    char buf[4096];
    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    while (true) {
        ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), buf, sizeof(buf)));
        if (n < 0) {
            PLOG(ERROR) << "Cannot read " << path;
    std::string content;
    if (!android::base::ReadFileToString(path, &content, true)) {
        PLOG(ERROR) << "Cannot access " << path;
        return std::nullopt;
    }
        if (n == 0) {
            break;
        }
        SHA256_Update(&ctx, buf, n);
    }
    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, content.c_str(), content.size());
    uint8_t out[32];
    SHA256_Final(out, &ctx);
    return ToHexString(out, sizeof(out));