From 8fe226598b00f9c621bb909c303f2b40590d9a56 Mon Sep 17 00:00:00 2001 From: Gregory Date: Sun, 3 Feb 2019 22:05:32 +0200 Subject: [PATCH] add sha256, sha224 to new dispatch table --- Makefile | 24 +-- inc/ft_sha.h | 183 +++++++++++++++--- src/ft_ssl_sha224_print.c | 56 ------ src/main.c | 11 +- src/md5/ft_md5.c | 6 +- src/sha/ft_sha224.c | 84 ++++++++ .../ft_sha224_file.c} | 21 +- .../ft_sha224_stdin.c} | 21 +- .../ft_sha224_string.c} | 19 +- src/sha/ft_sha256.c | 84 ++++++++ .../ft_sha256_file.c} | 25 +-- .../ft_sha256_stdin.c} | 21 +- .../ft_sha256_string.c} | 19 +- .../ft_sha_print.c} | 38 ++-- src/sha/ft_sha_usage.c | 23 +++ t/ete.sh | 73 +++++++ 16 files changed, 524 insertions(+), 184 deletions(-) delete mode 100644 src/ft_ssl_sha224_print.c create mode 100644 src/sha/ft_sha224.c rename src/{ft_ssl_sha224_file.c => sha/ft_sha224_file.c} (69%) rename src/{ft_ssl_sha224_stdin.c => sha/ft_sha224_stdin.c} (71%) rename src/{ft_ssl_sha224_string.c => sha/ft_sha224_string.c} (74%) create mode 100644 src/sha/ft_sha256.c rename src/{ft_ssl_sha256_file.c => sha/ft_sha256_file.c} (69%) rename src/{ft_ssl_sha256_stdin.c => sha/ft_sha256_stdin.c} (71%) rename src/{ft_ssl_sha256_string.c => sha/ft_sha256_string.c} (74%) rename src/{ft_ssl_sha256_print.c => sha/ft_sha_print.c} (61%) create mode 100644 src/sha/ft_sha_usage.c create mode 100755 t/ete.sh diff --git a/Makefile b/Makefile index 932c9cd1..3b059203 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ MUINUT_INC := -I $(MUINUT_DIR) MD5_SRC = ft_md5.c \ ft_md5_stdin.c \ ft_md5_string.c \ - ft_md5_file.c \ + ft_md5_file.c \ ft_md5_print.c \ ft_md5_init.c \ ft_md5_update.c \ @@ -61,7 +61,11 @@ MD5_SRC = ft_md5.c \ ft_md5_padding.c \ ft_md5_digest_string.c -SHA_SRC = ft_sha256_init.c \ +SHA_SRC = ft_sha256.c \ + ft_sha256_string.c \ + ft_sha256_file.c \ + ft_sha256_stdin.c \ + ft_sha256_init.c \ ft_sha256_update.c \ ft_sha256_decode.c \ ft_sha256_encode_len.c \ @@ -71,24 +75,22 @@ SHA_SRC = ft_sha256_init.c \ ft_sha256_final.c \ ft_sha256_digest_string.c \ ft_sha256_constants.c \ + ft_sha224_string.c \ + ft_sha224_file.c \ + ft_sha224_stdin.c \ + ft_sha224.c \ ft_sha224_init.c \ ft_sha224_update.c \ ft_sha224_final.c \ - ft_sha224_digest_string.c + ft_sha224_digest_string.c \ + ft_sha_print.c \ + ft_sha_usage.c BASE64_SRC = ft_base64_init.c \ ft_base64_fill_buffer.c \ ft_base64_encode.c SRC = main.c \ - ft_ssl_sha256_file.c \ - ft_ssl_sha256_stdin.c \ - ft_ssl_sha256_string.c \ - ft_ssl_sha256_print.c \ - ft_ssl_sha224_file.c \ - ft_ssl_sha224_stdin.c \ - ft_ssl_sha224_string.c \ - ft_ssl_sha224_print.c \ ft_ssl_usage.c SRC += $(MD5_SRC) \ diff --git a/inc/ft_sha.h b/inc/ft_sha.h index c37975ec..137a257b 100644 --- a/inc/ft_sha.h +++ b/inc/ft_sha.h @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/10/23 19:20:23 by gtertysh #+# #+# */ -/* Updated: 2018/10/23 19:29:10 by gtertysh ### ########.fr */ +/* Updated: 2019/02/03 18:41:48 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ # include +# define FT_SHA256_READ_BLOCK_SIZE 1024 # define FT_SHA256_BLOCK_SIZE 64 # define FT_SHA256_WORDS_COUNT 16 # define FT_SHA256_MESSAGE_LENGTH_BYTE 8 @@ -67,36 +68,156 @@ typedef struct s_temp_registers t_byte4 h; } t_temp_registers; -void ft_sha256_init(t_sha256_ctx *ctx); -void ft_sha224_init(t_sha256_ctx *ctx); -void ft_sha256_update(t_sha256_ctx *ctx, - t_byte1 *message, t_byte8 len); -void ft_sha224_update(t_sha256_ctx *ctx, - t_byte1 *message, t_byte8 len); -void ft_sha256_final( - t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], - t_sha256_ctx *ctx); -void ft_sha224_final( - t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], - t_sha256_ctx *ctx); -void ft_sha256_transform(t_sha256_ctx *ctx, - t_byte1 block[FT_SHA256_BLOCK_SIZE]); -void ft_sha256_decode(t_byte4 w[FT_SHA256_WORDS_COUNT], - t_byte1 b[FT_SHA256_BLOCK_SIZE]); -void ft_sha256_encode_len( - t_byte1 b[FT_SHA256_MESSAGE_LENGTH_BYTE], - t_byte8 len); -void ft_sha256_encode_register( - t_byte1 digest_part[FT_SHA256_REG_SIZE_BYTE], - t_byte4 reg); -void ft_sha256_padding( - t_byte1 padding[FT_SHA256_BLOCK_SIZE]); -void ft_sha256_digest_string( - t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], - t_byte1 digst_string[FT_SHA256_STRING_SIZE_BYTE]); -void ft_sha224_digest_string( - t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE], - t_byte1 digst_string[FT_SHA224_STRING_SIZE_BYTE]); +typedef struct s_sha_flags +{ + int quiet; + int reverse; + int print_stdin; + int something_printed; +} t_sha_flags; + +void ft_sha256 +( + int argc, + char **argv +); + +void ft_sha256_string +( + t_sha256_ctx *ctx, + t_byte1 *message, + t_sha_flags *flags +); + +void ft_sha256_file +( + t_sha256_ctx *ctx, + t_byte1 *filename, + t_sha_flags *flags +); + +void ft_sha256_stdin +( + t_sha256_ctx *ctx, + t_sha_flags *flags +); + +void ft_sha256_init +( + t_sha256_ctx *ctx +); + +void ft_sha256_update +( + t_sha256_ctx *ctx, + t_byte1 *message, + t_byte8 len +); + +void ft_sha256_final +( + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], + t_sha256_ctx *ctx +); + +void ft_sha256_transform +( + t_sha256_ctx *ctx, + t_byte1 block[FT_SHA256_BLOCK_SIZE] +); + +void ft_sha256_decode +( + t_byte4 w[FT_SHA256_WORDS_COUNT], + t_byte1 b[FT_SHA256_BLOCK_SIZE] +); + +void ft_sha256_encode_len +( + t_byte1 b[FT_SHA256_MESSAGE_LENGTH_BYTE], + t_byte8 len +); + +void ft_sha256_encode_register +( + t_byte1 digest_part[FT_SHA256_REG_SIZE_BYTE], + t_byte4 reg +); + +void ft_sha256_padding +( + t_byte1 padding[FT_SHA256_BLOCK_SIZE] +); + +void ft_sha256_digest_string +( + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], + t_byte1 digst_string[FT_SHA256_STRING_SIZE_BYTE] +); + t_byte4 *ft_sha256_constants(void); +void ft_sha224 +( + int argc, + char **argv +); + +void ft_sha224_string +( + t_sha256_ctx *ctx, + t_byte1 *message, + t_sha_flags *flags +); + +void ft_sha224_file +( + t_sha256_ctx *ctx, + t_byte1 *filename, + t_sha_flags *flags +); + +void ft_sha224_stdin +( + t_sha256_ctx *ctx, + t_sha_flags *flags +); + +void ft_sha224_init +( + t_sha256_ctx *ctx +); + +void ft_sha224_update +( + t_sha256_ctx *ctx, + t_byte1 *message, + t_byte8 len +); + +void ft_sha224_final +( + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], + t_sha256_ctx *ctx +); + +void ft_sha224_digest_string +( + t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE], + t_byte1 digst_string[FT_SHA224_STRING_SIZE_BYTE] +); + +void ft_sha_usage +( + const char *alorithm +); + +void ft_sha_print +( + const char *algorithm, + t_byte1 *message, + t_byte1 *digest, + t_sha_flags *flags +); + #endif diff --git a/src/ft_ssl_sha224_print.c b/src/ft_ssl_sha224_print.c deleted file mode 100644 index 03e2ebae..00000000 --- a/src/ft_ssl_sha224_print.c +++ /dev/null @@ -1,56 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_ssl_sha224_print.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gtertysh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/23 20:31:27 by gtertysh #+# #+# */ -/* Updated: 2018/10/23 20:31:42 by gtertysh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_sha.h" -#include "ft_ssl.h" -#include "libft.h" - -static void print_name(const char *name, int reverse) -{ - if (reverse) - { - ft_putstr(" "); - ft_putstr(name); - } - else - { - ft_putstr("SHA256 ("); - ft_putstr(name); - ft_putstr(") = "); - } -} - -void ft_ssl_sha224_print -( - const char *target, - unsigned char *digest, - t_ft_ssl *ft_ssl -) -{ - t_byte1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; - - ft_sha224_digest_string(digest, digest_string); - if (target && !ft_ssl->flags.quiet && ft_ssl->flags.reverse) - { - ft_putstr((const char *)digest_string); - print_name(target, 1); - } - else if (target && !ft_ssl->flags.quiet) - { - print_name(target, 0); - ft_putstr((const char *)digest_string); - } - else - ft_putstr((const char *)digest_string); - ft_putstr("\n"); - ft_ssl->flags.something_printed = 1; -} diff --git a/src/main.c b/src/main.c index 01cd7ecd..887aff4c 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,7 @@ #include "ft_ssl.h" #include "ft_md5.h" +#include "ft_sha.h" #include "libft.h" t_algorithm g_algorithms[] = { @@ -19,6 +20,14 @@ t_algorithm g_algorithms[] = { "md5", ft_md5, }, + { + "sha256", + ft_sha256, + }, + { + "sha224", + ft_sha224, + }, {NULL, NULL} }; @@ -34,7 +43,7 @@ int main(int argc, char **argv) while (alg_walker->function) { if (ft_strcmp(alg_walker->name, alg_name) == 0) - alg_walker->function(argc, argv); + alg_walker->function(argc - 1, ++argv); alg_walker++; } ft_ssl_usage(); diff --git a/src/md5/ft_md5.c b/src/md5/ft_md5.c index 5c527180..18c9d9ec 100644 --- a/src/md5/ft_md5.c +++ b/src/md5/ft_md5.c @@ -20,10 +20,6 @@ static void ft_md5_usage(void) exit(1); } -/* -** argc and argv are same as in main(); -*/ - static void init_flags(t_md5_flags *flags) { flags->quiet = 0; @@ -36,7 +32,7 @@ static int read_flags(int argc, char **argv, t_md5_flags *flags) { int i; - i = 2; + i = 1; while (i < argc) { if (ft_strcmp("-r", argv[i]) == 0) diff --git a/src/sha/ft_sha224.c b/src/sha/ft_sha224.c new file mode 100644 index 00000000..dcf0b49c --- /dev/null +++ b/src/sha/ft_sha224.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha224.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/03 18:42:15 by gtertysh #+# #+# */ +/* Updated: 2019/02/03 18:42:42 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_sha.h" +#include "libft.h" + +static void init_flags(t_sha_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_sha_flags *flags) +{ + int i; + + i = 1; + 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 argc, + char **argv, + t_sha_flags *flags, + t_sha256_ctx *ctx +) +{ + int i; + + i = 0; + while (i < argc) + { + if (ft_strcmp("-s", argv[i]) == 0) + { + if (i + 1 >= argc) + ft_sha_usage("SHA224"); + ft_sha224_string(ctx, (t_byte1 *)argv[++i], flags); + i++; + } + else + ft_sha224_file(ctx, (t_byte1 *)argv[i++], flags); + } +} + +void ft_sha224(int argc, char **argv) +{ + int flags_readed; + t_sha_flags flags; + t_sha256_ctx ctx; + + init_flags(&flags); + flags_readed = read_flags(argc, argv, &flags); + if (flags.print_stdin) + ft_sha224_stdin(&ctx, &flags); + process_strings_and_files( + argc - flags_readed, argv + flags_readed, &flags, &ctx); + if (!flags.something_printed) + ft_sha224_stdin(&ctx, &flags); + exit(0); +} diff --git a/src/ft_ssl_sha224_file.c b/src/sha/ft_sha224_file.c similarity index 69% rename from src/ft_ssl_sha224_file.c rename to src/sha/ft_sha224_file.c index 1f032f13..675f896c 100644 --- a/src/ft_ssl_sha224_file.c +++ b/src/sha/ft_sha224_file.c @@ -10,30 +10,29 @@ /* */ /* ************************************************************************** */ -#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) +void ft_sha224_file(t_sha256_ctx *ctx, t_byte1 *filename, t_sha_flags *flags) { int fd; int len; + t_byte1 buf[FT_SHA256_READ_BLOCK_SIZE]; t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - t_byte1 buf[FT_SSL_BUFFER_SIZE]; + t_byte1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; - (void)ft_ssl; - if ((fd = open(filename, O_RDONLY)) == -1) + ft_sha224_init(ctx); + if ((fd = open((const char *)filename, O_RDONLY)) == -1) { perror("ft_ssl"); 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_ssl_sha224_print(filename, digest, ft_ssl); + while ((len = read(fd, buf, FT_SHA256_READ_BLOCK_SIZE)) > 0) + ft_sha224_update(ctx, buf, len); + ft_sha224_final(digest, ctx); + ft_sha224_digest_string(digest, digest_string); + ft_sha_print("SHA224", filename, digest_string, flags); } diff --git a/src/ft_ssl_sha224_stdin.c b/src/sha/ft_sha224_stdin.c similarity index 71% rename from src/ft_ssl_sha224_stdin.c rename to src/sha/ft_sha224_stdin.c index b2fba97b..8543eaf6 100644 --- a/src/ft_ssl_sha224_stdin.c +++ b/src/sha/ft_sha224_stdin.c @@ -10,28 +10,27 @@ /* */ /* ************************************************************************** */ -#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) +void ft_sha224_stdin(t_sha256_ctx *ctx, t_sha_flags *flags) { int len; + t_byte1 buf[FT_SHA256_READ_BLOCK_SIZE]; t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - t_byte1 buf[FT_SSL_BUFFER_SIZE]; + t_byte1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; - (void)ft_ssl; - ft_sha224_init(&ctx); - while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) + ft_sha224_init(ctx); + while ((len = read(0, buf, FT_SHA256_READ_BLOCK_SIZE))) { - if (ft_ssl->flags.print_stdin) + if (flags->print_stdin) write(1, buf, len); - ft_sha224_update(&ctx, buf, len); + ft_sha224_update(ctx, buf, len); } - ft_sha224_final(digest, &ctx); - ft_ssl_sha224_print(NULL, digest, ft_ssl); + ft_sha224_final(digest, ctx); + ft_sha224_digest_string(digest, digest_string); + ft_sha_print("SHA224", NULL, digest_string, flags); } diff --git a/src/ft_ssl_sha224_string.c b/src/sha/ft_sha224_string.c similarity index 74% rename from src/ft_ssl_sha224_string.c rename to src/sha/ft_sha224_string.c index 44bb90b1..a70c521e 100644 --- a/src/ft_ssl_sha224_string.c +++ b/src/sha/ft_sha224_string.c @@ -10,21 +10,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_string(const char *str, t_ft_ssl *ft_ssl) +void ft_sha224_string( + t_sha256_ctx *ctx, + t_byte1 *message, + t_sha_flags *flags +) { t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; + t_byte1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; - (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_ssl_sha224_print(str, digest, ft_ssl); + ft_sha224_init(ctx); + ft_sha224_update(ctx, message, ft_strlen((const char *)message)); + ft_sha224_final(digest, ctx); + ft_sha224_digest_string(digest, digest_string); + ft_sha_print("SHA224", message, digest_string, flags); } diff --git a/src/sha/ft_sha256.c b/src/sha/ft_sha256.c new file mode 100644 index 00000000..3a04ddc7 --- /dev/null +++ b/src/sha/ft_sha256.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/03 22:03:58 by gtertysh #+# #+# */ +/* Updated: 2019/02/03 22:04:16 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_sha.h" +#include "libft.h" + +static void init_flags(t_sha_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_sha_flags *flags) +{ + int i; + + i = 1; + 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 argc, + char **argv, + t_sha_flags *flags, + t_sha256_ctx *ctx +) +{ + int i; + + i = 0; + while (i < argc) + { + if (ft_strcmp("-s", argv[i]) == 0) + { + if (i + 1 >= argc) + ft_sha_usage("SHA256"); + ft_sha256_string(ctx, (t_byte1 *)argv[++i], flags); + i++; + } + else + ft_sha256_file(ctx, (t_byte1 *)argv[i++], flags); + } +} + +void ft_sha256(int argc, char **argv) +{ + int flags_readed; + t_sha_flags flags; + t_sha256_ctx ctx; + + init_flags(&flags); + flags_readed = read_flags(argc, argv, &flags); + if (flags.print_stdin) + ft_sha256_stdin(&ctx, &flags); + process_strings_and_files( + argc - flags_readed, argv + flags_readed, &flags, &ctx); + if (!flags.something_printed) + ft_sha256_stdin(&ctx, &flags); + exit(0); +} diff --git a/src/ft_ssl_sha256_file.c b/src/sha/ft_sha256_file.c similarity index 69% rename from src/ft_ssl_sha256_file.c rename to src/sha/ft_sha256_file.c index 95f1c797..51ef0406 100644 --- a/src/ft_ssl_sha256_file.c +++ b/src/sha/ft_sha256_file.c @@ -10,30 +10,33 @@ /* */ /* ************************************************************************** */ -#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) +void ft_sha256_file( + t_sha256_ctx *ctx, + t_byte1 *filename, + t_sha_flags *flags +) { int fd; int len; + t_byte1 buf[FT_SHA256_READ_BLOCK_SIZE]; t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - t_byte1 buf[FT_SSL_BUFFER_SIZE]; + t_byte1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; - (void)ft_ssl; - if ((fd = open(filename, O_RDONLY)) == -1) + ft_sha256_init(ctx); + if ((fd = open((const char *)filename, O_RDONLY)) == -1) { perror("ft_ssl"); 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_ssl_sha256_print(filename, digest, ft_ssl); + while ((len = read(fd, buf, FT_SHA256_READ_BLOCK_SIZE)) > 0) + ft_sha256_update(ctx, buf, len); + ft_sha256_final(digest, ctx); + ft_sha256_digest_string(digest, digest_string); + ft_sha_print("SHA256", filename, digest_string, flags); } diff --git a/src/ft_ssl_sha256_stdin.c b/src/sha/ft_sha256_stdin.c similarity index 71% rename from src/ft_ssl_sha256_stdin.c rename to src/sha/ft_sha256_stdin.c index 0f3ff63f..761de23f 100644 --- a/src/ft_ssl_sha256_stdin.c +++ b/src/sha/ft_sha256_stdin.c @@ -10,28 +10,27 @@ /* */ /* ************************************************************************** */ -#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) +void ft_sha256_stdin(t_sha256_ctx *ctx, t_sha_flags *flags) { int len; + t_byte1 buf[FT_SHA256_READ_BLOCK_SIZE]; t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - t_byte1 buf[FT_SSL_BUFFER_SIZE]; + t_byte1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; - (void)ft_ssl; - ft_sha256_init(&ctx); - while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) + ft_sha256_init(ctx); + while ((len = read(0, buf, FT_SHA256_READ_BLOCK_SIZE))) { - if (ft_ssl->flags.print_stdin) + if (flags->print_stdin) write(1, buf, len); - ft_sha256_update(&ctx, buf, len); + ft_sha256_update(ctx, buf, len); } - ft_sha256_final(digest, &ctx); - ft_ssl_sha256_print(NULL, digest, ft_ssl); + ft_sha256_final(digest, ctx); + ft_sha256_digest_string(digest, digest_string); + ft_sha_print("SHA256", NULL, digest_string, flags); } diff --git a/src/ft_ssl_sha256_string.c b/src/sha/ft_sha256_string.c similarity index 74% rename from src/ft_ssl_sha256_string.c rename to src/sha/ft_sha256_string.c index 8fd80c93..65d73522 100644 --- a/src/ft_ssl_sha256_string.c +++ b/src/sha/ft_sha256_string.c @@ -10,21 +10,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_string(const char *str, t_ft_ssl *ft_ssl) +void ft_sha256_string( + t_sha256_ctx *ctx, + t_byte1 *message, + t_sha_flags *flags +) { t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; + t_byte1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; - (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_ssl_sha256_print(str, digest, ft_ssl); + ft_sha256_init(ctx); + ft_sha256_update(ctx, message, ft_strlen((const char *)message)); + ft_sha256_final(digest, ctx); + ft_sha256_digest_string(digest, digest_string); + ft_sha_print("SHA256", message, digest_string, flags); } diff --git a/src/ft_ssl_sha256_print.c b/src/sha/ft_sha_print.c similarity index 61% rename from src/ft_ssl_sha256_print.c rename to src/sha/ft_sha_print.c index c9b5804a..59a098b6 100644 --- a/src/ft_ssl_sha256_print.c +++ b/src/sha/ft_sha_print.c @@ -11,46 +11,44 @@ /* ************************************************************************** */ #include "ft_sha.h" -#include "ft_ssl.h" #include "libft.h" -static void print_name(const char *name, int reverse) +static void print_message(const char *algorithm, t_byte1 *message, int reverse) { if (reverse) { ft_putstr(" "); - ft_putstr(name); + ft_putstr((const char *)message); } else { - ft_putstr("SHA256 ("); - ft_putstr(name); + ft_putstr(algorithm); + ft_putstr(" ("); + ft_putstr((const char *)message); ft_putstr(") = "); } } -void ft_ssl_sha256_print +void ft_sha_print ( - const char *target, - unsigned char *digest, - t_ft_ssl *ft_ssl + const char *algorithm, + t_byte1 *target, + t_byte1 *digest, + t_sha_flags *flags ) { - t_byte1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; - - ft_sha256_digest_string(digest, digest_string); - if (target && !ft_ssl->flags.quiet && ft_ssl->flags.reverse) + if (target && !flags->quiet && flags->reverse) { - ft_putstr((const char *)digest_string); - print_name(target, 1); + ft_putstr((const char *)digest); + print_message(algorithm, target, 1); } - else if (target && !ft_ssl->flags.quiet) + else if (target && !flags->quiet) { - print_name(target, 0); - ft_putstr((const char *)digest_string); + print_message(algorithm, target, 0); + ft_putstr((const char *)digest); } else - ft_putstr((const char *)digest_string); + ft_putstr((const char *)digest); ft_putstr("\n"); - ft_ssl->flags.something_printed = 1; + flags->something_printed = 1; } diff --git a/src/sha/ft_sha_usage.c b/src/sha/ft_sha_usage.c new file mode 100644 index 00000000..f771de3f --- /dev/null +++ b/src/sha/ft_sha_usage.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha_usage.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/03 22:04:39 by gtertysh #+# #+# */ +/* Updated: 2019/02/03 22:04:41 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sha_usage(const char *algorithm) +{ + ft_putstr(algorithm); + ft_putstr(" Digest usage:\n"); + ft_putstr("ft_ssl "); + ft_putstr(algorithm); + ft_putstr(" [-p|-q|-r] [[-s string...] [file...]]\n\n"); + exit(1); +} diff --git a/t/ete.sh b/t/ete.sh new file mode 100755 index 00000000..c1df219c --- /dev/null +++ b/t/ete.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + + +PROG="$(dirname $0)/../ft_ssl" +FILE="$0" + +assert() { + if [ $# -ne 2 ]; then + echo "assert function should take 2 arguments: cmp1, cmp2" && + exit 1 + fi + + expected=$(eval $1) + real=$(eval $2) + + if [ $? -ne 0 ]; then + exit 1 + fi + + if [ "$expected" = "$real" ]; then + printf '%-80s%s\n' "$2" " OK" + else + tmp1="/tmp/1" + tmp2="/tmp/2" + echo "$1" > tmp1 + echo "$2" > tmp2 + echo "$expected" >> tmp1 + echo "$real" >> tmp2 + vim -d tmp1 tmp2 + echo -e "failed case:\n\n $2"; + exit 1 + fi +} + +function md5_tests { + assert "echo lol | md5" \ + "echo lol | $PROG md5" + + assert "echo lol | md5 -p" \ + "echo lol | $PROG md5 -p" + + assert "md5 -q -s asd" \ + "$PROG md5 -q -s asd" + + assert "md5 -q -s asd -s qwe -s zxc" \ + "$PROG md5 -q -s asd -s qwe -s zxc" + + assert "md5 -q $(which md5)" \ + "$PROG md5 -q $(which md5)" + + assert "md5 -r $(which md5)" \ + "$PROG md5 -r $(which md5)" + + assert "echo lol | md5 -p -r $(which md5)" \ + "echo lol | $PROG md5 -p -r $(which md5)" + + assert "echo 'GL HF lets go' | md5 -q -p -s foo $FILE" \ + "echo 'GL HF lets go' | $PROG md5 -q -p -s foo $FILE" + + assert "echo 'one more' | md5 -q -p -s foo -s bar $FILE" \ + "echo 'one more' | $PROG md5 -q -p -s foo -s bar $FILE" + + assert "echo 'just to be extra clear' | md5 -r -q -p -s foo $FILE" \ + "echo 'just to be extra clear' | $PROG md5 -r -q -p -s foo $FILE" +} + +function run { + clear + md5_tests + echo "passed!" +} + +run \ No newline at end of file