Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Created November 10, 2019 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/17320a2f30065a7084498ba2740c3f74 to your computer and use it in GitHub Desktop.
Save oconnor663/17320a2f30065a7084498ba2740c3f74 to your computer and use it in GitHub Desktop.
Libsodium crypto_generichash demo
#include <sodium.h>
#include <stdio.h>
#include <unistd.h>
int main() {
if (sodium_init() < 0) {
printf("init error\n");
return 1;
}
unsigned char hash[crypto_generichash_BYTES];
crypto_generichash_state state;
crypto_generichash_init(&state, NULL, 0, sizeof(hash));
unsigned char buf[65536];
while (1) {
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
if (n < 0) {
printf("reading error\n");
return 1;
}
if (n == 0) {
break;
}
crypto_generichash_update(&state, buf, n);
}
crypto_generichash_final(&state, hash, sizeof hash);
for (int i = 0; i < crypto_generichash_BYTES; i++) {
printf("%02x", hash[i]);
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment