libsodium/demos/generichash.c

53 lines
1.3 KiB
C
Raw Normal View History

/*
* 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 */
#include "utils.h" /* utility functions shared by demos */
/*
* 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 */
unsigned char m[MAX_INPUT_SIZE]; /* message */
2015-05-27 07:02:56 -04:00
size_t mlen; /* length */
puts("Example: crypto_generichash\n");
memset(k, 0, sizeof k);
2015-05-27 07:02:56 -04:00
prompt_input("Input your key > ", (char*)k, sizeof k);
2015-05-27 07:02:56 -04:00
mlen = prompt_input("Input your message > ", (char*)m, sizeof m);
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)
{
2015-05-27 10:10:07 -04:00
init();
generichash();
2015-05-27 10:10:07 -04:00
2015-05-27 06:41:43 -04:00
return 0;
}