diff options
| author | Chloe M. <chloe@aptel.org> | 2026-08-25 15:50:17 +0000 |
|---|---|---|
| committer | Chloe M. <chloe@aptel.org> | 2026-08-25 15:50:48 +0000 |
| commit | f33fd94c61f1596934bb145432642c69ee5a1061 (patch) | |
| tree | c80625f4baf88e0eddc85c79ad38a65a424025a9 | |
| parent | 12f8f954b60450152d0d9709266e7612b6cdffef (diff) | |
core: hash: Pack access date alongside content
Signed-off-by: Chloe M. <chloe@aptel.org>
| -rw-r--r-- | core/hash.c | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/core/hash.c b/core/hash.c index d982b15..5663d31 100644 --- a/core/hash.c +++ b/core/hash.c @@ -3,10 +3,13 @@ * Provided under the BSD-3 clause. */ +#include <sys/stat.h> #include <sys/mman.h> #include <stdio.h> +#include <stdlib.h> #include <unistd.h> #include <fcntl.h> +#include <string.h> #include "bop/hash.h" int @@ -14,8 +17,9 @@ bop_hash_file(const char *path, char hashres[BOP_HASH_LEN]) { int fd; size_t len, i; - uint8_t hash[SHA256_DIGEST_LENGTH]; + uint8_t *buf, hash[SHA256_DIGEST_LENGTH]; void *mem; + struct stat sb; if (path == NULL) { return -1; @@ -26,6 +30,12 @@ bop_hash_file(const char *path, char hashres[BOP_HASH_LEN]) return -1; } + if (stat(path, &sb) < 0) { + perror("stat"); + close(fd); + return -1; + } + /* Grab the file length */ len = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); @@ -46,11 +56,25 @@ bop_hash_file(const char *path, char hashres[BOP_HASH_LEN]) return -1; } - SHA256(mem, len, hash); + /* Allocate the buffer for packing */ + buf = malloc(len + sizeof(sb.st_mtime)); + if (buf == NULL) { + printf("fatal: failed to allocate hash buffer\n"); + munmap(mem, len); + close(fd); + return -1; + } + + /* Pack together access time and contents */ + memcpy(buf, mem, len); + memcpy(&buf[len], &sb.st_mtime, sizeof(sb.st_atime)); + SHA256(buf, len + sizeof(sb.st_atime), hash); + for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) { sprintf((char *)hashres + i*2, "%02X", hash[i]); } + free(buf); munmap(mem, len); close(fd); return 0; |
