2014-09-24 17:40:21 -04:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
2015-05-27 15:36:54 -04:00
|
|
|
* example programs for libsodium.
|
2014-09-24 17:40:21 -04:00
|
|
|
*/
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 15:36:54 -04:00
|
|
|
#include "utils.h" /* utility functions shared by examples */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
2015-05-27 12:10:28 -04:00
|
|
|
* This function computes a fixed-length fingerprint for an arbitrary long message.
|
2014-09-24 17:40:21 -04:00
|
|
|
*
|
2015-05-27 12:10:28 -04: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 17:40:21 -04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
generichash(void)
|
|
|
|
{
|
2015-05-27 12:10:28 -04:00
|
|
|
unsigned char key[crypto_generichash_KEYBYTES_MAX];
|
2015-05-27 14:35:40 -04:00
|
|
|
unsigned char hash[crypto_generichash_BYTES];
|
2015-05-27 15:19:17 -04:00
|
|
|
unsigned char message[MAX_INPUT_LEN];
|
2015-05-27 12:10:28 -04:00
|
|
|
size_t message_len;
|
|
|
|
size_t key_len;
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Example: crypto_generichash\n");
|
|
|
|
|
2015-05-27 12:10:28 -04: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 17:40:21 -04:00
|
|
|
|
|
|
|
printf("Hashing message with %s\n", crypto_generichash_primitive());
|
2015-05-27 12:10:28 -04: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 15:09:32 -04:00
|
|
|
printf("Hash: ");
|
2015-05-27 12:10:28 -04:00
|
|
|
print_hex(hash, sizeof hash);
|
|
|
|
}
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 06:41:43 -04:00
|
|
|
main(void)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
2015-05-27 10:10:07 -04:00
|
|
|
init();
|
2014-09-24 17:40:21 -04:00
|
|
|
generichash();
|
2015-05-27 10:10:07 -04:00
|
|
|
|
2015-05-27 06:41:43 -04:00
|
|
|
return 0;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|