summaryrefslogtreecommitdiff
path: root/detect_single_char_xor_s1.c
blob: 5f416d6f3901a3739bddd38b763bd67d1c50ed75 (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
// https://cryptopals.com/sets/1/challenges/4
#include "util/hex.h"
#include "util/score.h"
#include "util/xor.h"
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  char *line = NULL;
  size_t capacity = 0;
  char *curr_best_line, *overall_best_line;
  int best_so_far = INT_MAX;
  FILE *f = fopen("data/c4_s1.txt", "rb");
  while (getline(&line, &capacity, f) != -1) {
    *strrchr(line, '\n') = '\0';
    int curr_score = best_score_hex(line, &curr_best_line);
    if (curr_score < best_so_far) {
      best_so_far = curr_score;
      overall_best_line = strdup(curr_best_line);
    }
    free(curr_best_line);
  }
  char *actual = overall_best_line;
  char *expect = "Now that the party is jumping\n";
  if (strcmp(actual, expect) != 0) {
    printf("does not match!!!\n"
           "expected: %s\n"
           "received: %s\n",
           expect, actual);
    return 1;
  } else
    printf("passed :^)\n%s\n", expect);
  return 0;
}