1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 #ifndef LTC_NO_FILE
6 /**
7   @file hash_file.c
8   Hash a file, Tom St Denis
9 */
10 
11 /**
12   @param hash   The index of the hash desired
13   @param fname  The name of the file you wish to hash
14   @param out    [out] The destination of the digest
15   @param outlen [in/out] The max size and resulting size of the message digest
16   @result CRYPT_OK if successful
17 */
hash_file(int hash,const char * fname,unsigned char * out,unsigned long * outlen)18 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen)
19 {
20     FILE *in;
21     int err;
22     LTC_ARGCHK(fname  != NULL);
23     LTC_ARGCHK(out    != NULL);
24     LTC_ARGCHK(outlen != NULL);
25 
26     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
27         return err;
28     }
29 
30     in = fopen(fname, "rb");
31     if (in == NULL) {
32        return CRYPT_FILE_NOTFOUND;
33     }
34 
35     err = hash_filehandle(hash, in, out, outlen);
36     if (fclose(in) != 0) {
37        return CRYPT_ERROR;
38     }
39 
40     return err;
41 }
42 #endif /* #ifndef LTC_NO_FILE */
43 
44