partially working main

This commit is contained in:
Gregory 2018-10-22 22:33:05 +03:00
parent 9c82b1fee3
commit 50f726188d
17 changed files with 438 additions and 7 deletions

View file

@ -64,8 +64,22 @@ SHA_SRC = ft_sha256_init.c \
ft_sha224_digest_string.c ft_sha224_digest_string.c
SRC = main.c SRC = main.c \
SRC += $(MD5_SRC) 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 # project object files
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))

73
inc/ft_ssl.h Normal file
View file

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ssl.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

58
src/ft_ssl_init.c Normal file
View file

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

27
src/ft_ssl_md5_file.c Normal file
View file

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

13
src/ft_ssl_md5_print.c Normal file
View file

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

24
src/ft_ssl_md5_stdin.c Normal file
View file

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

21
src/ft_ssl_md5_string.c Normal file
View file

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

30
src/ft_ssl_sha224_file.c Normal file
View file

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

24
src/ft_ssl_sha224_stdin.c Normal file
View file

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

View file

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

30
src/ft_ssl_sha256_file.c Normal file
View file

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

24
src/ft_ssl_sha256_stdin.c Normal file
View file

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

View file

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

8
src/ft_ssl_usage.c Normal file
View file

@ -0,0 +1,8 @@
#include "ft_ssl.h"
#include "libft.h"
void ft_ssl_usage(void)
{
ft_putstr("usage: \n");
exit(1);
}

View file

@ -1,7 +1,50 @@
#include <stdio.h> #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); return (0);
} }

View file

@ -17,7 +17,7 @@ void ft_md5_update(t_md5_ctx *c, BYTE1 * message, BYTE8 message_len)
i = part_block_len; i = part_block_len;
while(i + 63 < message_len) while(i + 63 < message_len)
{ {
ft_md5_transform(c, &c->block[i]); ft_md5_transform(c, &message[i]);
i += FT_MD5_BLOCK_SIZE; i += FT_MD5_BLOCK_SIZE;
} }
curent_block_index = 0; curent_block_index = 0;

View file

@ -17,7 +17,7 @@ void ft_sha256_update(t_sha256_ctx *c, BYTE1 *message, BYTE8 message_len)
i = part_block_len; i = part_block_len;
while (i + 63 < message_len) while (i + 63 < message_len)
{ {
ft_sha256_transform(c, &c->block[i]); ft_sha256_transform(c, &message[i]);
i += FT_SHA256_BLOCK_SIZE; i += FT_SHA256_BLOCK_SIZE;
} }
curent_block_index = 0; curent_block_index = 0;