1
0
Fork 0
mirror of https://github.com/BLAKE3-team/BLAKE3 synced 2024-03-29 15:29:59 +01:00

include example.c

This commit is contained in:
Jack O'Connor 2020-10-20 12:27:57 -04:00
parent dae5dc5ef3
commit 09546a677d
5 changed files with 39 additions and 2 deletions

View File

@ -201,3 +201,8 @@ jobs:
working-directory: ./c
- run: BLAKE3_NO_SSE2=1 BLAKE3_NO_SSE41=1 BLAKE3_NO_AVX2=1 BLAKE3_NO_AVX512=1 make -f Makefile.testing test_asm
working-directory: ./c
# Restore the files we deleted above.
- run: git checkout .
# Build the example.
- run: make -f Makefile.testing example
working-directory: ./c

1
c/.gitignore vendored
View File

@ -1,2 +1,3 @@
blake3
example
*.o

View File

@ -71,5 +71,8 @@ test_asm: CFLAGS += -DBLAKE3_TESTING -fsanitize=address,undefined
test_asm: asm
./test.py
example: example.c blake3.c blake3_dispatch.c blake3_portable.c $(ASM_TARGETS)
$(CC) $(CFLAGS) $(EXTRAFLAGS) $^ -o $@ $(LDFLAGS)
clean:
rm -f $(NAME) *.o

View File

@ -35,8 +35,9 @@ int main() {
}
```
If you save the example code above as `example.c`, and you're on x86\_64
with a Unix-like OS, you can compile a working binary like this:
The code above is included in this directory as `example.c`. If you're
on x86\_64 with a Unix-like OS, you can compile a working binary like
this:
```bash
gcc -O3 -o example example.c blake3.c blake3_dispatch.c blake3_portable.c \

27
c/example.c Normal file
View File

@ -0,0 +1,27 @@
#include "blake3.h"
#include <stdio.h>
#include <unistd.h>
int main() {
// Initialize the hasher.
blake3_hasher hasher;
blake3_hasher_init(&hasher);
// Read input bytes from stdin.
unsigned char buf[65536];
ssize_t n;
while ((n = read(STDIN_FILENO, buf, sizeof(buf))) > 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;
}