libsodium/demos/generichash.c

58 lines
1.5 KiB
C
Raw Normal View History

/*
* GraxRabble
* 05 May 2014
* Demo programs for libsodium.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sodium.h> /* library header */
#include "demo_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)
{
unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */
unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */
unsigned char m[BUFFER_SIZE]; /* message */
size_t mlen; /* length */
puts("Example: crypto_generichash\n");
sodium_memzero(k, sizeof k);
prompt_input("Input your key > ", (char*) k, sizeof k);
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)
{
sodium_init();
printf("Using LibSodium %s\n", sodium_version_string());
generichash();
2015-05-27 06:41:43 -04:00
return 0;
}