diff --git a/Makefile b/Makefile index db41f018..932c9cd1 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,13 @@ LIB_DIR := $(ROOT)/lib/ TST_DIR := $(ROOT)/t/ +# project headers +MD5_HEADER := $(INC_DIR)/ft_md5.h +SHA_HEADER := $(INC_DIR)/ft_sha.h +SSL_HEADER := $(INC_DIR)/ft_ssl.h +HEADERS := $(MD5_HEADER) \ + $(SHA_HEADER) \ + $(SSL_HEADER) # libraries LIBFT_DIR := $(LIB_DIR)libft/ @@ -39,7 +46,12 @@ MUINUT_INC := -I $(MUINUT_DIR) # project source files -MD5_SRC = ft_md5_init.c \ +MD5_SRC = ft_md5.c \ + ft_md5_stdin.c \ + ft_md5_string.c \ + ft_md5_file.c \ + ft_md5_print.c \ + ft_md5_init.c \ ft_md5_update.c \ ft_md5_transform.c \ ft_md5_decode.c \ @@ -69,11 +81,6 @@ BASE64_SRC = ft_base64_init.c \ ft_base64_encode.c SRC = main.c \ - ft_ssl_init.c \ - ft_ssl_md5_file.c \ - ft_ssl_md5_stdin.c \ - ft_ssl_md5_string.c \ - ft_ssl_md5_print.c \ ft_ssl_sha256_file.c \ ft_ssl_sha256_stdin.c \ ft_ssl_sha256_string.c \ @@ -165,7 +172,7 @@ $(TEST_OBJ) $(OBJ): | $(OBJ_DIR) $(OBJ_DIR): mkdir $(OBJ_DIR) -$(OBJ_DIR)%.o: %.c +$(OBJ_DIR)%.o: %.c $(HEADERS) $(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS) $(LIBFT): diff --git a/inc/ft_md5.h b/inc/ft_md5.h index 6d5bf038..de36b642 100644 --- a/inc/ft_md5.h +++ b/inc/ft_md5.h @@ -15,6 +15,7 @@ # include +# define FT_MD5_READ_BLOCK_SIZE 1024 # define FT_MD5_BLOCK_SIZE 64 # define FT_MD5_WORDS_COUNT 16 # define FT_MD5_MESSAGE_LENGTH_BYTE 8 @@ -91,26 +92,93 @@ typedef struct s_md5_intr_registers t_byte4 d; } t_md5_intr_registers; -void ft_md5_init(t_md5_ctx *ctx); -void ft_md5_update(t_md5_ctx *ctx, t_byte1 *chunk, - t_byte8 len); -void ft_md5_transform(t_md5_ctx *ctx, - t_byte1 blck[FT_MD5_BLOCK_SIZE]); -void ft_md5_final(t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], - t_md5_ctx *ctx); -void ft_md5_decode(t_byte4 w[FT_MD5_WORDS_COUNT], - t_byte1 b[FT_MD5_BLOCK_SIZE]); -void ft_md5_encode_len( - t_byte1 bits[FT_MD5_MESSAGE_LENGTH_BYTE], - t_byte8 bit_len); -void ft_md5_encode_register(t_byte1 *digest_part, - t_byte4 reg); -void ft_md5_padding(t_byte1 padding[FT_MD5_BLOCK_SIZE]); -void ft_md5_digest_string( - t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], - t_byte1 string[FT_MD5_STRING_SIZE_BYTE]); -void ft_md5_string(t_byte1 *str); -void ft_md5_file(t_byte1 *filename); -void ft_md5_filter(void); +typedef struct s_md5_flags +{ + int quiet; + int reverse; + int print_stdin; + int something_printed; +} t_md5_flags; + +void ft_md5 +( + int argc, + char **argv +); + +void ft_md5_init +( + t_md5_ctx *ctx +); + +void ft_md5_update +( + t_md5_ctx *ctx, t_byte1 *chunk, + t_byte8 len +); + +void ft_md5_transform +( + t_md5_ctx *ctx, + t_byte1 blck[FT_MD5_BLOCK_SIZE] +); +void ft_md5_final +( + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], + t_md5_ctx *ctx +); + +void ft_md5_decode +( + t_byte4 w[FT_MD5_WORDS_COUNT], + t_byte1 b[FT_MD5_BLOCK_SIZE] +); + +void ft_md5_encode_len +( + t_byte1 bits[FT_MD5_MESSAGE_LENGTH_BYTE], + t_byte8 bit_len +); + +void ft_md5_encode_register +( + t_byte1 *digest_part, + t_byte4 reg +); + +void ft_md5_padding +( + t_byte1 padding[FT_MD5_BLOCK_SIZE] +); + +void ft_md5_digest_string +( + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], + t_byte1 string[FT_MD5_STRING_SIZE_BYTE] +); + +void ft_md5_string +( + const char *message, + t_md5_flags *flags +); + +void ft_md5_file +( + const char *filename, + t_md5_flags *flags +); + +void ft_md5_stdin +( + t_md5_flags *flags +); + +void ft_md5_print +( + t_byte1 *message, + t_byte1 *digest, + t_md5_flags *flags +); #endif diff --git a/inc/ft_ssl.h b/inc/ft_ssl.h index cca128af..6cead32d 100644 --- a/inc/ft_ssl.h +++ b/inc/ft_ssl.h @@ -18,13 +18,6 @@ # define FT_SSL_ALGS_COUNT 3 # define FT_SSL_BUFFER_SIZE 1024 -typedef enum e_alg_type -{ - DIGEST, - CIPHER, - END -} t_alg_type; - typedef struct s_ft_ssl t_ft_ssl; typedef void (*t_process_stdin)(t_ft_ssl *ft_ssl); @@ -33,6 +26,8 @@ typedef void (*t_process_string)(const char *string, typedef void (*t_process_file)(const char *filename, t_ft_ssl *ft_ssl); +typedef void (*t_alg_function)(int argc, char **argv); + typedef struct s_flags { int quiet; @@ -44,10 +39,7 @@ typedef struct s_flags typedef struct s_alorithm { const char *name; - t_process_stdin process_stdin; - t_process_file process_file; - t_process_string process_string; - t_alg_type type; + t_alg_function function; } t_algorithm; struct s_ft_ssl @@ -56,11 +48,10 @@ struct s_ft_ssl t_process_stdin process_stdin; t_process_file process_file; t_process_string process_string; - t_alg_type type; }; void ft_ssl_usage(void); -void ft_ssl_init(char *alg, t_ft_ssl *ft_ssl); +void ft_ssl_init(int argc, char **argv); void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl); void ft_ssl_md5_string(const char *strng, diff --git a/src/ft_ssl_init.c b/src/ft_ssl_init.c deleted file mode 100644 index 5ebc3cde..00000000 --- a/src/ft_ssl_init.c +++ /dev/null @@ -1,80 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_ssl_init.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gtertysh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/23 20:21:21 by gtertysh #+# #+# */ -/* Updated: 2018/10/23 20:28:00 by gtertysh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_ssl.h" -#include "libft.h" - -t_algorithm g_algorithms[] = { - { - "md5", - ft_ssl_md5_stdin, - ft_ssl_md5_file, - ft_ssl_md5_string, - DIGEST - }, - { - "sha256", - ft_ssl_sha256_stdin, - ft_ssl_sha256_file, - ft_ssl_sha256_string, - DIGEST - }, - { - "sha224", - ft_ssl_sha224_stdin, - ft_ssl_sha224_file, - ft_ssl_sha224_string, - DIGEST - }, - {NULL, NULL, NULL, NULL, END} -}; - -static void init_flags(t_ft_ssl *ft_ssl) -{ - ft_ssl->flags.print_stdin = 0; - ft_ssl->flags.quiet = 0; - ft_ssl->flags.reverse = 0; - ft_ssl->flags.something_printed = 0; -} - -static void set_algorithm(t_algorithm *alg, t_ft_ssl *ft_ssl) -{ - ft_ssl->process_file = alg->process_file; - ft_ssl->process_string = alg->process_string; - ft_ssl->process_stdin = alg->process_stdin; -} - -void ft_ssl_init(char *alg_name, t_ft_ssl *ft_ssl) -{ - int i; - t_algorithm *alg; - t_algorithm *alg_walker; - - i = 0; - alg = 0; - init_flags(ft_ssl); - alg_walker = g_algorithms; - while (alg_walker) - { - if (alg_walker[i].type == END) - break ; - if (ft_strcmp(alg_name, alg_walker[i].name) == 0) - { - alg = &alg_walker[i]; - break ; - } - i++; - } - if (!alg) - ft_ssl_usage(); - set_algorithm(alg, ft_ssl); -} diff --git a/src/ft_ssl_usage.c b/src/ft_ssl_usage.c index 37141119..69c8624d 100644 --- a/src/ft_ssl_usage.c +++ b/src/ft_ssl_usage.c @@ -15,13 +15,12 @@ void ft_ssl_usage(void) { + ft_putstr("\e[1;1H\e[2J"); ft_putstr("Message Digest commands:\n"); ft_putstr("md5\nsha256\nsha224\n\n"); - ft_putstr("Digest command usage:\n"); - ft_putstr("ft_ssl digest [-p|-q|-r] [[-s string...] [file...]]\n\n"); ft_putstr("Cipher commands:\n"); - ft_putstr("base64\n"); - ft_putstr("Cipher command usage:\n"); - ft_putstr("ft_ssl cipher [-p|-q|-r] [[-s string...] [file...]]\n\n"); + ft_putstr("base64\n\n"); + ft_putstr("To get help for specific command:\n"); + ft_putstr("ft_ssl command -h\n\n"); exit(1); } diff --git a/src/main.c b/src/main.c index 7fb89a87..01cd7ecd 100644 --- a/src/main.c +++ b/src/main.c @@ -11,68 +11,32 @@ /* ************************************************************************** */ #include "ft_ssl.h" +#include "ft_md5.h" #include "libft.h" -static int find_flag(const char *expected, char *real) -{ - return (ft_strcmp(real, expected) == 0); -} - -static int process_flags(int argc, char **argv, t_ft_ssl *ft_ssl) -{ - int i; - - i = 2; - while (i < argc) +t_algorithm g_algorithms[] = { { - if (find_flag("-r", argv[i])) - ft_ssl->flags.reverse = 1; - else if (find_flag("-q", argv[i])) - ft_ssl->flags.quiet = 1; - else if (find_flag("-p", argv[i])) - ft_ssl->flags.print_stdin = 1; - else - break ; - i++; - } - return (i); -} - -static void process_strings_and_files -( - int i, - int argc, - char **argv, - t_ft_ssl *ft_ssl -) -{ - while (i < argc) - { - if (find_flag("-s", argv[i])) - { - if (i + 1 >= argc) - ft_ssl_usage(); - ft_ssl->process_string(argv[++i], ft_ssl); - i++; - } - else - ft_ssl->process_file(argv[i++], ft_ssl); - } -} + "md5", + ft_md5, + }, + {NULL, NULL} +}; int main(int argc, char **argv) { - t_ft_ssl ft_ssl; - int i; + char *alg_name; + t_algorithm *alg_walker; + alg_walker = g_algorithms; + alg_name = argv[1]; if (argc < 2) ft_ssl_usage(); - ft_ssl_init(argv[1], &ft_ssl); - i = process_flags(argc, argv, &ft_ssl); - if (ft_ssl.flags.print_stdin) - ft_ssl.process_stdin(&ft_ssl); - process_strings_and_files(i, argc, argv, &ft_ssl); - if (!ft_ssl.flags.something_printed) - ft_ssl.process_stdin(&ft_ssl); + while (alg_walker->function) + { + if (ft_strcmp(alg_walker->name, alg_name) == 0) + alg_walker->function(argc, argv); + alg_walker++; + } + ft_ssl_usage(); return (0); } diff --git a/src/md5/ft_md5.c b/src/md5/ft_md5.c new file mode 100644 index 00000000..5c527180 --- /dev/null +++ b/src/md5/ft_md5.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/02 22:21:59 by gtertysh #+# #+# */ +/* Updated: 2019/02/02 22:26:35 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_md5.h" +#include "libft.h" + +static void ft_md5_usage(void) +{ + ft_putstr("MD5 Digest usage:\n"); + ft_putstr("ft_ssl md5 [-p|-q|-r] [[-s string...] [file...]]\n\n"); + exit(1); +} + +/* +** argc and argv are same as in main(); +*/ + +static void init_flags(t_md5_flags *flags) +{ + flags->quiet = 0; + flags->print_stdin = 0; + flags->reverse = 0; + flags->something_printed = 0; +} + +static int read_flags(int argc, char **argv, t_md5_flags *flags) +{ + int i; + + i = 2; + while (i < argc) + { + if (ft_strcmp("-r", argv[i]) == 0) + flags->reverse = 1; + else if (ft_strcmp("-q", argv[i]) == 0) + flags->quiet = 1; + else if (ft_strcmp("-p", argv[i]) == 0) + flags->print_stdin = 1; + else + break ; + i++; + } + return (i); +} + +static void process_strings_and_files +( + int i, + int argc, + char **argv, + t_md5_flags *flags +) +{ + while (i < argc) + { + if (ft_strcmp("-s", argv[i]) == 0) + { + if (i + 1 >= argc) + ft_md5_usage(); + ft_md5_string(argv[++i], flags); + i++; + } + else + ft_md5_file(argv[i++], flags); + } +} + +void ft_md5(int argc, char **argv) +{ + int flags_readed; + t_md5_flags flags; + + init_flags(&flags); + flags_readed = read_flags(argc, argv, &flags); + if (flags.print_stdin) + ft_md5_stdin(&flags); + process_strings_and_files(flags_readed, argc, argv, &flags); + if (!flags.something_printed) + ft_md5_stdin(&flags); + exit(0); +} diff --git a/src/ft_ssl_md5_file.c b/src/md5/ft_md5_file.c similarity index 84% rename from src/ft_ssl_md5_file.c rename to src/md5/ft_md5_file.c index effca1d1..657ea308 100644 --- a/src/ft_ssl_md5_file.c +++ b/src/md5/ft_md5_file.c @@ -15,25 +15,23 @@ #include "stdio.h" #include "unistd.h" #include "libft.h" -#include "ft_ssl.h" -void ft_ssl_md5_file(const char *filename, t_ft_ssl *ft_ssl) +void ft_md5_file(const char *filename, t_md5_flags *flags) { int fd; int len; t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; t_md5_ctx ctx; - t_byte1 buf[FT_SSL_BUFFER_SIZE]; + t_byte1 buf[FT_MD5_READ_BLOCK_SIZE]; - (void)ft_ssl; if ((fd = open(filename, O_RDONLY)) == -1) { perror("ft_ssl"); exit(1); } ft_md5_init(&ctx); - while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0) + while ((len = read(fd, buf, FT_MD5_READ_BLOCK_SIZE)) > 0) ft_md5_update(&ctx, buf, len); ft_md5_final(digest, &ctx); - ft_ssl_md5_print(filename, digest, ft_ssl); + ft_md5_print((t_byte1 *)filename, digest, flags); } diff --git a/src/ft_ssl_md5_print.c b/src/md5/ft_md5_print.c similarity index 76% rename from src/ft_ssl_md5_print.c rename to src/md5/ft_md5_print.c index 94ad183d..f26bb7e7 100644 --- a/src/ft_ssl_md5_print.c +++ b/src/md5/ft_md5_print.c @@ -11,46 +11,45 @@ /* ************************************************************************** */ #include "ft_md5.h" -#include "ft_ssl.h" #include "libft.h" -static void print_name(const char *name, int reverse) +static void print_name(t_byte1 *name, int reverse) { if (reverse) { ft_putstr(" "); - ft_putstr(name); + ft_putstr((const char *)name); } else { ft_putstr("MD5 ("); - ft_putstr(name); + ft_putstr((const char *)name); ft_putstr(") = "); } } -void ft_ssl_md5_print +void ft_md5_print ( - const char *target, - unsigned char *digest, - t_ft_ssl *ft_ssl + t_byte1 *message, + t_byte1 *digest, + t_md5_flags *flags ) { t_byte1 digest_string[FT_MD5_STRING_SIZE_BYTE]; ft_md5_digest_string(digest, digest_string); - if (target && !ft_ssl->flags.quiet && ft_ssl->flags.reverse) + if (message && !flags->quiet && flags->reverse) { ft_putstr((const char *)digest_string); - print_name(target, 1); + print_name(message, 1); } - else if (target && !ft_ssl->flags.quiet) + else if (message && !flags->quiet) { - print_name(target, 0); + print_name(message, 0); ft_putstr((const char *)digest_string); } else ft_putstr((const char *)digest_string); ft_putstr("\n"); - ft_ssl->flags.something_printed = 1; + flags->something_printed = 1; } diff --git a/src/ft_ssl_md5_stdin.c b/src/md5/ft_md5_stdin.c similarity index 79% rename from src/ft_ssl_md5_stdin.c rename to src/md5/ft_md5_stdin.c index 1d821497..838ed2e6 100644 --- a/src/ft_ssl_md5_stdin.c +++ b/src/md5/ft_md5_stdin.c @@ -10,27 +10,26 @@ /* */ /* ************************************************************************** */ -#include "ft_ssl.h" +#include +#include +#include +#include #include "ft_md5.h" -#include "fcntl.h" -#include "stdio.h" -#include "unistd.h" -void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl) +void ft_md5_stdin(t_md5_flags *flags) { int len; t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; t_md5_ctx ctx; - t_byte1 buf[FT_SSL_BUFFER_SIZE]; + t_byte1 buf[FT_MD5_READ_BLOCK_SIZE]; - (void)ft_ssl; ft_md5_init(&ctx); - while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) + while ((len = read(0, buf, FT_MD5_READ_BLOCK_SIZE))) { - if (ft_ssl->flags.print_stdin) + if (flags->print_stdin) write(1, buf, len); ft_md5_update(&ctx, buf, len); } ft_md5_final(digest, &ctx); - ft_ssl_md5_print(NULL, digest, ft_ssl); + ft_md5_print(NULL, digest, flags); } diff --git a/src/ft_ssl_md5_string.c b/src/md5/ft_md5_string.c similarity index 90% rename from src/ft_ssl_md5_string.c rename to src/md5/ft_md5_string.c index 792422cf..5436a109 100644 --- a/src/ft_ssl_md5_string.c +++ b/src/md5/ft_md5_string.c @@ -10,14 +10,13 @@ /* */ /* ************************************************************************** */ -#include "ft_ssl.h" #include "ft_md5.h" #include "fcntl.h" #include "stdio.h" #include "unistd.h" #include "libft.h" -void ft_ssl_md5_string(const char *str, t_ft_ssl *ft_ssl) +void ft_md5_string(const char *str, t_md5_flags *flags) { t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; t_md5_ctx ctx; @@ -25,5 +24,5 @@ void ft_ssl_md5_string(const char *str, t_ft_ssl *ft_ssl) ft_md5_init(&ctx); ft_md5_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); ft_md5_final(digest, &ctx); - ft_ssl_md5_print(str, digest, ft_ssl); + ft_md5_print((t_byte1 *)str, digest, flags); }