From 45a5b26691f4adcbf6038ebe5aed66a94642672d Mon Sep 17 00:00:00 2001 From: Blantheon Date: Tue, 10 Jun 2025 16:31:54 +0200 Subject: [PATCH 1/4] Finish --- C/serveur_TCP | 1 + Python/entropy_calculator/README.md | 4 +++ Python/entropy_calculator/main.py | 44 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 160000 C/serveur_TCP create mode 100644 Python/entropy_calculator/README.md create mode 100644 Python/entropy_calculator/main.py diff --git a/C/serveur_TCP b/C/serveur_TCP new file mode 160000 index 0000000..5da9fd3 --- /dev/null +++ b/C/serveur_TCP @@ -0,0 +1 @@ +Subproject commit 5da9fd350295dbadfa1207729176aade3e6c2bfc diff --git a/Python/entropy_calculator/README.md b/Python/entropy_calculator/README.md new file mode 100644 index 0000000..a95c66c --- /dev/null +++ b/Python/entropy_calculator/README.md @@ -0,0 +1,4 @@ +# Entropy calculator + +This is a little script writted a time ago by my self for calculate the entropy of a password. +There is several different method to do it so this script is based on [this article.](https://proton.me/blog/what-is-password-entropy) diff --git a/Python/entropy_calculator/main.py b/Python/entropy_calculator/main.py new file mode 100644 index 0000000..e707f5f --- /dev/null +++ b/Python/entropy_calculator/main.py @@ -0,0 +1,44 @@ +from math import log2 + +def calculate_entropy_string(password: str, range_password=0) -> int: + if not range_password: + # calculate range_password of a given password before calculate entropy + VALUES = {'lower': 26, 'upper': 26, 'int': 10, 'special' : 32} + check = {'lower': True, 'upper': True, 'int': True, 'special': True} + + + password_set = set(password) + for char in password_set: + if char.isalpha(): + if char.islower() and check['lower']: + range_password += VALUES['lower'] + check['lower'] = False + if char.isupper() and check['upper']: + range_password += VALUES['upper'] + check['upper'] = False + + elif char.isnumeric(): + if check['int']: + range_password += VALUES['int'] + check['int'] = False + + elif check['special']: + range_password += VALUES['special'] + check['special'] = False + + # expression for calculate entropy + return round(len(password) * log2(range_password), 2) + + +def calculate_entropy_diceware(password: str): + ENTROPY_BY_WORD = 12.92 + lst = password.split() + return len(lst) * ENTROPY_BY_WORD + +if __name__ == '__main__': + pwd = input('Enter you\'r password: ') + if len(pwd.split()) > 2: + entropy = calculate_entropy_diceware(pwd) + else: + entropy = calculate_entropy_string(pwd) + print(f"The password \"{pwd}\" have an entropy of {entropy}") From bc32d8f537e6393957300f47680ceb93ab7221d9 Mon Sep 17 00:00:00 2001 From: Blantheon Date: Tue, 10 Jun 2025 16:45:50 +0200 Subject: [PATCH 2/4] Revert "Finish" Error This reverts commit 45a5b26691f4adcbf6038ebe5aed66a94642672d. --- C/serveur_TCP | 1 - Python/entropy_calculator/README.md | 4 --- Python/entropy_calculator/main.py | 44 ----------------------------- 3 files changed, 49 deletions(-) delete mode 160000 C/serveur_TCP delete mode 100644 Python/entropy_calculator/README.md delete mode 100644 Python/entropy_calculator/main.py diff --git a/C/serveur_TCP b/C/serveur_TCP deleted file mode 160000 index 5da9fd3..0000000 --- a/C/serveur_TCP +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5da9fd350295dbadfa1207729176aade3e6c2bfc diff --git a/Python/entropy_calculator/README.md b/Python/entropy_calculator/README.md deleted file mode 100644 index a95c66c..0000000 --- a/Python/entropy_calculator/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Entropy calculator - -This is a little script writted a time ago by my self for calculate the entropy of a password. -There is several different method to do it so this script is based on [this article.](https://proton.me/blog/what-is-password-entropy) diff --git a/Python/entropy_calculator/main.py b/Python/entropy_calculator/main.py deleted file mode 100644 index e707f5f..0000000 --- a/Python/entropy_calculator/main.py +++ /dev/null @@ -1,44 +0,0 @@ -from math import log2 - -def calculate_entropy_string(password: str, range_password=0) -> int: - if not range_password: - # calculate range_password of a given password before calculate entropy - VALUES = {'lower': 26, 'upper': 26, 'int': 10, 'special' : 32} - check = {'lower': True, 'upper': True, 'int': True, 'special': True} - - - password_set = set(password) - for char in password_set: - if char.isalpha(): - if char.islower() and check['lower']: - range_password += VALUES['lower'] - check['lower'] = False - if char.isupper() and check['upper']: - range_password += VALUES['upper'] - check['upper'] = False - - elif char.isnumeric(): - if check['int']: - range_password += VALUES['int'] - check['int'] = False - - elif check['special']: - range_password += VALUES['special'] - check['special'] = False - - # expression for calculate entropy - return round(len(password) * log2(range_password), 2) - - -def calculate_entropy_diceware(password: str): - ENTROPY_BY_WORD = 12.92 - lst = password.split() - return len(lst) * ENTROPY_BY_WORD - -if __name__ == '__main__': - pwd = input('Enter you\'r password: ') - if len(pwd.split()) > 2: - entropy = calculate_entropy_diceware(pwd) - else: - entropy = calculate_entropy_string(pwd) - print(f"The password \"{pwd}\" have an entropy of {entropy}") From 799bbbf031f9a0cb6ecbce60b0b70792e5d63552 Mon Sep 17 00:00:00 2001 From: Blantheon Date: Tue, 10 Jun 2025 16:51:35 +0200 Subject: [PATCH 3/4] Finish --- C/serveur_TCP/Makefile | 35 ++++++++++++++ C/serveur_TCP/README.md | 8 ++++ C/serveur_TCP/include/global.h | 8 ++++ C/serveur_TCP/include/socket_client.h | 9 ++++ C/serveur_TCP/include/socket_server.h | 8 ++++ C/serveur_TCP/src/client/client.c | 25 ++++++++++ C/serveur_TCP/src/client/socket.c | 63 ++++++++++++++++++++++++ C/serveur_TCP/src/server/README.md | 69 +++++++++++++++++++++++++++ C/serveur_TCP/src/server/server.c | 31 ++++++++++++ C/serveur_TCP/src/server/socket.c | 64 +++++++++++++++++++++++++ Python/entropy_calculator/README.md | 4 ++ Python/entropy_calculator/main.py | 44 +++++++++++++++++ 12 files changed, 368 insertions(+) create mode 100644 C/serveur_TCP/Makefile create mode 100644 C/serveur_TCP/README.md create mode 100644 C/serveur_TCP/include/global.h create mode 100644 C/serveur_TCP/include/socket_client.h create mode 100644 C/serveur_TCP/include/socket_server.h create mode 100644 C/serveur_TCP/src/client/client.c create mode 100644 C/serveur_TCP/src/client/socket.c create mode 100644 C/serveur_TCP/src/server/README.md create mode 100644 C/serveur_TCP/src/server/server.c create mode 100644 C/serveur_TCP/src/server/socket.c create mode 100644 Python/entropy_calculator/README.md create mode 100644 Python/entropy_calculator/main.py diff --git a/C/serveur_TCP/Makefile b/C/serveur_TCP/Makefile new file mode 100644 index 0000000..367682b --- /dev/null +++ b/C/serveur_TCP/Makefile @@ -0,0 +1,35 @@ +CC := gcc +CFLAGS := -Wall -Wextra -Werror -g -I include +SOURCE_DIR = src +OBJ_DIR := obj +BIN_DIR := . + +SERVER_EXE := $(BIN_DIR)/server +SERVER_SRC := $(wildcard $(SOURCE_DIR)/server/*.c) +SERVER_OBJ := $(patsubst $(SOURCE_DIR)/server/%.c, \ + $(OBJ_DIR)/server/%.o, $(SERVER_SRC)) + +CLIENT_EXE := $(BIN_DIR)/client +CLIENT_SRC := $(wildcard $(SOURCE_DIR)/client/*.c) +CLIENT_OBJ := $(patsubst $(SOURCE_DIR)/client/%.c, \ + $(OBJ_DIR)/client/%.o, $(CLIENT_SRC)) + +all: $(SERVER_EXE) $(CLIENT_EXE) + +# $^ represent all prerequists and $@ represent the target +$(SERVER_EXE): $(SERVER_OBJ) + $(CC) $^ -o $@ + +$(OBJ_DIR)/server/%.o: $(SOURCE_DIR)/server/%.c + $(CC) $(CFLAGS) -c $< -o $@ + +$(CLIENT_EXE): $(CLIENT_OBJ) + $(CC) $^ -o $@ + +$(OBJ_DIR)/client/%.o: $(SOURCE_DIR)/client/%.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + @-rm $(SERVER_EXE) $(OBJ_DIR)/server/* 2> /dev/null + @-rm $(CLIENT_EXE) $(OBJ_DIR)/client/* 2> /dev/null + @echo "clean done !" diff --git a/C/serveur_TCP/README.md b/C/serveur_TCP/README.md new file mode 100644 index 0000000..5a31d37 --- /dev/null +++ b/C/serveur_TCP/README.md @@ -0,0 +1,8 @@ +# Server_TCP +A simple TCP server written in C +You can compile server and client binaries with make and launch server in a terminal and client in another +PS: Launch server before client +You can see the readme in the src/server directory a tried to explain how it work as best as I was able too + +If I made a mistake in my code or in my explanation please let me know. +I'm sorry too for my bad english I also done as best as I was able to diff --git a/C/serveur_TCP/include/global.h b/C/serveur_TCP/include/global.h new file mode 100644 index 0000000..d9bde2b --- /dev/null +++ b/C/serveur_TCP/include/global.h @@ -0,0 +1,8 @@ +#ifndef GLOBAL_H +#define GLOBAL_H + +#define IP INADDR_LOOPBACK +#define PORT 3000 +#define SIZE 1024 + +#endif diff --git a/C/serveur_TCP/include/socket_client.h b/C/serveur_TCP/include/socket_client.h new file mode 100644 index 0000000..23fb899 --- /dev/null +++ b/C/serveur_TCP/include/socket_client.h @@ -0,0 +1,9 @@ +#ifndef SOCKET_CLIENT_H +#define SOCKET_CLIENT_H + +struct sockaddr_in init_socket(int *fd); +struct sockaddr_in init_addr(void); +void connect_socket(int fd, struct sockaddr_in *addr); +void manage_socket(int fd); + +#endif diff --git a/C/serveur_TCP/include/socket_server.h b/C/serveur_TCP/include/socket_server.h new file mode 100644 index 0000000..1392e04 --- /dev/null +++ b/C/serveur_TCP/include/socket_server.h @@ -0,0 +1,8 @@ +#ifndef SOCKET_SERVER_H +#define SOCKET_SERVER_H + +void init_socket(int *fd); +void bind_s(int *fd, struct sockaddr_in *addr); +void manage_socket(int clientfd, struct sockaddr_in *client); + +#endif diff --git a/C/serveur_TCP/src/client/client.c b/C/serveur_TCP/src/client/client.c new file mode 100644 index 0000000..52a0360 --- /dev/null +++ b/C/serveur_TCP/src/client/client.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +#include +#include "socket_client.h" +#include "global.h" + +int main(void) +{ + int fd; + struct sockaddr_in addr; + + while (1) + { + addr = init_socket(&fd); + connect_socket(fd, &addr); + manage_socket(fd); + printf("Do you want to try another connection ? (y/n): "); + if (fgetc(stdin) == 'n') + break ; + fgetc(stdin); + } +} diff --git a/C/serveur_TCP/src/client/socket.c b/C/serveur_TCP/src/client/socket.c new file mode 100644 index 0000000..10e9378 --- /dev/null +++ b/C/serveur_TCP/src/client/socket.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include "socket_client.h" +#include "global.h" + +struct sockaddr_in init_socket(int *fd) +{ + *fd = socket(AF_INET, SOCK_STREAM, 0); + if (*fd == -1) + { + printf("Error in creation of the socket.\n"); + exit(EXIT_FAILURE); + } + return (init_addr()); +} + +struct sockaddr_in init_addr(void) +{ + struct sockaddr_in addr; + + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT); + addr.sin_addr.s_addr = htonl(IP); + return (addr); +} + +void connect_socket(int fd, struct sockaddr_in *addr) +{ + if (connect(fd, (struct sockaddr *)addr, sizeof(*addr)) < 0) + { + printf("Error in connection to the socket.\n"); + exit(EXIT_FAILURE); + } + system("clear"); + printf("You are connected to the server %s !\n",\ + inet_ntoa(addr->sin_addr)); +} + +void manage_socket(int fd) +{ + char serveur[SIZE]; + char message[SIZE]; + + while (1) + { + memset(serveur, 0, SIZE); + memset(message, 0, SIZE); + read(fd, serveur, SIZE); + printf("Serveur: %s\n", serveur); + printf("Message or \"quit\" command: "); + fgets(message, SIZE, stdin); + if (strcmp(message, "quit\n") == 0) + break ; + write(fd, message, sizeof(message)); + } + printf("The connection has been closed.\n"); + close(fd); +} diff --git a/C/serveur_TCP/src/server/README.md b/C/serveur_TCP/src/server/README.md new file mode 100644 index 0000000..f402753 --- /dev/null +++ b/C/serveur_TCP/src/server/README.md @@ -0,0 +1,69 @@ +# Description + +This directory correspond of all sources files making the TCP server + +# How it work + +In Unix world all is considered like a file, a real file, you'r hard drive (sda), special files (like /dev/null), all is a file. (for more precisions you can read [this post.](https://dev.to/eteimz/everything-is-a-file-explained-g2a) +So a socket too is like a file and we can interact with it like any other file, using [File descriptors.](https://en.wikipedia.org/wiki/File_descriptor) + +## Init socket + +The first step is to create the file descriptor for the socket (note that we use a pointer because it's his identifier and we want to keep it for read/write/close) +``` +*fd = socket(\ // socket is the function in C to create the file descriptor + AF_INET,\ // AF_INET represent the address family (here IPv4) + SOCK_STREAM,\ // SOCK_STREAM represent the type (TCP or UDP here it's TCP) + 0); // represent the protocol 0 is used because only protocol is possible with SOCK_STREAM +``` +now fd contain a file descriptor to the socket, that's how the socket will be identified by the kernel for our operations. + +We also create the address where the server will listen +``` +struct sockaddr_in addr; + +addr.sin_family = AF_INET; // We specify the address family, the same of the socket +addr.sin_port = htons(PORT); // We use htons to set the bytes of the port in the good order +addr.sin_addr.s_addr = htonl(IP); // htonl is the same as htons but return a long instead of a short +``` +see [big_endian](https://en.wikipedia.org/wiki/Endianness) to understand "the good order" of htons + +## Bind the socket +Now that we have our socket and our address we want to bind the socket and the address it's going to assign the address to the socket +``` +bind(*fd,\ // our file descriptor + (struct sockaddr *)addr,\ // we cast our addr to (struct sockaddr *) because this is what the function want + sizeof(*addr)) // we give the size in byte of our address +``` + +## Listen +Here we have a file descriptor that point to a socket binded with our local address, all is done. +So we can start at listening incoming connections (this is the job of the kernel) +``` +listen(*fd\ // our file descriptor + , 5) // the maximum connection in the queue +``` + +# Comunnicate in the socket + +## Accept connection +Now we can just accept all connections incoming in loop +``` +while (1) + { + memset(&client, 0, len); // we fill all the client's structure with 0 before accept a new one + + clientfd = accept(\ // the accept function return a new file descriptor that point sepcifically to the connection with our client + fd,\ // our socket's file descriptor + (struct sockaddr *)&client,\ client will be filled with all client's info like his IP, etc + &len); // the len of our client's variable (sizeof(client)) + + manage_socket(clientfd, &client); // once we accepted the connection we can enter in a loop to discuss with the client + } +``` + +## Manage socket +This is a simple function where the server and the client talk each in turn, we read/write in the socket with the file descriptor like in a regular file. + + +Thanks to read this if I made a mistake please let me know diff --git a/C/serveur_TCP/src/server/server.c b/C/serveur_TCP/src/server/server.c new file mode 100644 index 0000000..3957b0a --- /dev/null +++ b/C/serveur_TCP/src/server/server.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +#include "socket_server.h" +#include "global.h" + +int main(void) +{ + int fd; + int clientfd; + socklen_t len; + struct sockaddr_in client; + + init_socket(&fd); + len = sizeof(client); + while (1) + { + memset(&client, 0, len); + clientfd = accept(fd, (struct sockaddr *)&client, &len); + if (clientfd == -1) + printf("Accept failed !\n"); + manage_socket(clientfd, &client); + printf("Wait another connection ? (y/n) "); + if (fgetc(stdin) == 'n') + break ; + fgetc(stdin); + } +} diff --git a/C/serveur_TCP/src/server/socket.c b/C/serveur_TCP/src/server/socket.c new file mode 100644 index 0000000..fb47e4b --- /dev/null +++ b/C/serveur_TCP/src/server/socket.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "socket_server.h" +#include "global.h" + +void init_socket(int *fd) +{ + struct sockaddr_in addr; + + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT); + addr.sin_addr.s_addr = htonl(IP); + *fd = socket(AF_INET, SOCK_STREAM, 0); + bind_s(fd, &addr); +} + +void bind_s(int *fd, struct sockaddr_in *addr) +{ + if (bind(*fd, (struct sockaddr *)addr, sizeof(*addr)) == -1) + { + printf("Error in bind_s function during bind set-up\n"); + printf("errno code: %d\n", errno); + printf("errno text: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + printf("Socket succesfully binded.\n"); + if (listen(*fd, 5) == -1) + { + printf("Error un bind_s function during listen set-up\n"); + exit(EXIT_FAILURE); + } + printf("Socket is listening.\n"); +} + +void manage_socket(int clientfd, struct sockaddr_in *client) +{ + char buffer[SIZE]; + char response[SIZE]; + + system("clear"); + printf("---New Socket Created !---\n"); + printf("You are in communication with %s\n",\ + inet_ntoa(client->sin_addr)); + write(clientfd, "Hey, I listen you !\n", 20); + while (1) + { + memset(buffer, 0, SIZE); + memset(response, 0, SIZE); + if (read(clientfd, buffer, SIZE) <= 0) + break ; + printf("Client: %s\n", buffer); + printf("Response: "); + fgets(response, SIZE, stdin); + write(clientfd, response, strlen(response)); + } + printf("The client closed the session.\n"); + close(clientfd); +} diff --git a/Python/entropy_calculator/README.md b/Python/entropy_calculator/README.md new file mode 100644 index 0000000..a95c66c --- /dev/null +++ b/Python/entropy_calculator/README.md @@ -0,0 +1,4 @@ +# Entropy calculator + +This is a little script writted a time ago by my self for calculate the entropy of a password. +There is several different method to do it so this script is based on [this article.](https://proton.me/blog/what-is-password-entropy) diff --git a/Python/entropy_calculator/main.py b/Python/entropy_calculator/main.py new file mode 100644 index 0000000..e707f5f --- /dev/null +++ b/Python/entropy_calculator/main.py @@ -0,0 +1,44 @@ +from math import log2 + +def calculate_entropy_string(password: str, range_password=0) -> int: + if not range_password: + # calculate range_password of a given password before calculate entropy + VALUES = {'lower': 26, 'upper': 26, 'int': 10, 'special' : 32} + check = {'lower': True, 'upper': True, 'int': True, 'special': True} + + + password_set = set(password) + for char in password_set: + if char.isalpha(): + if char.islower() and check['lower']: + range_password += VALUES['lower'] + check['lower'] = False + if char.isupper() and check['upper']: + range_password += VALUES['upper'] + check['upper'] = False + + elif char.isnumeric(): + if check['int']: + range_password += VALUES['int'] + check['int'] = False + + elif check['special']: + range_password += VALUES['special'] + check['special'] = False + + # expression for calculate entropy + return round(len(password) * log2(range_password), 2) + + +def calculate_entropy_diceware(password: str): + ENTROPY_BY_WORD = 12.92 + lst = password.split() + return len(lst) * ENTROPY_BY_WORD + +if __name__ == '__main__': + pwd = input('Enter you\'r password: ') + if len(pwd.split()) > 2: + entropy = calculate_entropy_diceware(pwd) + else: + entropy = calculate_entropy_string(pwd) + print(f"The password \"{pwd}\" have an entropy of {entropy}") From 87462deb7820e34fee8539a99dbb1b584c4c836b Mon Sep 17 00:00:00 2001 From: Blantheon Date: Tue, 10 Jun 2025 16:55:05 +0200 Subject: [PATCH 4/4] Update README.md --- C/serveur_TCP/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/C/serveur_TCP/README.md b/C/serveur_TCP/README.md index 5a31d37..2c5f6ed 100644 --- a/C/serveur_TCP/README.md +++ b/C/serveur_TCP/README.md @@ -1,8 +1,12 @@ # Server_TCP A simple TCP server written in C + You can compile server and client binaries with make and launch server in a terminal and client in another + PS: Launch server before client + You can see the readme in the src/server directory a tried to explain how it work as best as I was able too If I made a mistake in my code or in my explanation please let me know. + I'm sorry too for my bad english I also done as best as I was able to