continue work on 1-6
This commit is contained in:
60
src/set1.c
60
src/set1.c
@ -2,8 +2,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "lfcrypto.h"
|
||||
#include "lfvector.h"
|
||||
#include "lfinput.h"
|
||||
#include "lfparsing.h"
|
||||
#include "helpers.h"
|
||||
@ -98,8 +100,64 @@ static void challenge5() {
|
||||
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() {
|
||||
|
Reference in New Issue
Block a user