aboutsummaryrefslogtreecommitdiff
path: root/05/src/common.c
blob: b09c06eb13ffa386fdb6e0f7bd93cea9fa27d701 (plain)
1
2
3
4
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
// common.c --- shared between client and server
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// Commentary:
//
// Code:

#include "common.h"
#include "utils.h"

bool
load_aes_256_key (aes_256_key key, const char *filename)
{
  FILE *stream = fopen (filename, "r");
  if (stream == NULL)
    return false;

  // TODO(cmmm): handle wrong format better...
  int assignments = 0;
  for (int i = 0; i < AES_256_KEY_SIZE; ++i)
    assignments += fscanf (stream, "%02hhx", &key[i]);
  fclose (stream);

  return assignments == AES_256_KEY_SIZE;
}

void
print_hex (uint8_t *data, size_t size)
{
  for (size_t i = 0; i < size; ++i) {
    if (i && i % 2 == 0) printf (" ");
    printf ("%02x", data[i]);
  } printf ("\n");
}

// common.c ends here