libsodium/examples/generichash.c

81 lines
2.5 KiB
C
Raw Normal View History

/*
* GraxRabble
2015-05-27 15:36:54 -04:00
* example programs for libsodium.
*/
2015-05-27 07:02:56 -04:00
#include <sodium.h> /* library header */
2015-05-27 15:36:54 -04:00
#include "utils.h" /* utility functions shared by examples */
/*
* This function computes a fixed-length fingerprint for an arbitrary long message.
*
* 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).
*/
void
generichash(void)
{
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];
size_t message_len;
size_t key_len;
puts("Example: crypto_generichash\n");
key_len = prompt_input("a key", (char*)key, sizeof key, 1);
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
printf("Hashing message with %s\n", crypto_generichash_primitive());
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 {
printf("Hash: ");
print_hex(hash, sizeof hash);
}
}
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;
}