continue work on 1-6

This commit is contained in:
Evan Burkey 2024-06-15 15:56:50 -07:00
parent 5d4bf996c2
commit f7f68e7b2d
3 changed files with 68 additions and 4 deletions

View File

@ -1,6 +1,8 @@
CMAKE_OPTS=-DCMAKE_EXPORT_COMPILE_COMMANDS=1 CMAKE_OPTS=-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DCMAKE_BUILD_TYPE=Debug
all: all: clean run
build:
mkdir -p build mkdir -p build
cd build && \ cd build && \
cmake ${CMAKE_OPTS} .. && \ cmake ${CMAKE_OPTS} .. && \
@ -10,3 +12,7 @@ all:
clean: clean:
rm -rf build rm -rf build
rm -f compile_commands.json rm -f compile_commands.json
run: build
build/cryptopals

@ -1 +1 @@
Subproject commit 0318221f3ff621ba046eb8b4385f07e790912cdb Subproject commit a99d1d3784eebf2400653df97968a5584f1e20d8

View File

@ -2,8 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include "lfcrypto.h" #include "lfcrypto.h"
#include "lfvector.h"
#include "lfinput.h" #include "lfinput.h"
#include "lfparsing.h" #include "lfparsing.h"
#include "helpers.h" #include "helpers.h"
@ -98,8 +100,64 @@ static void challenge5() {
free(answer); free(answer);
} }
static void challenge6() { static void challenge6_vecdestroy(void *v) {
if (v == NULL) {
return;
}
vec_destroy((Vector *)v);
free(v);
}
static void challenge6() {
char *input = get_input("input/1-6");
size_t encoded_sz = 0;
unsigned char *encoded = b64_decode(input, strlen(input), &encoded_sz);
unsigned int smallest = UINT_MAX;
size_t keysize = 0;
for (size_t k = 2; k <= 20; ++k) {
char a[41], b[41];
strncpy(a, encoded, k);
strncpy(b, encoded + k, k);
unsigned int hd = hamming_distance_s(a, b) / (unsigned int)keysize;
if (hd < smallest) {
smallest = hd;
keysize = k;
}
}
/*
for (unsigned char *c = encoded; *c != '\0'; ++c) {
printf("%02x", *c);
}
printf("\n");
*/
/*
Vector *vec = malloc(sizeof(Vector));
vec_init(vec, challenge6_vecdestroy);
// Account for remainders
size_t vec_sz = encoded_sz / keysize;
if (encoded_sz % keysize != 0) {
++vec_sz;
}
for (size_t i = 0; i < vec_sz; ++i) {
Vector *vec_t = malloc(sizeof(Vector));
vec_init(vec_t, free);
vec_push(vec, (void *)vec_t);
}
for (size_t i = 0; i < encoded_sz; ++i) {
unsigned char *c = malloc(sizeof(unsigned char));
*c = encoded[i];
vec_at(vec, i % keysize) = c;
}
vec_destroy(vec);
*/
free(encoded);
free(input);
} }
void set1() { void set1() {