generichash_stream example

This commit is contained in:
Frank Denis 2015-05-27 20:35:40 +02:00
parent b1bcecf086
commit 9b209f0078
3 changed files with 64 additions and 61 deletions

View File

@ -53,7 +53,7 @@ void
generichash(void) generichash(void)
{ {
unsigned char key[crypto_generichash_KEYBYTES_MAX]; 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]; unsigned char message[MAX_INPUT_SIZE];
size_t message_len; size_t message_len;
size_t key_len; size_t key_len;

View File

@ -0,0 +1,63 @@
/*
* GraxRabble
* Demo programs for libsodium.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sodium.h> /* 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;
}

View File

@ -1,60 +0,0 @@
/*
* GraxRabble
* Demo programs for libsodium.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sodium.h> /* 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;
}