From 5d211f451aaaefb7c611bc80d582d42f63774b99 Mon Sep 17 00:00:00 2001 From: None Date: Mon, 25 May 2015 14:25:55 +0200 Subject: [PATCH] Cleanup --- Makefile | 13 +- main.cpp | 8 + set1/runner.cpp | 135 ++++++++++++ set1/runner.h | 21 ++ set1/xor_char_finder.cpp | 28 +-- set1/xor_char_finder.h | 1 - utils/{hex_to_base64.cpp => base64.cpp} | 3 +- utils/{hex_to_base64.h => base64.h} | 7 +- utils/common.c | 30 +++ utils/common.h | 3 + utils/hamming.c | 2 +- utils/runner.cpp | 106 ++++++++++ utils/runner.h | 22 ++ utils/utils_tester.cpp | 270 ------------------------ 14 files changed, 342 insertions(+), 307 deletions(-) create mode 100644 main.cpp create mode 100644 set1/runner.cpp create mode 100644 set1/runner.h rename utils/{hex_to_base64.cpp => base64.cpp} (99%) rename utils/{hex_to_base64.h => base64.h} (86%) create mode 100644 utils/common.c create mode 100644 utils/common.h create mode 100644 utils/runner.cpp create mode 100644 utils/runner.h delete mode 100644 utils/utils_tester.cpp diff --git a/Makefile b/Makefile index cb084a1..1a65296 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,16 @@ INC=. all: mkdir -p obj - g++ -g -I${INC} -c utils/hex_to_base64.cpp -o obj/hex_to_base64.o - g++ -g -I${INC} -c utils/utils_tester.cpp -o obj/utils_tester.o + g++ -g -I${INC} -c utils/common.c -o obj/common.o + g++ -g -I${INC} -c utils/base64.cpp -o obj/base64.o + g++ -g -I${INC} -c utils/runner.cpp -o obj/utils_runner.o g++ -g -I${INC} -c utils/xor.c -o obj/xor.o g++ -g -I${INC} -c set1/xor_char_finder.cpp -o obj/xor_char_finder.o g++ -g -I${INC} -c utils/hamming.c -o obj/hamming.o - g++ -g -lpthread -o utils_tester obj/hex_to_base64.o obj/utils_tester.o obj/xor_char_finder.o obj/xor.o obj/hamming.o + g++ -g -I${INC} -c set1/runner.cpp -o obj/set1_runner.o + g++ -g -I${INC} -c main.cpp -o obj/main.o + g++ -g -lpthread -o main obj/common.o obj/base64.o obj/xor_char_finder.o obj/xor.o \ + obj/hamming.o obj/utils_runner.o \ + obj/set1_runner.o obj/main.o clean: - rm -rf obj utils_tester + rm -rf obj diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..3f2ac87 --- /dev/null +++ b/main.cpp @@ -0,0 +1,8 @@ +#include "set1/runner.h" +#include "utils/runner.h" + +int main() +{ + UTILS::run(); + SET1::run(); +} \ No newline at end of file diff --git a/set1/runner.cpp b/set1/runner.cpp new file mode 100644 index 0000000..83dba41 --- /dev/null +++ b/set1/runner.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + +#include "set1/xor_char_finder.h" +#include "utils/common.h" +#include "utils/base64.h" +#include "utils/xor.h" +#include "utils/hamming.h" + +void set1_challenge3_test() +{ + static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; + struct frequency_t max_score; + + // ----------------------------------------------------------------------------- + // + // 1. Convert ASCII ciphertext to HEX + // + // ----------------------------------------------------------------------------- + const int ciphertext_len = strlen(ciphertext) / 2; + unsigned char ciphertext_xor[ciphertext_len]; + unsigned char* p_ciphertext_xor = ciphertext_xor; + convert_string_to_hex(ciphertext, ciphertext_len*2, p_ciphertext_xor); + + xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); + assert(max_score.letter == 'X'); +} + +void set1_challange4_test() +{ + FILE* fp; + char* line=NULL; + size_t len=0; + int line_nb = 0; + unsigned char hexdump[30]; + unsigned char* p_hexdump = hexdump; + + fp=fopen("etc/set1_t4_input.txt", "r"); + if(fp==NULL) + { + fprintf(stderr, "etc/set1_t4_input.txt not found"); + return; + } + + while( getline(&line, &len, fp) != -1 ) + { + ++line_nb; + // printf("%s",line); + struct frequency_t max_score; + max_score.letter=0x0; + max_score.score = 0; + + + // ----------------------------------------------------------------------------- + // + // 1. Convert ASCII ciphertext to HEX + // + // ----------------------------------------------------------------------------- + const int ciphertext_len = strlen(line) / 2; + unsigned char ciphertext_xor[ciphertext_len]; + unsigned char* p_ciphertext_xor = ciphertext_xor; + convert_string_to_hex(line, ciphertext_len*2, p_ciphertext_xor); + + xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); + if(max_score.letter != 0x00) + { + // printf("%d -> %c\n", line_nb, max_score.letter); + + convert_string_to_hex(line, 60, p_hexdump); + for(int i=0; i<30;++i) + { + // printf("%c", hexdump[i]^max_score.letter); + } + // printf("\n"); + } + free(line); line=0; len=0; + + } + free(line); +} + +void set1_challenge_5_test() +{ + static const char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; + static const char xor_val[] = "ICE"; + static const unsigned char expected[] = { + 0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69, + 0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63, + 0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65, + 0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28, + 0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f}; + + unsigned char o_ch1[256]; + memset(o_ch1, '\0', 256); + unsigned char* po_ch1 = o_ch1; + + xor_repeatedly(xor_val, ch1, po_ch1); + + for(unsigned i=0; i %c %x\n", max_score.letter, max_score.letter); + } +} diff --git a/set1/runner.h b/set1/runner.h new file mode 100644 index 0000000..8e3d858 --- /dev/null +++ b/set1/runner.h @@ -0,0 +1,21 @@ +#ifndef __set1_runner__ +#define __set1_runner__ + +void set1_challenge3_test(); +void set1_challange4_test(); +void set1_challenge_5_test(); +void set1_challenge_6_test(); + +namespace SET1 { + + void run() + { + set1_challenge3_test(); + set1_challange4_test(); + set1_challenge_5_test(); + set1_challenge_6_test(); + } + +} + +#endif// __set1_runner__ diff --git a/set1/xor_char_finder.cpp b/set1/xor_char_finder.cpp index bc8d701..056a442 100644 --- a/set1/xor_char_finder.cpp +++ b/set1/xor_char_finder.cpp @@ -1,28 +1,8 @@ #include #include -#include "utils/hex_to_base64.h" +#include "utils/base64.h" #include "set1/xor_char_finder.h" -int char_in_array(const unsigned char iX, const char* const iArray, const int iSize ) -{ - int found = 0; - for(int i=0; i #include #include -#include "utils/hex_to_base64.h" +#include "utils/base64.h" +#include "utils/common.h" #define MIN(a,b) a +#include +#include + +unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr) +{ + FILE* fh=0; + size_t len=0; + char* line=NULL; + unsigned long total_len=0; + + if( (fh=fopen(filepath, "rb")) == 0 ) + { + fprintf(stderr, "File %s doesn't exist. Exiting...\n", filepath); + exit(1); + } + + while( getline(&line, &len, fh) != -1 ) + { + len = strlen(line); + if(remove_new_line_chr) + len--; + + strncpy(obuff+total_len, line, len); + total_len+=len; + free(line); + line=NULL; + } + return total_len; +} diff --git a/utils/common.h b/utils/common.h new file mode 100644 index 0000000..ff3e806 --- /dev/null +++ b/utils/common.h @@ -0,0 +1,3 @@ +struct int24 { unsigned int data:24; }; + +unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1); diff --git a/utils/hamming.c b/utils/hamming.c index 07f1ff4..9ea7a75 100644 --- a/utils/hamming.c +++ b/utils/hamming.c @@ -1,6 +1,6 @@ #include #include -#include "utils/hex_to_base64.h" +#include "utils/base64.h" static unsigned calculate_nb_of_bits_set(unsigned char i_char) { diff --git a/utils/runner.cpp b/utils/runner.cpp new file mode 100644 index 0000000..1347531 --- /dev/null +++ b/utils/runner.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +#include "utils/base64.h" +#include "utils/hamming.h" +#include "utils/xor.h" + +#include "set1/xor_char_finder.h" + +void hex_to_base64_test() { + const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; + const char expected_buff1[] ="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; + + const char test_buff2[] ="4927"; + const char expected_buff2[] ="SSH"; + + const char test_buff3[] ="49"; + const char expected_buff3[] ="SB"; + + const char test_buff4[] ="33"; + const char expected_buff4[] ="z"; + + char output[256]; + char* po=output; + + memset (output,'\0', 256); + hex_to_base64(test_buff1, po, strlen(test_buff1) ); + assert( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 ); + assert( strlen(expected_buff1) == strlen(output)); + + // test case when there are 2 hex'es + memset (output,'\0', 256); + hex_to_base64(test_buff2, po, strlen(test_buff2) ); + assert( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 ); + assert( strlen(expected_buff2) == strlen(output)); + + // test case when there is 1 hex + memset (output,'\0', 256); + hex_to_base64(test_buff3, po, strlen(test_buff3) ); + assert( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 ); + assert( strlen(expected_buff3) == strlen(output)); + + // test case when there is 1 char + memset (output,'\0', 256); + hex_to_base64(test_buff4, po, strlen(test_buff4) ); + assert( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 ); + assert( strlen(expected_buff4) == strlen(output)); +} + +void convert_string_to_hex_test() +{ + const char test_buff1[] = "49276d"; + unsigned char out_buff[3]; + unsigned char* p = out_buff; + convert_string_to_hex(test_buff1, strlen(test_buff1), p); + assert( p[0] == 0x49 + && p[1] == 0x27 + && p[2] == 0x6d); + + // and back + char out_buf[6]; + char* op = out_buf; + convert_hex_to_string(p, 3, op); + assert( memcmp(op, test_buff1, 6) == 0 ); + +} + +void xor_strings_test() +{ + const char i_buf_1[] = "1c0111001f010100061a024b53535009181c"; + const char* p_ibuf1=i_buf_1; + + const char i_buf_2[] = "686974207468652062756c6c277320657965"; + const char* p_ibuf2=i_buf_2; + + char out_buf[36]; + char* p_out_buf=out_buf; + + unsigned char buf[36/2]; + unsigned char* p_buf = buf; + + xor_strings(p_ibuf1, p_ibuf2, p_buf); + + convert_hex_to_string(buf, 36/2, p_out_buf); + assert( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0); +} + +void hamming_test() +{ + static const char ch1[]="this is a test"; + static const char ch2[]="wokka wokka!!!"; + assert(block_distance(ch1,ch2,strlen(ch1)) == 37); +} + +void base64_to_hex_test() +{ + static const char ch2[]="TWFuTWFu"; // 0x41 0x61 0x6E 0x41 0x61 0x6E + unsigned char out[6]; + unsigned char *p=out; + unsigned char expected[6] = {0x4D, 0x61, 0x6E, 0x4D, 0x61, 0x6E}; + base64_to_hex(ch2, 8, p); + assert( memcmp(expected, out, 6) == 0 ); +} + diff --git a/utils/runner.h b/utils/runner.h new file mode 100644 index 0000000..ac438ef --- /dev/null +++ b/utils/runner.h @@ -0,0 +1,22 @@ +#ifndef __utils_run__ +#define __utils_run__ + +void hex_to_base64_test(); +unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1); +void xor_strings_test(); +void hamming_test(); +void base64_to_hex_test(); + +namespace UTILS { + + void run() { + hex_to_base64_test(); + // convert_string_to_hex_test(); + xor_strings_test(); + hamming_test(); + base64_to_hex_test(); + } + +} + +#endif // __utils_run__ diff --git a/utils/utils_tester.cpp b/utils/utils_tester.cpp deleted file mode 100644 index 99cc092..0000000 --- a/utils/utils_tester.cpp +++ /dev/null @@ -1,270 +0,0 @@ -#include "hex_to_base64.h" -#include "set1/xor_char_finder.h" -#include "hamming.h" -#include -#include -#include -#include -#include "xor.h" - -void hex_to_base64_text() { - const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; - const char expected_buff1[] ="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; - - const char test_buff2[] ="4927"; - const char expected_buff2[] ="SSH"; - - const char test_buff3[] ="49"; - const char expected_buff3[] ="SB"; - - const char test_buff4[] ="33"; - const char expected_buff4[] ="z"; - - char output[256]; - char* po=output; - - memset (output,'\0', 256); - hex_to_base64(test_buff1, po, strlen(test_buff1) ); - assert( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 ); - assert( strlen(expected_buff1) == strlen(output)); - - // test case when there are 2 hex'es - memset (output,'\0', 256); - hex_to_base64(test_buff2, po, strlen(test_buff2) ); - assert( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 ); - assert( strlen(expected_buff2) == strlen(output)); - - // test case when there is 1 hex - memset (output,'\0', 256); - hex_to_base64(test_buff3, po, strlen(test_buff3) ); - assert( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 ); - assert( strlen(expected_buff3) == strlen(output)); - - // test case when there is 1 char - memset (output,'\0', 256); - hex_to_base64(test_buff4, po, strlen(test_buff4) ); - assert( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 ); - assert( strlen(expected_buff4) == strlen(output)); -} - -unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1) -{ - FILE* fh=0; - size_t len=0; - char* line=NULL; - unsigned long total_len=0; - - if( (fh=fopen(filepath, "rb")) == 0 ) - { - fprintf(stderr, "File %s doesn't exist. Exiting...\n", filepath); - exit(1); - } - - while( getline(&line, &len, fh) != -1 ) - { - len = strlen(line); - if(remove_new_line_chr) - len--; - - strncpy(obuff+total_len, line, len); - total_len+=len; - free(line); - line=NULL; - } - return total_len; -} - -void convert_string_to_hex_test() -{ - const char test_buff1[] = "49276d"; - unsigned char out_buff[3]; - unsigned char* p = out_buff; - convert_string_to_hex(test_buff1, strlen(test_buff1), p); - assert( p[0] == 0x49 - && p[1] == 0x27 - && p[2] == 0x6d); - - // and back - char out_buf[6]; - char* op = out_buf; - convert_hex_to_string(p, 3, op); - assert( memcmp(op, test_buff1, 6) == 0 ); - -} - -void xor_strings_test() -{ - const char i_buf_1[] = "1c0111001f010100061a024b53535009181c"; - const char* p_ibuf1=i_buf_1; - - const char i_buf_2[] = "686974207468652062756c6c277320657965"; - const char* p_ibuf2=i_buf_2; - - char out_buf[36]; - char* p_out_buf=out_buf; - - unsigned char buf[36/2]; - unsigned char* p_buf = buf; - - xor_strings(p_ibuf1, p_ibuf2, p_buf); - - convert_hex_to_string(buf, 36/2, p_out_buf); - assert( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0); -} - -void set1_challenge3_test() -{ - static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; - struct frequency_t max_score; - - // ----------------------------------------------------------------------------- - // - // 1. Convert ASCII ciphertext to HEX - // - // ----------------------------------------------------------------------------- - const int ciphertext_len = strlen(ciphertext) / 2; - unsigned char ciphertext_xor[ciphertext_len]; - unsigned char* p_ciphertext_xor = ciphertext_xor; - convert_string_to_hex(ciphertext, ciphertext_len*2, p_ciphertext_xor); - - xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); - assert(max_score.letter == 'X'); -} - -void set2_challange4_test() -{ - FILE* fp; - char* line=NULL; - size_t len=0; - int line_nb = 0; - unsigned char hexdump[30]; - unsigned char* p_hexdump = hexdump; - - fp=fopen("etc/set1_t4_input.txt", "r"); - if(fp==NULL) - { - fprintf(stderr, "etc/set1_t4_input.txt not found"); - return; - } - - while( getline(&line, &len, fp) != -1 ) - { - ++line_nb; - // printf("%s",line); - struct frequency_t max_score; - max_score.letter=0x0; - max_score.score = 0; - - - // ----------------------------------------------------------------------------- - // - // 1. Convert ASCII ciphertext to HEX - // - // ----------------------------------------------------------------------------- - const int ciphertext_len = strlen(line) / 2; - unsigned char ciphertext_xor[ciphertext_len]; - unsigned char* p_ciphertext_xor = ciphertext_xor; - convert_string_to_hex(line, ciphertext_len*2, p_ciphertext_xor); - - xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); - if(max_score.letter != 0x00) - { - // printf("%d -> %c\n", line_nb, max_score.letter); - - convert_string_to_hex(line, 60, p_hexdump); - for(int i=0; i<30;++i) - { - // printf("%c", hexdump[i]^max_score.letter); - } - // printf("\n"); - } - free(line); line=0; len=0; - - } - free(line); -} - -void set2_challenge_5_test() -{ - static const char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; - static const char xor_val[] = "ICE"; - static const unsigned char expected[] = { - 0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69, - 0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63, - 0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65, - 0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28, - 0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f}; - - unsigned char o_ch1[256]; - memset(o_ch1, '\0', 256); - unsigned char* po_ch1 = o_ch1; - - xor_repeatedly(xor_val, ch1, po_ch1); - - for(unsigned i=0; i %c %x\n", max_score.letter, max_score.letter); - } - // delete [] slide; - -} - -int main() { - hex_to_base64_text(); - convert_string_to_hex_test(); - xor_strings_test(); - //set1_challenge3_test(); - set2_challange4_test(); - set2_challenge_5_test(); - hamming_test(); - base64_to_hex_test(); - set2_challenge_6_test(); -} -