diff --git a/Makefile b/Makefile index f6ed4760..1eaac5a3 100644 --- a/Makefile +++ b/Makefile @@ -73,9 +73,11 @@ 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_ssl.h b/inc/ft_ssl.h index e8c5186e..7d9f1111 100644 --- a/inc/ft_ssl.h +++ b/inc/ft_ssl.h @@ -29,6 +29,7 @@ typedef struct s_flags int quiet; int reverse; int print_stdin; + int something_printed; } t_flags; typedef struct s_alorithm @@ -55,19 +56,26 @@ 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_md5_print(const char *target, + 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_print(const char *target, + unsigned char *digest, + 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); +void ft_ssl_sha256_print(const char *target, + unsigned char *digest, + t_ft_ssl *ft_ssl); #endif diff --git a/src/ft_ssl_init.c b/src/ft_ssl_init.c index 4c304007..a6b327bb 100644 --- a/src/ft_ssl_init.c +++ b/src/ft_ssl_init.c @@ -26,6 +26,14 @@ static t_algorithm *init_algorithms(void) return algs; } +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; @@ -42,6 +50,7 @@ void ft_ssl_init(char *alg_name, t_ft_ssl *ft_ssl) i = 0; alg = 0; + init_flags(ft_ssl); alg_walker = init_algorithms(); while (i < FT_SSL_ALGS_COUNT) { diff --git a/src/ft_ssl_md5_file.c b/src/ft_ssl_md5_file.c index ae89e7de..67d2e73e 100644 --- a/src/ft_ssl_md5_file.c +++ b/src/ft_ssl_md5_file.c @@ -23,5 +23,5 @@ void ft_ssl_md5_file(const char *filename, t_ft_ssl *ft_ssl) 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); + ft_ssl_md5_print(filename, 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 index 11266c09..cd136fb8 100644 --- a/src/ft_ssl_md5_print.c +++ b/src/ft_ssl_md5_print.c @@ -2,12 +2,43 @@ #include "ft_ssl.h" #include "libft.h" -void ft_ssl_md5_print(unsigned char *digest, - t_ft_ssl *ft_ssl) +static void print_name(const char *name, int reverse) +{ + if (reverse) + { + ft_putstr(" "); + ft_putstr(name); + } + else + { + ft_putstr("MD5 ("); + ft_putstr(name); + ft_putstr(") = "); + } +} + +void ft_ssl_md5_print +( + const char *target, + 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); + 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; } \ No newline at end of file diff --git a/src/ft_ssl_md5_stdin.c b/src/ft_ssl_md5_stdin.c index dc8622a5..4444aa1a 100644 --- a/src/ft_ssl_md5_stdin.c +++ b/src/ft_ssl_md5_stdin.c @@ -20,5 +20,5 @@ void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl) ft_md5_update(&ctx, buf, len); } ft_md5_final(digest, &ctx); - ft_ssl_md5_print(digest, ft_ssl); + ft_ssl_md5_print(NULL, 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 index e4102564..12e3f806 100644 --- a/src/ft_ssl_md5_string.c +++ b/src/ft_ssl_md5_string.c @@ -8,14 +8,10 @@ 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"); + ft_ssl_md5_print(str, digest, ft_ssl); } \ No newline at end of file diff --git a/src/ft_ssl_sha224_file.c b/src/ft_ssl_sha224_file.c index 02e271b7..c741650e 100644 --- a/src/ft_ssl_sha224_file.c +++ b/src/ft_ssl_sha224_file.c @@ -10,21 +10,18 @@ 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: "); + 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_sha224_digest_string(digest, digest_string); - ft_putstr((const char *)digest_string); - ft_putstr("\n"); + ft_ssl_sha224_print(filename, digest, ft_ssl); } \ No newline at end of file diff --git a/src/ft_ssl_sha224_print.c b/src/ft_ssl_sha224_print.c new file mode 100644 index 00000000..e2730df3 --- /dev/null +++ b/src/ft_ssl_sha224_print.c @@ -0,0 +1,44 @@ +#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 +) +{ + 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; +} \ No newline at end of file diff --git a/src/ft_ssl_sha224_stdin.c b/src/ft_ssl_sha224_stdin.c index 47ea08f4..0ab15765 100644 --- a/src/ft_ssl_sha224_stdin.c +++ b/src/ft_ssl_sha224_stdin.c @@ -9,16 +9,17 @@ 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))) + { + if (ft_ssl->flags.print_stdin) + write(1, buf, len); 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"); + ft_ssl_sha224_print(NULL, digest, ft_ssl); } \ No newline at end of file diff --git a/src/ft_ssl_sha224_string.c b/src/ft_ssl_sha224_string.c index ab0ff945..ae547558 100644 --- a/src/ft_ssl_sha224_string.c +++ b/src/ft_ssl_sha224_string.c @@ -8,14 +8,11 @@ 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"); + ft_ssl_sha224_print(str, digest, ft_ssl); } \ No newline at end of file diff --git a/src/ft_ssl_sha256_file.c b/src/ft_ssl_sha256_file.c index ba6015d9..cf6ab7f3 100644 --- a/src/ft_ssl_sha256_file.c +++ b/src/ft_ssl_sha256_file.c @@ -10,21 +10,18 @@ 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: "); + 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_sha256_digest_string(digest, digest_string); - ft_putstr((const char *)digest_string); - ft_putstr("\n"); + ft_ssl_sha256_print(filename, digest, ft_ssl); } \ No newline at end of file diff --git a/src/ft_ssl_sha256_print.c b/src/ft_ssl_sha256_print.c new file mode 100644 index 00000000..98a96579 --- /dev/null +++ b/src/ft_ssl_sha256_print.c @@ -0,0 +1,44 @@ +#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_sha256_print +( + const char *target, + unsigned char *digest, + t_ft_ssl *ft_ssl +) +{ + 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) + { + 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; +} \ No newline at end of file diff --git a/src/ft_ssl_sha256_stdin.c b/src/ft_ssl_sha256_stdin.c index c30ad442..94c9a570 100644 --- a/src/ft_ssl_sha256_stdin.c +++ b/src/ft_ssl_sha256_stdin.c @@ -9,16 +9,17 @@ 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))) + { + if (ft_ssl->flags.print_stdin) + write(1, buf, len); 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 + ft_ssl_sha256_print(NULL, digest, ft_ssl); +} diff --git a/src/ft_ssl_sha256_string.c b/src/ft_ssl_sha256_string.c index e4aebefa..c0126d27 100644 --- a/src/ft_ssl_sha256_string.c +++ b/src/ft_ssl_sha256_string.c @@ -8,14 +8,11 @@ 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"); + ft_ssl_sha256_print(str, digest, ft_ssl); } \ No newline at end of file diff --git a/src/ft_ssl_usage.c b/src/ft_ssl_usage.c index 9c580f03..12249985 100644 --- a/src/ft_ssl_usage.c +++ b/src/ft_ssl_usage.c @@ -3,6 +3,9 @@ void ft_ssl_usage(void) { - ft_putstr("usage: \n"); + ft_putstr("\n\nusage: \n"); + ft_putstr("ft_ssl algorithm [-p|-q|-r] [[-s string...] [file...]]\n\n"); + ft_putstr("algorithms:\n"); + ft_putstr("md5 sha256 sha224\n\n\n"); exit(1); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 0fdea363..8eafa65b 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,27 @@ static int process_flags(int argc, char **argv, t_ft_ssl *ft_ssl) } 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); + } +} int main(int argc, char **argv) { t_ft_ssl ft_ssl; @@ -35,16 +55,10 @@ int main(int argc, char **argv) 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) + if (ft_ssl.flags.print_stdin) 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) + process_strings_and_files(i, argc, argv, &ft_ssl); + if (!ft_ssl.flags.something_printed) ft_ssl.process_stdin(&ft_ssl); return (0); } \ No newline at end of file