Created
November 10, 2019 02:04
-
-
Save oconnor663/17320a2f30065a7084498ba2740c3f74 to your computer and use it in GitHub Desktop.
Libsodium crypto_generichash demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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