2020-10-20 18:27:57 +02:00
|
|
|
#include "blake3.h"
|
2021-08-24 18:09:32 +02:00
|
|
|
#include <errno.h>
|
2020-10-20 18:27:57 +02:00
|
|
|
#include <stdio.h>
|
2021-08-24 18:09:32 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-10-20 18:27:57 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
// Initialize the hasher.
|
|
|
|
blake3_hasher hasher;
|
|
|
|
blake3_hasher_init(&hasher);
|
|
|
|
|
|
|
|
// Read input bytes from stdin.
|
|
|
|
unsigned char buf[65536];
|
2021-08-24 18:09:32 +02:00
|
|
|
while (1) {
|
|
|
|
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
|
|
|
|
if (n > 0) {
|
|
|
|
blake3_hasher_update(&hasher, buf, n);
|
|
|
|
} else if (n == 0) {
|
|
|
|
break; // end of file
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "read failed: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-10-20 18:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|