2014-09-24 14:40:21 -07:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
|
|
|
* Demo programs for libsodium.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-05-27 04:02:56 -07:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 06:47:49 -07:00
|
|
|
#include "utils.h" /* utility functions shared by demos */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
|
|
|
/*
|
2015-05-27 09:10:28 -07:00
|
|
|
* This function computes a fixed-length fingerprint for an arbitrary long message.
|
2014-09-24 14:40:21 -07:00
|
|
|
*
|
2015-05-27 09:10:28 -07:00
|
|
|
* Sample use cases:
|
|
|
|
*
|
|
|
|
* File integrity checking
|
|
|
|
* Creating unique identifiers to index arbitrary long data
|
|
|
|
*
|
|
|
|
* The crypto_generichash() function puts a fingerprint of the
|
|
|
|
* message in whose length is inlen bytes into out. The output size
|
|
|
|
* can be chosen by the application.
|
|
|
|
*
|
|
|
|
* The minimum recommended output size is crypto_generichash_BYTES.
|
|
|
|
* This size makes it practically impossible for two messages to
|
|
|
|
* produce the same fingerprint.
|
|
|
|
*
|
|
|
|
* But for specific use cases, the size can be any value between
|
|
|
|
* crypto_generichash_BYTES_MIN (included) and
|
|
|
|
* crypto_generichash_BYTES_MAX (included).
|
|
|
|
*
|
|
|
|
* key can be NULL and keylen can be 0. In this case, a message will
|
|
|
|
* always have the same fingerprint, similar to the MD5 or SHA-1
|
|
|
|
* functions for which crypto_generichash() is a faster and more
|
|
|
|
* secure alternative.
|
|
|
|
*
|
|
|
|
* But a key can also be specified. A message will always have the
|
|
|
|
* same fingerprint for a given key, but different keys used to hash
|
|
|
|
* the same message are very likely to produce distinct fingerprints.
|
|
|
|
*
|
|
|
|
* In particular, the key can be used to make sure that different
|
|
|
|
* applications generate different fingerprints even if they process
|
|
|
|
* the same data.
|
|
|
|
*
|
|
|
|
* The recommended key size is crypto_generichash_KEYBYTES bytes.
|
|
|
|
*
|
|
|
|
* However, the key size can by any value between
|
|
|
|
* crypto_generichash_KEYBYTES_MIN (included) and
|
|
|
|
* crypto_generichash_KEYBYTES_MAX (included).
|
2014-09-24 14:40:21 -07:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
generichash(void)
|
|
|
|
{
|
2015-05-27 09:10:28 -07:00
|
|
|
unsigned char key[crypto_generichash_KEYBYTES_MAX];
|
2015-05-27 11:35:40 -07:00
|
|
|
unsigned char hash[crypto_generichash_BYTES];
|
2015-05-27 09:10:28 -07:00
|
|
|
unsigned char message[MAX_INPUT_SIZE];
|
|
|
|
size_t message_len;
|
|
|
|
size_t key_len;
|
2014-09-24 14:40:21 -07:00
|
|
|
|
|
|
|
puts("Example: crypto_generichash\n");
|
|
|
|
|
2015-05-27 09:10:28 -07:00
|
|
|
key_len = prompt_input("a key", (char*)key, sizeof key, 1);
|
|
|
|
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
2014-09-24 14:40:21 -07:00
|
|
|
|
|
|
|
printf("Hashing message with %s\n", crypto_generichash_primitive());
|
2015-05-27 09:10:28 -07:00
|
|
|
if (crypto_generichash(hash, sizeof hash, message, message_len,
|
|
|
|
key, key_len) != 0) {
|
|
|
|
puts("Couldn't hash the message, probably due to the key length");
|
|
|
|
} else {
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Hash: ");
|
2015-05-27 09:10:28 -07:00
|
|
|
print_hex(hash, sizeof hash);
|
|
|
|
}
|
2014-09-24 14:40:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 03:41:43 -07:00
|
|
|
main(void)
|
2014-09-24 14:40:21 -07:00
|
|
|
{
|
2015-05-27 07:10:07 -07:00
|
|
|
init();
|
2014-09-24 14:40:21 -07:00
|
|
|
generichash();
|
2015-05-27 07:10:07 -07:00
|
|
|
|
2015-05-27 03:41:43 -07:00
|
|
|
return 0;
|
2014-09-24 14:40:21 -07:00
|
|
|
}
|