sha.c 774 Bytes
Newer Older
1
2
3
/*
 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 */

#include <stdio.h>
#include <openssl/sha.h>

#include "debug.h"

#define BUFFER_SIZE	256

int sha_file(const char *filename, unsigned char *md)
{
	FILE *inFile;
	SHA256_CTX shaContext;
	int bytes;
	unsigned char data[BUFFER_SIZE];

	if ((filename == NULL) || (md == NULL)) {
		ERROR("%s(): NULL argument\n", __FUNCTION__);
		return 0;
	}

	inFile = fopen(filename, "rb");
	if (inFile == NULL) {
		ERROR("Cannot read %s\n", filename);
		return 0;
	}

	SHA256_Init(&shaContext);
	while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
		SHA256_Update(&shaContext, data, bytes);
	}
	SHA256_Final(md, &shaContext);

	fclose(inFile);
	return 1;
}