aboutsummaryrefslogtreecommitdiff
path: root/c/README.md
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2020-01-29 12:51:11 -0500
committerJack O'Connor <[email protected]>2020-01-29 13:01:40 -0500
commitaf0ef0751940ec2b61f91cc2d535320d7c2463e2 (patch)
treed2e2fce6bae6301319188768f5c070d263f5754f /c/README.md
parent37e153cc607ba5b975ce8bd7f2017f01624781bb (diff)
update the c/README.md example to hash stdin
Diffstat (limited to 'c/README.md')
-rw-r--r--c/README.md57
1 files changed, 36 insertions, 21 deletions
diff --git a/c/README.md b/c/README.md
index a0d87af..abdecc7 100644
--- a/c/README.md
+++ b/c/README.md
@@ -22,34 +22,49 @@ struct and five functions in [`blake3.h`](blake3.h):
## Example
+Here's an example program that hashes bytes from standard input and
+prints the result:
+
```c
-#include <stdio.h>
#include "blake3.h"
+#include <stdio.h>
int main() {
- // Initialize the hasher.
- blake3_hasher hasher;
- blake3_hasher_init(&hasher);
-
- // Write some input bytes.
- blake3_hasher_update(&hasher, "foo", 3);
- blake3_hasher_update(&hasher, "bar", 3);
- blake3_hasher_update(&hasher, "baz", 3);
-
- // Finalize the hash. BLAKE3_OUT_LEN is 32 bytes, but extended outputs are
- // also supported.
- uint8_t output[BLAKE3_OUT_LEN];
- blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
-
- // Print the hash as hexadecimal.
- for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) {
- printf("%02x", output[i]);
- }
- printf("\n");
- return 0;
+ // Initialize the hasher.
+ blake3_hasher hasher;
+ blake3_hasher_init(&hasher);
+
+ // Read input bytes from stdin.
+ unsigned char buf[65536];
+ size_t n;
+ while ((n = fread(buf, 1, 65536, stdin)) > 0) {
+ blake3_hasher_update(&hasher, buf, n);
+ }
+
+ // Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes.
+ uint8_t output[BLAKE3_OUT_LEN];
+ blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
+
+ // Print the hash as hexadecimal.
+ for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) {
+ printf("%02x", output[i]);
+ }
+ printf("\n");
+ return 0;
}
```
+If you save the example code above as `example.c`, and you're on x86,
+you can compile a working binary like this:
+
+```bash
+gcc -c -O3 -msse4.1 blake3_sse41.c -o blake3_sse41.o
+gcc -c -O3 -mavx2 blake3_avx2.c -o blake3_avx2.o
+gcc -c -O3 -mavx512f -mavx512vl blake3_avx512.c -o blake3_avx512.o
+gcc -O3 example.c blake3.c blake3_dispatch.c blake3_portable.c \
+ blake3_avx2.o blake3_avx512.o blake3_sse41.o -o blake3
+```
+
## Building
The Makefile included in this implementation is for testing. It's