LIBKECCAK_...SED_SUM_FD(3)

πŸ“¦ version 1.4.3, in arch rolling, opensuse-leap 16.0

πŸ’… LIBKECCAK_...SED_SUM_FD(3) ✨

okay so this is a page from the Library Functions Manual and honestly? it's a glow-up for your hashing era. sit down, grab a strawberry drink πŸ“, we're learning about libkeccak_generalised_sum_fd.


🌸 NAME 🌸

libkeccak_generalised_sum_fd β€” literally just calculates the hash of a file, no cap πŸ’–


πŸ¦‹ 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);

and like, don't forget to link with -lkeccak or it's giving broken build energy 😭


πŸŽ€ DESCRIPTION πŸŽ€

so libkeccak_generalised_sum_fd() hashes a file for you β€” the file is the one whose file descriptor you pass in as fd, and bestie, that descriptor should be at the beginning of the file before you start, mkay? πŸ‘‘

the actual hash algorithm is picked by *spec and suffix, where *spec is the tuning of the algorithm and suffix is the bits appended to the message β€” or NULL if there are none, no pressure 🫢

the hash gets stored in binary form into hashsum. and hashsum needs an allocation size of at least:

(((spec->output + 7) / 8) * sizeof(char))

don't skimp on the memory, it's not cute πŸ’€

now the spicy part 🌢️ β€” *state should not be initialised unless spec is NULL. libkeccak_generalised_sum_fd() initialises *state itself unless spec is NULL. so if *state is already initialised and spec is non-NULL, you get a memory leak. not the vibe, watch out for it πŸ™ˆ


⭐ RETURN VALUES ⭐

  • returns 0 when everything slays (successful completion) ✨
  • on error it returns -1 and errno gets set to describe the error, so you know exactly what went wrong πŸ”

πŸ”₯ ERRORS πŸ”₯

libkeccak_generalised_sum_fd() may fail for any reason β€” the only exceptions are those that would set errno to EINTR, as specified for the functions read(2), malloc(3), and realloc(3). so like, it's a whole spectrum of possible failure, iykyk πŸ’…


🧚 NOTES 🧚 (read these fr fr)

  • be aware: libkeccak_generalised_sum_fd() hashes the file until the end has been reached. for pipes and sockets 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, because that could limit what you can do and make the library more complex. freedom, but at what cost 😭
  • libkeccak_generalised_sum_fd() does not stop if interrupted (read(2) returns EINTR). it's a stubborn icon, periodt.
  • 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. it trusts you. main character energy, honestly πŸ‘‘

🌈 EXAMPLE 🌈

this one calculates the Keccak[b = 1024, c = 576, n = 256] hash of the input from stdin, and prints the hash, in hexadecimal form, to stdout. iconic behaviour, look:

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);

copy it, run it, live it πŸ’œ


πŸ‘‘ SEE ALSO πŸ‘‘

go read these next, bestie, they're all part of the same friend group:


ok that's the whole page!! πŸ’… this diva comes to you from LIBKECCAK, dated LIBKECCAK_...SED_SUM_FD(3) β€” stay hashing, stay hydrated, mwah πŸŽ€πŸ’–βœ¨
πŸ“¦ where this page comes from