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 07:02:56 -04:00
|
|
|
#include "demo_utils.h" /* utility functions shared by demos */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic hash is intended as a variable output hash with enough strength
|
|
|
|
* to ensure data integrity. The hash out put is also able to vary in size.
|
|
|
|
* Key is optional and is able to vary in size.
|
|
|
|
*
|
|
|
|
* Note that it is recommended to stay within the range of MIN and MAX
|
|
|
|
* output because larger output will produce gaps.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
generichash(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 */
|
2015-05-27 09:39:34 -04:00
|
|
|
unsigned char m[MAX_INPUT_SIZE]; /* message */
|
2015-05-27 07:02:56 -04:00
|
|
|
size_t mlen; /* length */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Example: crypto_generichash\n");
|
|
|
|
|
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
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
mlen = prompt_input("Input your message > ", (char*)m, sizeof m);
|
2014-09-24 17:40:21 -04:00
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
printf("Hashing message with %s\n", crypto_generichash_primitive());
|
|
|
|
crypto_generichash(h, sizeof h, m, mlen, k, sizeof k);
|
|
|
|
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
|
|
|
{
|
|
|
|
sodium_init();
|
|
|
|
printf("Using LibSodium %s\n", sodium_version_string());
|
|
|
|
|
|
|
|
generichash();
|
2015-05-27 06:41:43 -04:00
|
|
|
return 0;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|