-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigest.h
More file actions
47 lines (40 loc) · 1.56 KB
/
digest.h
File metadata and controls
47 lines (40 loc) · 1.56 KB
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
43
44
45
46
47
#ifndef DIGEST_H
#define DIGEST_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef struct Words {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t e;
uint32_t f;
uint32_t g;
uint32_t h;
} Words;
typedef struct Sha256StreamingContext {
// State for the SHA-256 context, gets updated with each hashed block.
struct Words state;
// Stack buffer for hash input (one block).
uint8_t buf[64];
// Length of the buffer currently being processed (`data` in `sha256_stream_update`).
size_t buf_len;
// Total length of the hashed message, used for padding in the last block.
size_t tot_len;
// Set to `true` if the hashing process has been finalized.
bool finalized;
} Sha256StreamingContext;
// Sets up the context with SHA-256's initial hash values and clears all buffers.
// This must be called beore any other streaming operations.
void sha256_stream_init(Sha256StreamingContext *const ctx);
// Process a chunk of data through the SHA-256 streaming context.
//
// This function can be called multiple times to incrementally hash large amounts of data. It
// handles block boundaries and buffering of incomplete blocks. For optimal performance on very
// large inputs, use a buffer of 64KB.
void sha256_stream_update(Sha256StreamingContext *const ctx, const uint8_t *data, size_t len);
// Applies SHA-256 padding, processes any remaining buffered data, and outputs the final hash
// as a NULL-terminated hex string.
void sha256_stream_final(Sha256StreamingContext *const ctx, char *out);
#endif