LIBKECCAK_GENERALISED_SUM_FD(3)

NAME
libkeccak_generalised_sum_fd - Calculate the hash of a file

SYNOPSIS
#include <libkeccak.h>
int libkeccak_generalised_sum_fd(int fd, struct libkeccak_state *state, const struct libkeccak_spec *spec,
                                 const char *suffix, void *hashsum);

Link with -lkeccak.

DESCRIPTION
The libkeccak_generalised_sum_fd() function calculates the hash of a file, whose file desriptor is specified by fd (and should be at the beginning of the file.) The hash algorithm is specified by *spec and suffix, where *spec is the tuning of the algorithm and suffix is the bits append to the message (or NULL if none.)

The hash is stored in binary form to hashsum. hashsum should have an allocation size of at least (((spec->output + 7) / 8) * sizeof(char)).

*state should not be initialised unless spec is NULL. libkeccak_generalised_sum_fd() initialises *state itself unless spec is NULL. Therefore there would be a memory leak if *state is already initialised and spec is non-NULL.

RETURN VALUES
The libkeccak_generalised_sum_fd() function returns 0 upon successful completion. On error, -1 is returned and errno is set to describe the error.

ERRORS
The libkeccak_generalised_sum_fd() function may fail for any reason, except those resulting in errno being set to EINTR, specified for the functions read(2), malloc(3), and realloc(3).

NOTES
Be aware, libkeccak_generalised_sum_fd() hashes the file until the end has been reached. For pipes and sockets and this means until the file has been closed. But for character devices, this usually means never. Attempting to hash files in /dev is therefore usually a bad idea. libkeccak_generalised_sum_fd() does not check for the file length or file type before hashing as this could limit what you can do, and make the library more complex.

libkeccak_generalised_sum_fd() does not stop if interrupted (read(2) returns EINTR.)

libkeccak_generalised_sum_fd() assumes all information is non-sensitive, and will therefore not perform any secure erasure of information.

libkeccak_generalised_sum_fd() does not validate the tuning of the algorithm.

EXAMPLE
This example calculates the Keccak[b = 1024, c = 576, n = 256] hash of the input from stdin, and prints the hash, in hexadecimal form, to stdout.

struct libkeccak_state state;
struct libkeccak_spec spec;
char binhash[256 / 8];
char hexhash[256 / 8 * 2 + 1];
spec.bitrate = 1024;
spec.capacity = 576;
spec.output = 256;
if (libkeccak_generalised_sum_fd(STDIN_FILENO, &state, &spec, NULL, binhash) < 0)
	goto fail;
libkeccak_behex_lower(hexhash, binhash, sizeof(binhash));
printf("%s\n", hexhash);
libkeccak_state_destroy(&state);

SEE ALSO
libkeccak_behex_lower(3), libkeccak_behex_upper(3), libkeccak_keccaksum_fd(3), libkeccak_sha3sum_fd(3), libkeccak_rawshakesum_fd(3), libkeccak_shakesum_fd(3), libkeccak_spec_cshake(3), libkeccak_spec_sha3(3), libkeccak_spec_shake(3), libkeccak_spec_rawshake(3), libkeccak_spec_check(3), libkeccak_generalised_spec_initialise(3), libkeccak_state_initialise(3)