/*
pow! - the pow!erful package manager
Copyright (C) 2026 forever2k38
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "hash.h"
#include "log.h"
#include
#include
#include
#include
#include
int pow_check_blake2b_hash(char *path, char *target_hash) {
FILE *fp = fopen(path, "rb");
if (!fp) {
pow_log_error("failed to open file to check hash");
return 1;
}
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fsize < 0) {
pow_log_error("error: ftell");
fclose(fp);
return 1;
}
char *buf = NULL;
if (fsize > 0) {
buf = malloc((size_t)fsize);
if (!buf) {
pow_log_error("out of memory");
fclose(fp);
return 1;
}
if (fread(buf, 1, (size_t)fsize, fp) != (size_t)fsize) {
pow_log_error("read error");
free(buf);
fclose(fp);
return 1;
}
}
fclose(fp);
uint8_t digest[BLAKE2B_OUTBYTES];
if (blake2b(digest, buf, NULL, BLAKE2B_OUTBYTES, (size_t)fsize, 0) != 0) {
pow_log_error("blake2b failed");
free(buf);
return 1;
}
free(buf);
char hex[BLAKE2B_OUTBYTES * 2 + 1];
for (size_t i = 0; i < BLAKE2B_OUTBYTES; i++) {
snprintf(&hex[i * 2], 3, "%02x", digest[i]);
}
hex[BLAKE2B_OUTBYTES * 2] = '\0';
if (!target_hash || strcmp(hex, target_hash) != 0) {
return 1;
}
return 0;
}