Solve challenge 6
This commit is contained in:
parent
87a1547478
commit
cb30d47479
4 changed files with 14 additions and 19 deletions
|
@ -6,23 +6,15 @@
|
||||||
|
|
||||||
#define BUF_SIZE 4096
|
#define BUF_SIZE 4096
|
||||||
|
|
||||||
double normalized_edit_distance(unsigned int keysize,
|
double normalized_edit_distance(
|
||||||
const unsigned char buf[static keysize * 4],
|
unsigned int keysize, unsigned int blocks_count,
|
||||||
unsigned int blocks_count) {
|
const unsigned char buf[static keysize * blocks_count]) {
|
||||||
char blocks[blocks_count][keysize];
|
|
||||||
|
|
||||||
for (size_t i = 0; i < blocks_count; ++i) {
|
|
||||||
for (size_t j = 0; j < keysize; ++j) {
|
|
||||||
blocks[i][j] = buf[i * keysize + j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double avg_dist = 0;
|
double avg_dist = 0;
|
||||||
for (size_t i = 0; i < blocks_count - 1; ++i) {
|
for (size_t i = 0; i < blocks_count - 1; ++i) {
|
||||||
avg_dist += hamming(keysize, blocks[i], blocks[i + 1]) / (double)keysize;
|
avg_dist += hamming(keysize, &buf[i * keysize], &buf[(i + 1) * keysize]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return avg_dist / ((double)blocks_count - 1.0);
|
return avg_dist / (double)keysize / ((double)blocks_count - 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
@ -66,10 +58,10 @@ int main(int argc, char *argv[]) {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int keysize = 40;
|
unsigned int keysize = 0;
|
||||||
double min_edit_dist = INFINITY;
|
double min_edit_dist = INFINITY;
|
||||||
for (unsigned int i = 40; i > 1; --i) {
|
for (unsigned int i = 2; i < 40; ++i) {
|
||||||
double edit_dist = normalized_edit_distance(i, buf, len / i);
|
double edit_dist = normalized_edit_distance(i, len / i, buf);
|
||||||
if (edit_dist < min_edit_dist) {
|
if (edit_dist < min_edit_dist) {
|
||||||
min_edit_dist = edit_dist;
|
min_edit_dist = edit_dist;
|
||||||
keysize = i;
|
keysize = i;
|
||||||
|
|
|
@ -115,7 +115,8 @@ double frequency_score(size_t len, const char buf[static len]) {
|
||||||
return chi2;
|
return chi2;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int hamming(size_t len, const char s1[static len], const char s2[static len]) {
|
unsigned int hamming(size_t len, const unsigned char s1[static len],
|
||||||
|
const unsigned char s2[static len]) {
|
||||||
unsigned int res = 0;
|
unsigned int res = 0;
|
||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
unsigned int n = s1[i] ^ s2[i];
|
unsigned int n = s1[i] ^ s2[i];
|
||||||
|
|
|
@ -8,7 +8,7 @@ size_t hex_to_bytes(unsigned char out[static 1], const char hex[static 1]);
|
||||||
char *bytes_to_base64(char out[static 4], const unsigned char in[static 3], size_t len);
|
char *bytes_to_base64(char out[static 4], const unsigned char in[static 3], size_t len);
|
||||||
size_t base64_to_bytes(unsigned char out[static 3], const char in[static 4]);
|
size_t base64_to_bytes(unsigned char out[static 3], const char in[static 4]);
|
||||||
double frequency_score(size_t len, const char buf[static len]);
|
double frequency_score(size_t len, const char buf[static len]);
|
||||||
unsigned int hamming(size_t len, const char s1[static len], const char s2[static len]);
|
unsigned int hamming(size_t len, const unsigned char s1[static len], const unsigned char s2[static len]);
|
||||||
char best_single_char_xor_key(size_t len, unsigned char buf[static len]);
|
char best_single_char_xor_key(size_t len, unsigned char buf[static len]);
|
||||||
|
|
||||||
#endif /* UTILS_H */
|
#endif /* UTILS_H */
|
||||||
|
|
|
@ -83,7 +83,9 @@ static MunitResult test_hamming(const MunitParameter params[], void *data) {
|
||||||
(void)params;
|
(void)params;
|
||||||
(void)data;
|
(void)data;
|
||||||
|
|
||||||
assert_int(hamming(14, "this is a test", "wokka wokka!!!"), ==, 37);
|
assert_int(hamming(14, (const unsigned char *)"this is a test",
|
||||||
|
(const unsigned char *)"wokka wokka!!!"),
|
||||||
|
==, 37);
|
||||||
|
|
||||||
return MUNIT_OK;
|
return MUNIT_OK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue