1/*
2 * Copyright 2017, DornerWorks
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * This data was produced by DornerWorks, Ltd. of Grand Rapids, MI, USA under
7 * a DARPA SBIR, Contract Number D16PC00107.
8 *
9 * Approved for Public Release, Distribution Unlimited.
10 *
11 */
12
13#include <printf.h>
14#include <types.h>
15
16#include "../hash.h"
17
18/* Function to perform all hash operations.
19 *
20 * The outputted hash is stored in the outputted_hash pointer after the "sum" operation is used.
21 *
22 * This way beats having a bunch of #ifdefs in the source code, and is scalable to any other
23 * hashing algoritm
24 */
25void get_hash(hashes_t hashes, const void *file_to_hash, unsigned long bytes_to_hash, uint8_t *outputted_hash)
26{
27    if (hashes.hash_type == SHA_256) {
28        sha256_t calculated_hash = hashes.sha_structure;
29        sha256_init(&calculated_hash);
30        sha256_update(&calculated_hash, file_to_hash, bytes_to_hash);
31        sha256_sum(&calculated_hash, outputted_hash);
32    } else {
33        md5_t calculated_hash = hashes.md5_structure;
34        md5_init(&calculated_hash);
35        md5_update(&calculated_hash, file_to_hash, bytes_to_hash);
36        md5_sum(&calculated_hash, outputted_hash);
37    }
38}
39
40/* Function to print the hash */
41void print_hash(uint8_t *hash_to_print, int bytes_to_print)
42{
43    for (int i = 0; i < bytes_to_print; i++) {
44        printf("%02x", *hash_to_print++);
45    }
46    printf("\n");
47}
48