added print functions

This commit is contained in:
Gregory 2018-10-23 18:44:21 +03:00
parent 50f726188d
commit 5ea4c064b1
17 changed files with 192 additions and 51 deletions

View file

@ -73,9 +73,11 @@ SRC = main.c \
ft_ssl_sha256_file.c \ ft_ssl_sha256_file.c \
ft_ssl_sha256_stdin.c \ ft_ssl_sha256_stdin.c \
ft_ssl_sha256_string.c \ ft_ssl_sha256_string.c \
ft_ssl_sha256_print.c \
ft_ssl_sha224_file.c \ ft_ssl_sha224_file.c \
ft_ssl_sha224_stdin.c \ ft_ssl_sha224_stdin.c \
ft_ssl_sha224_string.c \ ft_ssl_sha224_string.c \
ft_ssl_sha224_print.c \
ft_ssl_usage.c ft_ssl_usage.c
SRC += $(MD5_SRC) \ SRC += $(MD5_SRC) \

View file

@ -29,6 +29,7 @@ typedef struct s_flags
int quiet; int quiet;
int reverse; int reverse;
int print_stdin; int print_stdin;
int something_printed;
} t_flags; } t_flags;
typedef struct s_alorithm typedef struct s_alorithm
@ -55,19 +56,26 @@ void ft_ssl_md5_string(const char *strng,
t_ft_ssl *ft_ssl); t_ft_ssl *ft_ssl);
void ft_ssl_md5_file(const char *filename, void ft_ssl_md5_file(const char *filename,
t_ft_ssl *ft_ssl); t_ft_ssl *ft_ssl);
void ft_ssl_md5_print(unsigned char *digest, void ft_ssl_md5_print(const char *target,
t_ft_ssl *ft_ssl); unsigned char *digest,
t_ft_ssl *ft_ssl);
void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl); void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl);
void ft_ssl_sha256_string(const char *strng, void ft_ssl_sha256_string(const char *strng,
t_ft_ssl *ft_ssl); t_ft_ssl *ft_ssl);
void ft_ssl_sha256_file(const char *filename, void ft_ssl_sha256_file(const char *filename,
t_ft_ssl *ft_ssl); 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_stdin(t_ft_ssl *ft_ssl);
void ft_ssl_sha224_string(const char *strng, void ft_ssl_sha224_string(const char *strng,
t_ft_ssl *ft_ssl); t_ft_ssl *ft_ssl);
void ft_ssl_sha224_file(const char *filename, void ft_ssl_sha224_file(const char *filename,
t_ft_ssl *ft_ssl); t_ft_ssl *ft_ssl);
void ft_ssl_sha256_print(const char *target,
unsigned char *digest,
t_ft_ssl *ft_ssl);
#endif #endif

View file

@ -26,6 +26,14 @@ static t_algorithm *init_algorithms(void)
return algs; 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) static void set_algorithm(t_algorithm *alg, t_ft_ssl *ft_ssl)
{ {
ft_ssl->process_file = alg->process_file; 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; i = 0;
alg = 0; alg = 0;
init_flags(ft_ssl);
alg_walker = init_algorithms(); alg_walker = init_algorithms();
while (i < FT_SSL_ALGS_COUNT) while (i < FT_SSL_ALGS_COUNT)
{ {

View file

@ -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) while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0)
ft_md5_update(&ctx, buf, len); ft_md5_update(&ctx, buf, len);
ft_md5_final(digest, &ctx); ft_md5_final(digest, &ctx);
ft_ssl_md5_print(digest, ft_ssl); ft_ssl_md5_print(filename, digest, ft_ssl);
} }

View file

@ -2,12 +2,43 @@
#include "ft_ssl.h" #include "ft_ssl.h"
#include "libft.h" #include "libft.h"
void ft_ssl_md5_print(unsigned char *digest, static void print_name(const char *name, int reverse)
t_ft_ssl *ft_ssl) {
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]; BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE];
(void)ft_ssl;
ft_md5_digest_string(digest, digest_string); 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_putstr("\n");
ft_ssl->flags.something_printed = 1;
} }

