change dispatch table, md5 done

This commit is contained in:
Gregory 2019-02-02 22:28:23 +02:00
parent d6826a38c7
commit 21d1eb9616
11 changed files with 246 additions and 212 deletions

View file

@ -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):

View file

@ -15,6 +15,7 @@
# include <stdint.h>
# 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(
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_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);
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

View file

@ -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,

View file

@ -1,80 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ssl_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

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

View file

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

90
src/md5/ft_md5.c Normal file
View file

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_md5.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

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

View file

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

View file

@ -10,27 +10,26 @@
/* */
/* ************************************************************************** */
#include "ft_ssl.h"
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#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);
}

View file

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