From e35d8cf13627092b9605023f5d288dee1ae90759 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Thu, 30 Apr 2026 21:28:09 -0400 Subject: [PATCH] core: blob+stream: Store stream/blob hashes Signed-off-by: Chloe M. --- Makefile | 4 +++- core/blob_list.c | 3 +++ core/blob_subr.c | 1 + core/stream_file.c | 2 ++ inc/blobchain/blob.h | 3 +++ inc/blobchain/common.h | 2 ++ inc/blobchain/stream.h | 4 ++++ 7 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index acaa4bc..c300eb2 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ DFILES = $(CFILES:.c=.d) OFILES = $(CFILES:.c=.o) CC = gcc +LIBS = $(shell pkg-config --libs "openssl") + CFLAGS = \ -Wall \ -pedantic \ @@ -19,7 +21,7 @@ all: blobchain .PHONY: blobchain blobchain: $(OFILES) - $(CC) $^ -o $@ + $(CC) $(LIBS) $^ -o $@ -include $(DFILES) %.o: %.c diff --git a/core/blob_list.c b/core/blob_list.c index d7d254b..6c7502e 100644 --- a/core/blob_list.c +++ b/core/blob_list.c @@ -95,6 +95,9 @@ blob_list_dump(struct blob_list *lp) while (blob != NULL) { printf("blob %zd/%zd\n", count++, lp->count); printf("... length :: %zd\n", blob->length); + printf("... hash :: "); + dump_bytes(blob->hash, SHA256_N_BYTES); + printf("... data :: "); dump_bytes(blob->data, blob->length < DUMP_SIZE ? blob->length diff --git a/core/blob_subr.c b/core/blob_subr.c index 305f407..b8c6202 100644 --- a/core/blob_subr.c +++ b/core/blob_subr.c @@ -22,5 +22,6 @@ blob_allocate(struct data_stream *stmp) memcpy(bp->data, stmp->data, stmp->length); } + memcpy(bp->hash, stmp->hash, SHA256_N_BYTES); return bp; } diff --git a/core/stream_file.c b/core/stream_file.c index e169bbb..90b6845 100644 --- a/core/stream_file.c +++ b/core/stream_file.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "blobchain/memlib.h" #include "blobchain/stream.h" @@ -56,6 +57,7 @@ stream_from_file(struct data_stream *stmp, const char *path) return -1; } + SHA256(stmp->data, stmp->length, stmp->hash); return 0; } diff --git a/inc/blobchain/blob.h b/inc/blobchain/blob.h index df4562c..55265b1 100644 --- a/inc/blobchain/blob.h +++ b/inc/blobchain/blob.h @@ -9,17 +9,20 @@ #include #include #include "blobchain/stream.h" +#include "blobchain/common.h" /* * Represents a single blob of data * * @data: Buffer containing blob data * @length: Length of blob + * @hash: Hash of file contents * @next: Next blob */ struct blob { void *data; size_t length; + uint8_t hash[SHA256_N_BYTES]; struct blob *next; }; diff --git a/inc/blobchain/common.h b/inc/blobchain/common.h index 1505c85..390c5f6 100644 --- a/inc/blobchain/common.h +++ b/inc/blobchain/common.h @@ -6,6 +6,8 @@ #ifndef BLOBCHAIN_COMMON_H #define BLOBCHAIN_COMMON_H 1 +#define SHA256_N_BYTES 32 + #define BLOBCHAIN_DEBUG 1 #define BLOBCHAIN_VERSION "0.0.1" diff --git a/inc/blobchain/stream.h b/inc/blobchain/stream.h index 5e9c523..eee475a 100644 --- a/inc/blobchain/stream.h +++ b/inc/blobchain/stream.h @@ -6,17 +6,21 @@ #ifndef BLOBCHAIN_STREAM_H #define BLOBCHAIN_STREAM_H 1 +#include #include +#include "blobchain/common.h" /* * Represents a data stream * * @data: Data buffer * @length: Number of bytes in data buffer + * @hash: SHA256 hash of file contents */ struct data_stream { void *data; size_t length; + uint8_t hash[SHA256_N_BYTES]; }; /*