1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 #ifndef LTC_NO_FILE
13 /**
14   @file hash_file.c
15   Hash a file, Tom St Denis
16 */
17 
18 /**
19   @param hash   The index of the hash desired
20   @param fname  The name of the file you wish to hash
21   @param out    [out] The destination of the digest
22   @param outlen [in/out] The max size and resulting size of the message digest
23   @result CRYPT_OK if successful
24 */
hash_file(int hash,const char * fname,unsigned char * out,unsigned long * outlen)25 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen)
26 {
27     FILE *in;
28     int err;
29     LTC_ARGCHK(fname  != NULL);
30     LTC_ARGCHK(out    != NULL);
31     LTC_ARGCHK(outlen != NULL);
32 
33     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
34         return err;
35     }
36 
37     in = fopen(fname, "rb");
38     if (in == NULL) {
39        return CRYPT_FILE_NOTFOUND;
40     }
41 
42     err = hash_filehandle(hash, in, out, outlen);
43     if (fclose(in) != 0) {
44        return CRYPT_ERROR;
45     }
46 
47     return err;
48 }
49 #endif /* #ifndef LTC_NO_FILE */
50 
51 
52 /* ref:         $Format:%D$ */
53 /* git commit:  $Format:%H$ */
54 /* commit time: $Format:%ai$ */
55