2014-09-24 17:40:21 -04:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
|
|
|
* Demo programs for libsodium.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 09:47:49 -04:00
|
|
|
#include "utils.h" /* utility functions shared by demos */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2015-05-27 07:02:56 -04:00
|
|
|
unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */
|
|
|
|
unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */
|
|
|
|
crypto_generichash_state state; /* hash stream */
|
2015-05-27 09:39:34 -04:00
|
|
|
unsigned char m[MAX_INPUT_SIZE]; /* input buffer */
|
2015-05-27 07:02:56 -04:00
|
|
|
size_t mlen; /* input length */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Example: crypto_generichashstream\n");
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 09:46:17 -04:00
|
|
|
memset(k, 0, sizeof k);
|
2015-05-27 07:02:56 -04:00
|
|
|
prompt_input("Input your key > ", (char*)k, sizeof k);
|
2014-09-24 17:40:21 -04:00
|
|
|
putchar('\n');
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2014-09-24 17:40:21 -04:00
|
|
|
printf("Hashing message with %s\n", crypto_generichash_primitive());
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2014-09-24 17:40:21 -04:00
|
|
|
/* initialize the stream */
|
|
|
|
crypto_generichash_init(&state, k, sizeof k, sizeof h);
|
|
|
|
|
|
|
|
while (1) {
|
2015-05-27 07:02:56 -04:00
|
|
|
mlen = prompt_input("> ", (char*)m, sizeof m);
|
|
|
|
if (mlen == 0)
|
|
|
|
break;
|
|
|
|
|
2014-09-24 17:40:21 -04:00
|
|
|
/* keep appending data */
|
|
|
|
crypto_generichash_update(&state, m, mlen);
|
|
|
|
}
|
|
|
|
crypto_generichash_final(&state, h, sizeof h);
|
|
|
|
putchar('\n');
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2014-09-24 17:40:21 -04:00
|
|
|
fputs("Hash: ", stdout);
|
|
|
|
print_hex(h, sizeof h);
|
|
|
|
putchar('\n');
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 06:41:43 -04:00
|
|
|
main(void)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
2015-05-27 10:10:07 -04:00
|
|
|
init();
|
2014-09-24 17:40:21 -04:00
|
|
|
generichashstream();
|
2015-05-27 10:10:07 -04:00
|
|
|
|
2015-05-27 06:41:43 -04:00
|
|
|
return 0;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|