From 50f726188d1d04e4a61a6ec32cd3d08194492654 Mon Sep 17 00:00:00 2001 From: Gregory Date: Mon, 22 Oct 2018 22:33:05 +0300 Subject: [PATCH] partially working main --- Makefile | 18 ++++++++-- inc/ft_ssl.h | 73 ++++++++++++++++++++++++++++++++++++++ src/ft_ssl_init.c | 58 ++++++++++++++++++++++++++++++ src/ft_ssl_md5_file.c | 27 ++++++++++++++ src/ft_ssl_md5_print.c | 13 +++++++ src/ft_ssl_md5_stdin.c | 24 +++++++++++++ src/ft_ssl_md5_string.c | 21 +++++++++++ src/ft_ssl_sha224_file.c | 30 ++++++++++++++++ src/ft_ssl_sha224_stdin.c | 24 +++++++++++++ src/ft_ssl_sha224_string.c | 21 +++++++++++ src/ft_ssl_sha256_file.c | 30 ++++++++++++++++ src/ft_ssl_sha256_stdin.c | 24 +++++++++++++ src/ft_ssl_sha256_string.c | 21 +++++++++++ src/ft_ssl_usage.c | 8 +++++ src/main.c | 49 +++++++++++++++++++++++-- src/md5/ft_md5_update.c | 2 +- src/sha/ft_sha256_update.c | 2 +- 17 files changed, 438 insertions(+), 7 deletions(-) create mode 100644 inc/ft_ssl.h create mode 100644 src/ft_ssl_init.c create mode 100644 src/ft_ssl_md5_file.c create mode 100644 src/ft_ssl_md5_print.c create mode 100644 src/ft_ssl_md5_stdin.c create mode 100644 src/ft_ssl_md5_string.c create mode 100644 src/ft_ssl_sha224_file.c create mode 100644 src/ft_ssl_sha224_stdin.c create mode 100644 src/ft_ssl_sha224_string.c create mode 100644 src/ft_ssl_sha256_file.c create mode 100644 src/ft_ssl_sha256_stdin.c create mode 100644 src/ft_ssl_sha256_string.c create mode 100644 src/ft_ssl_usage.c diff --git a/Makefile b/Makefile index 4e217d60..f6ed4760 100644 --- a/Makefile +++ b/Makefile @@ -64,8 +64,22 @@ SHA_SRC = ft_sha256_init.c \ ft_sha224_digest_string.c -SRC = main.c -SRC += $(MD5_SRC) +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 \ + ft_ssl_sha224_file.c \ + ft_ssl_sha224_stdin.c \ + ft_ssl_sha224_string.c \ + ft_ssl_usage.c + +SRC += $(MD5_SRC) \ + $(SHA_SRC) # project object files OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) diff --git a/inc/ft_ssl.h b/inc/ft_ssl.h new file mode 100644 index 00000000..e8c5186e --- /dev/null +++ b/inc/ft_ssl.h @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/22 20:07:36 by gtertysh #+# #+# */ +/* Updated: 2018/10/22 20:15:49 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_SSL_H +# define FT_SSL_H + +# define FT_SSL_ALGS_COUNT 3 +# define FT_SSL_BUFFER_SIZE 1024 + +typedef struct s_ft_ssl t_ft_ssl; + +typedef void (*t_process_stdin)(t_ft_ssl *ft_ssl); +typedef void (*t_process_string)(const char *string, + t_ft_ssl *ft_ssl); +typedef void (*t_process_file)(const char *filename, + t_ft_ssl *ft_ssl); + +typedef struct s_flags +{ + int quiet; + int reverse; + int print_stdin; +} t_flags; + +typedef struct s_alorithm +{ + const char *name; + t_process_stdin process_stdin; + t_process_file process_file; + t_process_string process_string; +} t_algorithm; + +struct s_ft_ssl +{ + t_flags flags; + t_process_stdin process_stdin; + t_process_file process_file; + t_process_string process_string; +}; + +void ft_ssl_usage(void); +void ft_ssl_init(char *alg, t_ft_ssl *ft_ssl); + +void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl); +void ft_ssl_md5_string(const char *strng, + t_ft_ssl *ft_ssl); +void ft_ssl_md5_file(const char *filename, + t_ft_ssl *ft_ssl); +void ft_ssl_md5_print(unsigned char *digest, + t_ft_ssl *ft_ssl); + +void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl); +void ft_ssl_sha256_string(const char *strng, + t_ft_ssl *ft_ssl); +void ft_ssl_sha256_file(const char *filename, + t_ft_ssl *ft_ssl); + +void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl); +void ft_ssl_sha224_string(const char *strng, + t_ft_ssl *ft_ssl); +void ft_ssl_sha224_file(const char *filename, + t_ft_ssl *ft_ssl); + +#endif diff --git a/src/ft_ssl_init.c b/src/ft_ssl_init.c new file mode 100644 index 00000000..4c304007 --- /dev/null +++ b/src/ft_ssl_init.c @@ -0,0 +1,58 @@ +#include "ft_ssl.h" +#include "libft.h" + +static t_algorithm *init_algorithms(void) +{ + static t_algorithm algs[FT_SSL_ALGS_COUNT] = { + { + "md5", + ft_ssl_md5_stdin, + ft_ssl_md5_file, + ft_ssl_md5_string, + }, + { + "sha256", + ft_ssl_sha256_stdin, + ft_ssl_sha256_file, + ft_ssl_sha256_string, + }, + { + "sha224", + ft_ssl_sha224_stdin, + ft_ssl_sha224_file, + ft_ssl_sha224_string, + } + }; + return algs; +} + +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; + + alg_walker = init_algorithms(); + while (i < FT_SSL_ALGS_COUNT) + { + 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); +} \ No newline at end of file diff --git a/src/ft_ssl_md5_file.c b/src/ft_ssl_md5_file.c new file mode 100644 index 00000000..ae89e7de --- /dev/null +++ b/src/ft_ssl_md5_file.c @@ -0,0 +1,27 @@ +#include "ft_ssl.h" +#include "ft_md5.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" +#include "libft.h" + +void ft_ssl_md5_file(const char *filename, t_ft_ssl *ft_ssl) +{ + int fd; + int len; + BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + t_md5_ctx ctx; + BYTE1 buf[FT_SSL_BUFFER_SIZE]; + + (void)ft_ssl; + if((fd = open(filename, O_RDONLY)) == -1) + { + perror("Error: "); + exit(1); + } + ft_md5_init(&ctx); + while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0) + ft_md5_update(&ctx, buf, len); + ft_md5_final(digest, &ctx); + ft_ssl_md5_print(digest, ft_ssl); +} \ No newline at end of file diff --git a/src/ft_ssl_md5_print.c b/src/ft_ssl_md5_print.c new file mode 100644 index 00000000..11266c09 --- /dev/null +++ b/src/ft_ssl_md5_print.c @@ -0,0 +1,13 @@ +#include "ft_md5.h" +#include "ft_ssl.h" +#include "libft.h" + +void ft_ssl_md5_print(unsigned char *digest, + t_ft_ssl *ft_ssl) +{ + BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE]; + (void)ft_ssl; + ft_md5_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_md5_stdin.c b/src/ft_ssl_md5_stdin.c new file mode 100644 index 00000000..dc8622a5 --- /dev/null +++ b/src/ft_ssl_md5_stdin.c @@ -0,0 +1,24 @@ +#include "ft_ssl.h" +#include "ft_md5.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" + +void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl) +{ + int len; + BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + t_md5_ctx ctx; + BYTE1 buf[FT_SSL_BUFFER_SIZE]; + + (void)ft_ssl; + ft_md5_init(&ctx); + while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) + { + if (ft_ssl->flags.print_stdin) + write(1, buf, len); + ft_md5_update(&ctx, buf, len); + } + ft_md5_final(digest, &ctx); + ft_ssl_md5_print(digest, ft_ssl); +} \ No newline at end of file diff --git a/src/ft_ssl_md5_string.c b/src/ft_ssl_md5_string.c new file mode 100644 index 00000000..e4102564 --- /dev/null +++ b/src/ft_ssl_md5_string.c @@ -0,0 +1,21 @@ +#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) +{ + BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE]; + t_md5_ctx ctx; + + (void)ft_ssl; + ft_md5_init(&ctx); + ft_md5_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); + ft_md5_final(digest, &ctx); + ft_md5_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_sha224_file.c b/src/ft_ssl_sha224_file.c new file mode 100644 index 00000000..02e271b7 --- /dev/null +++ b/src/ft_ssl_sha224_file.c @@ -0,0 +1,30 @@ +#include "ft_ssl.h" +#include "ft_sha.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" +#include "libft.h" + +void ft_ssl_sha224_file(const char *filename, t_ft_ssl *ft_ssl) +{ + int fd; + int len; + BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; + BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; + t_sha256_ctx ctx; + BYTE1 buf[FT_SSL_BUFFER_SIZE]; + + (void)ft_ssl; + if ((fd = open(filename, O_RDONLY)) == -1) + { + perror("Error: "); + exit(1); + } + ft_sha224_init(&ctx); + while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0) + ft_sha224_update(&ctx, buf, len); + ft_sha224_final(digest, &ctx); + ft_sha224_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_sha224_stdin.c b/src/ft_ssl_sha224_stdin.c new file mode 100644 index 00000000..47ea08f4 --- /dev/null +++ b/src/ft_ssl_sha224_stdin.c @@ -0,0 +1,24 @@ +#include "ft_ssl.h" +#include "ft_sha.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" +#include "libft.h" + +void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl) +{ + int len; + BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; + BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; + t_sha256_ctx ctx; + BYTE1 buf[FT_SSL_BUFFER_SIZE]; + + (void)ft_ssl; + ft_sha224_init(&ctx); + while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) + ft_sha224_update(&ctx, buf, len); + ft_sha224_final(digest, &ctx); + ft_sha224_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_sha224_string.c b/src/ft_ssl_sha224_string.c new file mode 100644 index 00000000..ab0ff945 --- /dev/null +++ b/src/ft_ssl_sha224_string.c @@ -0,0 +1,21 @@ +#include "ft_ssl.h" +#include "ft_sha.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" +#include "libft.h" + +void ft_ssl_sha224_string(const char *str, t_ft_ssl *ft_ssl) +{ + BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; + BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; + t_sha256_ctx ctx; + + (void)ft_ssl; + ft_sha224_init(&ctx); + ft_sha224_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); + ft_sha224_final(digest, &ctx); + ft_sha224_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_sha256_file.c b/src/ft_ssl_sha256_file.c new file mode 100644 index 00000000..ba6015d9 --- /dev/null +++ b/src/ft_ssl_sha256_file.c @@ -0,0 +1,30 @@ +#include "ft_ssl.h" +#include "ft_sha.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" +#include "libft.h" + +void ft_ssl_sha256_file(const char *filename, t_ft_ssl *ft_ssl) +{ + int fd; + int len; + BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; + t_sha256_ctx ctx; + BYTE1 buf[FT_SSL_BUFFER_SIZE]; + + (void)ft_ssl; + if ((fd = open(filename, O_RDONLY)) == -1) + { + perror("Error: "); + exit(1); + } + ft_sha256_init(&ctx); + while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0) + ft_sha256_update(&ctx, buf, len); + ft_sha256_final(digest, &ctx); + ft_sha256_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_sha256_stdin.c b/src/ft_ssl_sha256_stdin.c new file mode 100644 index 00000000..c30ad442 --- /dev/null +++ b/src/ft_ssl_sha256_stdin.c @@ -0,0 +1,24 @@ +#include "ft_ssl.h" +#include "ft_sha.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" +#include "libft.h" + +void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl) +{ + int len; + BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; + t_sha256_ctx ctx; + BYTE1 buf[FT_SSL_BUFFER_SIZE]; + + (void)ft_ssl; + ft_sha256_init(&ctx); + while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) + ft_sha256_update(&ctx, buf, len); + ft_sha256_final(digest, &ctx); + ft_sha256_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_sha256_string.c b/src/ft_ssl_sha256_string.c new file mode 100644 index 00000000..e4aebefa --- /dev/null +++ b/src/ft_ssl_sha256_string.c @@ -0,0 +1,21 @@ +#include "ft_ssl.h" +#include "ft_sha.h" +#include "fcntl.h" +#include "stdio.h" +#include "unistd.h" +#include "libft.h" + +void ft_ssl_sha256_string(const char *str, t_ft_ssl *ft_ssl) +{ + BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; + t_sha256_ctx ctx; + + (void)ft_ssl; + ft_sha256_init(&ctx); + ft_sha256_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); + ft_sha256_final(digest, &ctx); + ft_sha256_digest_string(digest, digest_string); + ft_putstr((const char *)digest_string); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/ft_ssl_usage.c b/src/ft_ssl_usage.c new file mode 100644 index 00000000..9c580f03 --- /dev/null +++ b/src/ft_ssl_usage.c @@ -0,0 +1,8 @@ +#include "ft_ssl.h" +#include "libft.h" + +void ft_ssl_usage(void) +{ + ft_putstr("usage: \n"); + exit(1); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 9a45afdf..0fdea363 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,50 @@ -#include +#include "ft_ssl.h" +#include "libft.h" -int main() +static int find_flag(const char *expected, char *real) { - printf("hello from sha256\n"); + 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) + { + 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; +} + +int main(int argc, char **argv) +{ + t_ft_ssl ft_ssl; + int i; + + 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 || i == argc) + ft_ssl.process_stdin(&ft_ssl); + while (i < argc) + { + if (find_flag("-s", argv[i]) && ++i < argc) + ft_ssl.process_string(argv[i++], &ft_ssl); + else + ft_ssl.process_file(argv[i++], &ft_ssl); + } + if (argc == 2) + ft_ssl.process_stdin(&ft_ssl); return (0); } \ No newline at end of file diff --git a/src/md5/ft_md5_update.c b/src/md5/ft_md5_update.c index fb092823..e489bf64 100644 --- a/src/md5/ft_md5_update.c +++ b/src/md5/ft_md5_update.c @@ -17,7 +17,7 @@ void ft_md5_update(t_md5_ctx *c, BYTE1 * message, BYTE8 message_len) i = part_block_len; while(i + 63 < message_len) { - ft_md5_transform(c, &c->block[i]); + ft_md5_transform(c, &message[i]); i += FT_MD5_BLOCK_SIZE; } curent_block_index = 0; diff --git a/src/sha/ft_sha256_update.c b/src/sha/ft_sha256_update.c index 4e218d0a..a8f59bf4 100644 --- a/src/sha/ft_sha256_update.c +++ b/src/sha/ft_sha256_update.c @@ -17,7 +17,7 @@ void ft_sha256_update(t_sha256_ctx *c, BYTE1 *message, BYTE8 message_len) i = part_block_len; while (i + 63 < message_len) { - ft_sha256_transform(c, &c->block[i]); + ft_sha256_transform(c, &message[i]); i += FT_SHA256_BLOCK_SIZE; } curent_block_index = 0;