hash_filehandle.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include "mycrypt.h"
  12. int hash_filehandle(int hash, FILE *in, unsigned char *dst, unsigned long *outlen)
  13. {
  14. #ifdef NO_FILE
  15. return CRYPT_NOP;
  16. #else
  17. hash_state md;
  18. unsigned char buf[512];
  19. size_t x;
  20. int err;
  21. _ARGCHK(dst != NULL);
  22. _ARGCHK(outlen != NULL);
  23. _ARGCHK(in != NULL);
  24. if ((err = hash_is_valid(hash)) != CRYPT_OK) {
  25. return err;
  26. }
  27. if (*outlen < hash_descriptor[hash].hashsize) {
  28. return CRYPT_BUFFER_OVERFLOW;
  29. }
  30. *outlen = hash_descriptor[hash].hashsize;
  31. hash_descriptor[hash].init(&md);
  32. do {
  33. x = fread(buf, 1, sizeof(buf), in);
  34. if ((err = hash_descriptor[hash].process(&md, buf, x)) != CRYPT_OK) {
  35. return err;
  36. }
  37. } while (x == sizeof(buf));
  38. err = hash_descriptor[hash].done(&md, dst);
  39. #ifdef CLEAN_STACK
  40. zeromem(buf, sizeof(buf));
  41. #endif
  42. return err;
  43. #endif
  44. }