From 4cf8b35097a131abcfc8e0d04d35294be13943ac Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Tue, 6 Jan 2026 03:27:12 -0600 Subject: initial commit --- lib/include/sha1.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/include/sha1.h (limited to 'lib/include/sha1.h') diff --git a/lib/include/sha1.h b/lib/include/sha1.h new file mode 100644 index 0000000..05cedd4 --- /dev/null +++ b/lib/include/sha1.h @@ -0,0 +1,42 @@ +#ifndef VL_SHA1_H_INCLUDED +#define VL_SHA1_H_INCLUDED + +#include +#include + +#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 */ -- cgit v1.2.3-70-g09d2