Debian Codegraph @

debian/rust-vendor/blake3/c/example.c

raw
1 #include "blake3.h"
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6
7 int main(void) {
8 // Initialize the hasher.
9 blake3_hasher hasher;
10 blake3_hasher_init(&hasher);
11
12 // Read input bytes from stdin.
13 unsigned char buf[65536];
14 while (1) {
15 ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
16 if (n > 0) {
17 blake3_hasher_update(&hasher, buf, n);
18 } else if (n == 0) {
19 break; // end of file
20 } else {
21 fprintf(stderr, "read failed: %s\n", strerror(errno));
22 return 1;
23 }
24 }
25
26 // Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes.
27 uint8_t output[BLAKE3_OUT_LEN];
28 blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
29
30 // Print the hash as hexadecimal.
31 for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) {
32 printf("%02x", output[i]);
33 }
34 printf("\n");
35 return 0;
36 }