hash_filehandle.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
  31. return err;
  32. }
  33. *outlen = hash_descriptor[hash].hashsize;
  34. do {
  35. x = fread(buf, 1, sizeof(buf), in);
  36. if ((err = hash_descriptor[hash].process(&md, buf, x)) != CRYPT_OK) {
  37. return err;
  38. }
  39. } while (x == sizeof(buf));
  40. err = hash_descriptor[hash].done(&md, dst);
  41. #ifdef CLEAN_STACK
  42. zeromem(buf, sizeof(buf));
  43. #endif
  44. return err;
  45. #endif
  46. }