mirror of
https://github.com/BLAKE3-team/BLAKE3
synced 2024-05-12 14:46:15 +02:00
update the c/README.md example to hash stdin
This commit is contained in:
parent
37e153cc60
commit
af0ef07519
51
c/README.md
51
c/README.md
|
@ -22,34 +22,49 @@ struct and five functions in [`blake3.h`](blake3.h):
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
Here's an example program that hashes bytes from standard input and
|
||||||
|
prints the result:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include <stdio.h>
|
|
||||||
#include "blake3.h"
|
#include "blake3.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Initialize the hasher.
|
// Initialize the hasher.
|
||||||
blake3_hasher hasher;
|
blake3_hasher hasher;
|
||||||
blake3_hasher_init(&hasher);
|
blake3_hasher_init(&hasher);
|
||||||
|
|
||||||
// Write some input bytes.
|
// Read input bytes from stdin.
|
||||||
blake3_hasher_update(&hasher, "foo", 3);
|
unsigned char buf[65536];
|
||||||
blake3_hasher_update(&hasher, "bar", 3);
|
size_t n;
|
||||||
blake3_hasher_update(&hasher, "baz", 3);
|
while ((n = fread(buf, 1, 65536, stdin)) > 0) {
|
||||||
|
blake3_hasher_update(&hasher, buf, n);
|
||||||
|
}
|
||||||
|
|
||||||
// Finalize the hash. BLAKE3_OUT_LEN is 32 bytes, but extended outputs are
|
// Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes.
|
||||||
// also supported.
|
uint8_t output[BLAKE3_OUT_LEN];
|
||||||
uint8_t output[BLAKE3_OUT_LEN];
|
blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
|
||||||
blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
|
|
||||||
|
|
||||||
// Print the hash as hexadecimal.
|
// Print the hash as hexadecimal.
|
||||||
for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) {
|
for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) {
|
||||||
printf("%02x", output[i]);
|
printf("%02x", output[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return 0;
|
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
|
## Building
|
||||||
|
|
||||||
The Makefile included in this implementation is for testing. It's
|
The Makefile included in this implementation is for testing. It's
|
||||||
|
|
Loading…
Reference in New Issue