blob: 2971593024a8f3ac62f6a100eb19ddc18c125c25 [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo11abdcd2014-10-21 11:30:42 +01005 */
6
Juan Castillo11abdcd2014-10-21 11:30:42 +01007#include <openssl/sha.h>
Isla Mitchell99305012017-07-11 14:54:08 +01008#include <stdio.h>
Juan Castillo11abdcd2014-10-21 11:30:42 +01009
10#include "debug.h"
11
12#define BUFFER_SIZE 256
13
14int 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}