aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_init_sha1_hasher.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-23 22:36:41 +0200
committerMattias Andrée <maandree@kth.se>2024-08-23 22:40:29 +0200
commit2fd94aae29d53d9859bfc3f53777970def38e253 (patch)
treee45573486f44271d75ee22284953e5dcde303ca5 /libhashsum_init_sha1_hasher.c
parentFirst commit (diff)
downloadlibhashsum-2fd94aae29d53d9859bfc3f53777970def38e253.tar.gz
libhashsum-2fd94aae29d53d9859bfc3f53777970def38e253.tar.bz2
libhashsum-2fd94aae29d53d9859bfc3f53777970def38e253.tar.xz
Fixes + add SHA0 and SHA1 using libsha1
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--libhashsum_init_sha1_hasher.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/libhashsum_init_sha1_hasher.c b/libhashsum_init_sha1_hasher.c
new file mode 100644
index 0000000..6058b73
--- /dev/null
+++ b/libhashsum_init_sha1_hasher.c
@@ -0,0 +1,69 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+LIBHASHSUM_1_NONNULL_
+static size_t
+process(struct libhashsum_hasher *this, const void *data, size_t bytes)
+{
+ const uint8_t *m = data;
+ int i;
+ bytes -= bytes & 63U;
+ if (bytes > SIZE_MAX >> 3) {
+ for (i = 0; i < 8; i++) {
+ libsha1_update(&this->state.sha1.s, m, bytes);
+ m = &m[bytes >> 3];
+ }
+ } else if (bytes) {
+ libsha1_update(&this->state.sha1.s, m, bytes << 3);
+ }
+ return bytes;
+}
+
+
+LIBHASHSUM_1_NONNULL_
+static int
+finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits)
+{
+ const uint8_t *m = data;
+ size_t r;
+
+ if (extra_bits > 7U) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ r = process(this, m, bytes);
+ m = &m[r];
+ bytes -= r;
+
+ libsha1_digest(&this->state.sha1.s, data, (bytes << 3) | extra_bits, this->state.sha1.sum);
+ memset(&this->state.sha1.s, 0, sizeof(this->state.sha1.s));
+ this->hash_output = this->state.sha1.sum;
+ return 0;
+}
+
+
+LIBHASHSUM_1_NONNULL_
+static int
+finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extra_bits, size_t size)
+{
+ (void) size;
+ return finalise_const(this, data, bytes, extra_bits);
+}
+
+
+int
+libhashsum_init_sha1_hasher(struct libhashsum_hasher *this)
+{
+ this->algorithm = LIBHASHSUM_SHA1;
+ this->input_block_size = 64U;
+ this->hash_size = sizeof(this->state.sha1.sum);
+ this->hash_output = NULL;
+ this->supports_non_whole_bytes = 0;
+ this->process = &process;
+ this->finalise_const = &finalise_const;
+ this->finalise = &finalise;
+ libsha1_init(&this->state.sha1.s, LIBSHA1_1);
+ return 0;
+}