Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 7 | #include <openssl/sha.h> |
Isla Mitchell | 9930501 | 2017-07-11 14:54:08 +0100 | [diff] [blame] | 8 | #include <stdio.h> |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 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 | } |