Change the way the hamming function handles its arguments length

This commit is contained in:
Dimitri Lozeve 2020-06-10 15:25:21 +02:00
parent 2a122cb5a6
commit 87c2fdc95d
4 changed files with 7 additions and 23 deletions

View file

@ -19,7 +19,7 @@ double normalized_edit_distance(unsigned int keysize,
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(blocks[i], blocks[i + 1]) / (double)keysize; avg_dist += hamming(keysize, blocks[i], blocks[i + 1]) / (double)keysize;
} }
return avg_dist / ((double)blocks_count - 1.0); return avg_dist / ((double)blocks_count - 1.0);
@ -60,7 +60,7 @@ int main(int argc, char *argv[]) {
unsigned int keysize = 40; unsigned int keysize = 40;
double min_edit_dist = INFINITY; double min_edit_dist = INFINITY;
for (unsigned int i = 30; i > 1; --i) { for (unsigned int i = 40; i > 1; --i) {
double edit_dist = normalized_edit_distance(i, buf, len / i); double edit_dist = normalized_edit_distance(i, buf, len / i);
if (edit_dist < min_edit_dist) { if (edit_dist < min_edit_dist) {
min_edit_dist = edit_dist; min_edit_dist = edit_dist;
@ -86,7 +86,7 @@ int main(int argc, char *argv[]) {
key[j] = best_single_char_xor_key(blocks_count, blocks[j]); key[j] = best_single_char_xor_key(blocks_count, blocks[j]);
printf("%c\n", key[j]); printf("%c\n", key[j]);
} }
printf("key: %s\n", key); //printf("key: %s\n", key);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View file

@ -4,19 +4,6 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#define min(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _b : _a; \
})
#define max(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
unsigned char hex_to_byte(const char c) { unsigned char hex_to_byte(const char c) {
if (isdigit(c)) { if (isdigit(c)) {
@ -121,12 +108,9 @@ double frequency_score(size_t len, const char buf[static len]) {
return chi2; return chi2;
} }
unsigned int hamming(const char s1[static 1], const char s2[static 1]) { unsigned int hamming(size_t len, const char s1[static len], const char s2[static len]) {
unsigned int len1 = strlen(s1);
unsigned int len2 = strlen(s2);
unsigned int res = 0; unsigned int res = 0;
for (size_t i = 0; i < min(len1, len2); ++i) { for (size_t i = 0; i < len; ++i) {
unsigned int n = s1[i] ^ s2[i]; unsigned int n = s1[i] ^ s2[i];
while (n) { while (n) {
res += n & 1; res += n & 1;

View file

@ -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(const char s1[static 1], const char s2[static 1]); unsigned int hamming(size_t len, const char s1[static len], const 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 */

View file

@ -83,7 +83,7 @@ static MunitResult test_hamming(const MunitParameter params[], void *data) {
(void)params; (void)params;
(void)data; (void)data;
assert_int(hamming("this is a test", "wokka wokka!!!"), ==, 37); assert_int(hamming(14, "this is a test", "wokka wokka!!!"), ==, 37);
return MUNIT_OK; return MUNIT_OK;
} }