View file

@ -20,5 +20,5 @@ void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl)
ft_md5_update(&ctx, buf, len); ft_md5_update(&ctx, buf, len);
} }
ft_md5_final(digest, &ctx); ft_md5_final(digest, &ctx);
ft_ssl_md5_print(digest, ft_ssl); ft_ssl_md5_print(NULL, digest, ft_ssl);
} }

View file

@ -8,14 +8,10 @@
void ft_ssl_md5_string(const char *str, t_ft_ssl *ft_ssl) void ft_ssl_md5_string(const char *str, t_ft_ssl *ft_ssl)
{ {
BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE];
t_md5_ctx ctx; t_md5_ctx ctx;
(void)ft_ssl;
ft_md5_init(&ctx); ft_md5_init(&ctx);
ft_md5_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); ft_md5_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str));
ft_md5_final(digest, &ctx); ft_md5_final(digest, &ctx);
ft_md5_digest_string(digest, digest_string); ft_ssl_md5_print(str, digest, ft_ssl);
ft_putstr((const char *)digest_string);
ft_putstr("\n");
} }

View file

@ -10,21 +10,18 @@ void ft_ssl_sha224_file(const char *filename, t_ft_ssl *ft_ssl)
int fd; int fd;
int len; int len;
BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE];
t_sha256_ctx ctx; t_sha256_ctx ctx;
BYTE1 buf[FT_SSL_BUFFER_SIZE]; BYTE1 buf[FT_SSL_BUFFER_SIZE];
(void)ft_ssl; (void)ft_ssl;
if ((fd = open(filename, O_RDONLY)) == -1) if ((fd = open(filename, O_RDONLY)) == -1)
{ {
perror("Error: "); perror("./ft_ssl: ");
exit(1); exit(1);
} }
ft_sha224_init(&ctx); ft_sha224_init(&ctx);
while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0) while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0)
ft_sha224_update(&ctx, buf, len); ft_sha224_update(&ctx, buf, len);
ft_sha224_final(digest, &ctx); ft_sha224_final(digest, &ctx);
ft_sha224_digest_string(digest, digest_string); ft_ssl_sha224_print(filename, digest, ft_ssl);
ft_putstr((const char *)digest_string);
ft_putstr("\n");
} }

44
src/ft_ssl_sha224_print.c Normal file
View file

@ -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;
}

View file

@ -9,16 +9,17 @@ void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl)
{ {
int len; int len;
BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE];
t_sha256_ctx ctx; t_sha256_ctx ctx;
BYTE1 buf[FT_SSL_BUFFER_SIZE]; BYTE1 buf[FT_SSL_BUFFER_SIZE];
(void)ft_ssl; (void)ft_ssl;
ft_sha224_init(&ctx); ft_sha224_init(&ctx);
while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) 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_update(&ctx, buf, len);
}
ft_sha224_final(digest, &ctx); ft_sha224_final(digest, &ctx);
ft_sha224_digest_string(digest, digest_string); ft_ssl_sha224_print(NULL, digest, ft_ssl);
ft_putstr((const char *)digest_string);
ft_putstr("\n");
} }

View file

@ -8,14 +8,11 @@
void ft_ssl_sha224_string(const char *str, t_ft_ssl *ft_ssl) void ft_ssl_sha224_string(const char *str, t_ft_ssl *ft_ssl)
{ {
BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE];
t_sha256_ctx ctx; t_sha256_ctx ctx;
(void)ft_ssl; (void)ft_ssl;
ft_sha224_init(&ctx); ft_sha224_init(&ctx);
ft_sha224_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); ft_sha224_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str));
ft_sha224_final(digest, &ctx); ft_sha224_final(digest, &ctx);
ft_sha224_digest_string(digest, digest_string); ft_ssl_sha224_print(str, digest, ft_ssl);
ft_putstr((const char *)digest_string);
ft_putstr("\n");
} }

