summaryrefslogtreecommitdiffstats
path: root/include/sha1.h
blob: 05cedd4cb188a2befffb0779d773af97912b4bce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 */