Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
      3  *
      4  * SPDX-License-Identifier: BSD-3-Clause
      5  */
      6 
      7 #include <openssl/sha.h>
      8 #include <stdio.h>
      9 
     10 #include "debug.h"
     11 
     12 #define BUFFER_SIZE	256
     13 
     14 int sha_file(const char *filename, unsigned char *md)
     15 {
     16 	FILE *inFile;
     17 	SHA256_CTX shaContext;
     18 	int bytes;
     19 	unsigned char data[BUFFER_SIZE];
     20 
     21 	if ((filename == NULL) || (md == NULL)) {
     22 		ERROR("%s(): NULL argument\n", __FUNCTION__);
     23 		return 0;
     24 	}
     25 
     26 	inFile = fopen(filename, "rb");
     27 	if (inFile == NULL) {
     28 		ERROR("Cannot read %s\n", filename);
     29 		return 0;
     30 	}
     31 
     32 	SHA256_Init(&shaContext);
     33 	while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
     34 		SHA256_Update(&shaContext, data, bytes);
     35 	}
     36 	SHA256_Final(md, &shaContext);
     37 
     38 	fclose(inFile);
     39 	return 1;
     40 }
     41