commit 049d4dcd29fb8bf2e0455b52b08e2a32a4325c66
parent d13b760aa41cf2b2ef105e5a5d6492351a377c1a
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Sat, 20 Feb 2021 21:43:28 -0600
sha256.*: add sha256_file
Diffstat:
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/sha256.c b/sha256.c
@@ -194,3 +194,28 @@ sha256_tostr(char *dest, const char *src)
return dest[64] = '\0';
}
+
+int
+sha256_file(char *path, char *dest)
+{
+ FILE *f;
+ struct sha256 ctx;
+ char buf[BUFSIZ];
+ int n;
+
+ if ((f = fopen(path, "r")) == NULL)
+ return -1;
+
+ sha256_init(&ctx);
+ while ((n = fread(buf, sizeof(char), BUFSIZ, f)))
+ sha256_update(&ctx, buf, n);
+
+ if (ferror(f)) {
+ fclose(f);
+ return -1;
+ }
+
+ sha256_sum(&ctx, dest);
+ fclose(f);
+ return 0;
+}
diff --git a/sha256.h b/sha256.h
@@ -20,3 +20,5 @@ void sha256_sum(void *ctx, uint8_t md[SHA256_DIGEST_LENGTH]);
int sha256_fromstr(char *dest, const char *src);
/* convert 32 byte array to hex string */
int sha256_tostr(char *dest, const char *src);
+/* sha256 entire file at path */
+int sha256_file(char *path, char *dest);