hash_file.c 914 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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_file(int hash, const char *fname, unsigned char *dst, unsigned long *outlen)
  13. {
  14. #ifdef NO_FILE
  15. return CRYPT_NOP;
  16. #else
  17. FILE *in;
  18. int err;
  19. _ARGCHK(fname != NULL);
  20. _ARGCHK(dst != NULL);
  21. _ARGCHK(outlen != NULL);
  22. if ((err = hash_is_valid(hash)) != CRYPT_OK) {
  23. return err;
  24. }
  25. in = fopen(fname, "rb");
  26. if (in == NULL) {
  27. return CRYPT_FILE_NOTFOUND;
  28. }
  29. err = hash_filehandle(hash, in, dst, outlen);
  30. if (fclose(in) != 0) {
  31. return CRYPT_ERROR;
  32. }
  33. return err;
  34. #endif
  35. }