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 11:17:36 -04:00
|
|
|
* Many applications and programming language implementations were
|
|
|
|
* recently found to be vulnerable to denial-of-service attacks when
|
|
|
|
* a hash function with weak security guarantees, such as Murmurhash
|
|
|
|
* 3, was used to construct a hash table.
|
|
|
|
*
|
|
|
|
* In order to address this, Sodium provides the crypto_shorthash()
|
|
|
|
* function, which outputs short but unpredictable (without knowing
|
|
|
|
* the secret key) values suitable for picking a list in a hash table
|
|
|
|
* for a given key.
|
|
|
|
*
|
|
|
|
* This function is optimized for short inputs.
|
|
|
|
*
|
|
|
|
* The output of this function is only 64 bits. Therefore, it should
|
|
|
|
* not be considered collision-resistant.
|
|
|
|
*
|
|
|
|
* Use cases:
|
|
|
|
*
|
|
|
|
* - Hash tables
|
|
|
|
* - Probabilistic data structures such as Bloom filters
|
|
|
|
* - Integrity checking in interactive protocols
|
2014-09-24 17:40:21 -04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
shorthash(void)
|
|
|
|
{
|
2015-05-27 11:17:36 -04:00
|
|
|
unsigned char key[crypto_shorthash_KEYBYTES];
|
|
|
|
unsigned char hash[crypto_shorthash_BYTES];
|
2015-05-27 15:19:17 -04:00
|
|
|
unsigned char message[MAX_INPUT_LEN];
|
2015-05-27 11:17:36 -04:00
|
|
|
size_t message_len;
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Example: crypto_shorthash\n");
|
|
|
|
|
2015-05-27 12:10:28 -04:00
|
|
|
prompt_input("a key", (char*)key, sizeof key, 0);
|
|
|
|
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 11:17:36 -04:00
|
|
|
printf("Hashing the message with %s\n", crypto_shorthash_primitive());
|
|
|
|
crypto_shorthash(hash, message, message_len, key);
|
2015-05-27 15:09:32 -04:00
|
|
|
printf("Hash: ");
|
2015-05-27 11:17:36 -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
|
|
|
shorthash();
|
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
|
|
|
}
|