View file

@ -10,21 +10,18 @@ void ft_ssl_sha256_file(const char *filename, t_ft_ssl *ft_ssl)
int fd; int fd;
int len; int len;
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE];
t_sha256_ctx ctx; t_sha256_ctx ctx;
BYTE1 buf[FT_SSL_BUFFER_SIZE]; BYTE1 buf[FT_SSL_BUFFER_SIZE];
(void)ft_ssl; (void)ft_ssl;
if ((fd = open(filename, O_RDONLY)) == -1) if ((fd = open(filename, O_RDONLY)) == -1)
{ {
perror("Error: "); perror("./ft_ssl: ");
exit(1); exit(1);
} }
ft_sha256_init(&ctx); ft_sha256_init(&ctx);
while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0) while ((len = read(fd, buf, FT_SSL_BUFFER_SIZE)) > 0)
ft_sha256_update(&ctx, buf, len); ft_sha256_update(&ctx, buf, len);
ft_sha256_final(digest, &ctx); ft_sha256_final(digest, &ctx);
ft_sha256_digest_string(digest, digest_string); ft_ssl_sha256_print(filename, digest, ft_ssl);
ft_putstr((const char *)digest_string);
ft_putstr("\n");
} }

44
src/ft_ssl_sha256_print.c Normal file
View file

@ -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;
}

View file

@ -9,16 +9,17 @@ void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl)
{ {
int len; int len;
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE];
t_sha256_ctx ctx; t_sha256_ctx ctx;
BYTE1 buf[FT_SSL_BUFFER_SIZE]; BYTE1 buf[FT_SSL_BUFFER_SIZE];
(void)ft_ssl; (void)ft_ssl;
ft_sha256_init(&ctx); ft_sha256_init(&ctx);
while ((len = read(0, buf, FT_SSL_BUFFER_SIZE))) 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_update(&ctx, buf, len);
}
ft_sha256_final(digest, &ctx); ft_sha256_final(digest, &ctx);
ft_sha256_digest_string(digest, digest_string); ft_ssl_sha256_print(NULL, digest, ft_ssl);
ft_putstr((const char *)digest_string);
ft_putstr("\n");
} }

View file

@ -8,14 +8,11 @@
void ft_ssl_sha256_string(const char *str, t_ft_ssl *ft_ssl) void ft_ssl_sha256_string(const char *str, t_ft_ssl *ft_ssl)
{ {
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE];
t_sha256_ctx ctx; t_sha256_ctx ctx;
(void)ft_ssl; (void)ft_ssl;
ft_sha256_init(&ctx); ft_sha256_init(&ctx);
ft_sha256_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); ft_sha256_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str));
ft_sha256_final(digest, &ctx); ft_sha256_final(digest, &ctx);
ft_sha256_digest_string(digest, digest_string); ft_ssl_sha256_print(str, digest, ft_ssl);
ft_putstr((const char *)digest_string);
ft_putstr("\n");
} }

View file

@ -3,6 +3,9 @@
void ft_ssl_usage(void) 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); exit(1);
} }

View file

@ -25,7 +25,27 @@ static int process_flags(int argc, char **argv, t_ft_ssl *ft_ssl)
} }
return 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);
}
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_ft_ssl ft_ssl; t_ft_ssl ft_ssl;
@ -35,16 +55,10 @@ int main(int argc, char **argv)
ft_ssl_usage(); ft_ssl_usage();
ft_ssl_init(argv[1], &ft_ssl); ft_ssl_init(argv[1], &ft_ssl);
i = process_flags(argc, argv, &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); ft_ssl.process_stdin(&ft_ssl);
while (i < argc) process_strings_and_files(i, argc, argv, &ft_ssl);
{ if (!ft_ssl.flags.something_printed)
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); ft_ssl.process_stdin(&ft_ssl);
return (0); return (0);
} }