Linux repositories inspector




Development files for libkeccak


libkeccak_sha3sum_fd - Calculate a SHA-3 hashsum of a file


#include <libkeccak.h>

int libkeccak_sha3sum_fd(int fd, struct libkeccak_state *state, long int output, void *hashsum);
Link with -lkeccak.


The libkeccak_sha3sum_fd() function calculates a SHA-3 hashsum of a file, whose file desriptor is specified by fd (and should be at the beginning of the file.) The hash algorithm is tuned by the output parameter; it specifies the output size, in bits.
The hash is stored in binary form to hashsum. hashsum should have an allocation size of at least (((output + 7) / 8) * sizeof(char)).
*state should not be initialised. libkeccak_sha3sum_fd() initialises *state itself. Therefore there would be a memory leak if *state is already initialised.


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


The libkeccak_sha3sum_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).


Be aware, libkeccak_sha3sum_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_sha3sum_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_sha3sum_fd() does not stop if interrupted (read(2) returns EINTR.)
libkeccak_sha3sum_fd() assumes all information is non-sensitive, and will therefore not perform any secure erasure of information.
libkeccak_sha3sum_fd() does not validate the tuning of the algorithm.


This example calculates the SHA3-256 hash of the input from stdin, and prints the hash, in hexadecimal form, to stdout.
struct libkeccak_state state;
if (libkeccak_sha3sum_fd(STDIN_FILENO, &state, 256, binhash) < 0)
        goto fail;
libkeccak_behex_lower(hexhash, binhash, sizeof(binhash));
printf(\(dq%s\n\(dq, hexhash);
⇧ Top