From 9b209f0078daa9361fc1c373a4fef59983587611 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:35:40 +0200 Subject: [PATCH] generichash_stream example --- demos/generichash.c | 2 +- demos/generichash_stream.c | 63 ++++++++++++++++++++++++++++++++++++++ demos/generichashstream.c | 60 ------------------------------------ 3 files changed, 64 insertions(+), 61 deletions(-) create mode 100644 demos/generichash_stream.c delete mode 100644 demos/generichashstream.c diff --git a/demos/generichash.c b/demos/generichash.c index 1e4d15fc..ea2df02f 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -53,7 +53,7 @@ void generichash(void) { unsigned char key[crypto_generichash_KEYBYTES_MAX]; - unsigned char hash[crypto_generichash_BYTES_MIN]; + unsigned char hash[crypto_generichash_BYTES]; unsigned char message[MAX_INPUT_SIZE]; size_t message_len; size_t key_len; diff --git a/demos/generichash_stream.c b/demos/generichash_stream.c new file mode 100644 index 00000000..5e666f77 --- /dev/null +++ b/demos/generichash_stream.c @@ -0,0 +1,63 @@ +/* + * GraxRabble + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "utils.h" /* utility functions shared by demos */ + +/* + * Streaming variant of generic hash. This has the ability to hash + * data in chunks at a time and compute the same result as hashing + * all of the data at once. + */ +void +generichash_stream(void) +{ + unsigned char key[crypto_generichash_KEYBYTES_MAX]; + unsigned char hash[crypto_generichash_BYTES]; + unsigned char message_part[MAX_INPUT_SIZE]; + crypto_generichash_state state; + size_t message_part_len; + + puts("Example: crypto_generichashstream\n"); + + prompt_input("a key", (char*)key, sizeof key, 1); + putchar('\n'); + + printf("Hashing message with %s\n", crypto_generichash_primitive()); + + /* initialize the stream */ + if (crypto_generichash_init(&state, key, sizeof key, sizeof hash) != 0) { + puts("Couldn't hash the message, probably due to the key length"); + exit(EXIT_FAILURE); + } + + for(;;) { + message_part_len = prompt_input("the next part of the message", + (char*)message_part, sizeof message_part, 1); + if (message_part_len == 0) + break; + + /* keep appending data */ + crypto_generichash_update(&state, message_part, message_part_len); + } + crypto_generichash_final(&state, hash, sizeof hash); + + fputs("Hash: ", stdout); + print_hex(hash, sizeof hash); + putchar('\n'); +} + +int +main(void) +{ + init(); + generichash_stream(); + + return 0; +} diff --git a/demos/generichashstream.c b/demos/generichashstream.c deleted file mode 100644 index 1e3b84c8..00000000 --- a/demos/generichashstream.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * GraxRabble - * Demo programs for libsodium. - */ -#include -#include -#include - -#include /* library header */ - -#include "utils.h" /* utility functions shared by demos */ - -/* - * Streaming variant of generic hash. This has the ability to hash - * data in chunks at a time and compute the same result as hashing - * all of the data at once. - */ -void -generichashstream(void) -{ - unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ - unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ - crypto_generichash_state state; /* hash stream */ - unsigned char m[MAX_INPUT_SIZE]; /* input buffer */ - size_t mlen; /* input length */ - - puts("Example: crypto_generichashstream\n"); - - prompt_input("a key", (char*)k, sizeof k, 0); - putchar('\n'); - - printf("Hashing message with %s\n", crypto_generichash_primitive()); - - /* initialize the stream */ - crypto_generichash_init(&state, k, sizeof k, sizeof h); - - for(;;) { - mlen = prompt_input("the next part of the message", (char*)m, sizeof m); - if (mlen == 0) - break; - - /* keep appending data */ - crypto_generichash_update(&state, m, mlen); - } - crypto_generichash_final(&state, h, sizeof h); - putchar('\n'); - - fputs("Hash: ", stdout); - print_hex(h, sizeof h); - putchar('\n'); -} - -int -main(void) -{ - init(); - generichashstream(); - - return 0; -}