summaryrefslogtreecommitdiffstats
path: root/include/sha1.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/sha1.h')
-rw-r--r--include/sha1.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/include/sha1.h b/include/sha1.h
new file mode 100644
index 0000000..05cedd4
--- /dev/null
+++ b/include/sha1.h
@@ -0,0 +1,42 @@
+#ifndef VL_SHA1_H_INCLUDED
+#define VL_SHA1_H_INCLUDED
+
+#include <stdint.h>
+#include <stddef.h>
+
+#define VL_SHA1_STATELEN_32 ( 5u) /* 160 bits / 32 (bits per int) = 5 ints */
+#define VL_SHA1_CHUNKLEN_BYTES (64u) /* 512 bits / 8 (bits per byte) = 64 bytes */
+
+#define VL_SHA1_DIGEST_BYTES (20u)
+#define VL_SHA1_DIGEST_HEX_STRLEN (VL_SHA1_DIGEST_BYTES << 1)
+
+typedef struct {
+ /* struct fields are internal (struct size is ABI doe) */
+ size_t vl_nchunk;
+ uint64_t vl_total;
+ uint32_t vl_state[VL_SHA1_STATELEN_32];
+ uint8_t vl_chunk[VL_SHA1_CHUNKLEN_BYTES];
+} vl_sha1_st;
+
+typedef uint8_t vl_sha1[VL_SHA1_DIGEST_BYTES];
+
+/* initializes a SHA1 state struct */
+void vl_sha1_init(vl_sha1_st *st);
+
+/* updates a SHA1 state struct with new data (must have been initialized with vl_sha1_init first) */
+void vl_sha1_update(vl_sha1_st *st, const void *data, size_t sz);
+
+/* finalizes a SHA1 state struct, writing the final digest to the second argument. the state struct
+ * is now invalid, and vl_sha1_init must be used on it again before it can be reused. */
+void vl_sha1_finalize(vl_sha1_st *st, vl_sha1 out);
+
+/* shortcut for hashing a buffer */
+void vl_sha1_buffer(vl_sha1 odigest, const void *data, size_t sz);
+
+/* converts digest bytes to a hex string (does NOT place a NUL byte) */
+void vl_sha1_encode(const vl_sha1 dig, char hex[VL_SHA1_DIGEST_HEX_STRLEN]);
+
+/* converts a hex string to digest bytes */
+int vl_sha1_decode(vl_sha1 odigest, const char hex[VL_SHA1_DIGEST_HEX_STRLEN]);
+
+#endif /* include guard */