Our code ☭

This commit is contained in:
Matthias Guillitte
2025-10-29 13:13:53 +01:00
parent d0eb17f772
commit 573b5e48d2
3 changed files with 45 additions and 28 deletions

View File

@@ -0,0 +1,21 @@
package common;
public class Logger {
public static void displayInfo(String message){
final String BLUE = "\u001B[34m";
final String RESET = "\u001B[0m";
System.out.println(BLUE + "Info >>> " + message + RESET);
}
public static void displayReceived(String message){
String GREEN = "\u001B[32m";
String RESET = "\u001B[0m";
System.out.println(GREEN + "Received >>> " + message + RESET);
}
public static void displaySent(String message){
final String RED = "\u001B[31m";
final String RESET = "\u001B[0m";
System.out.println(RED + "Sent >>> " + message + RESET);
}
}

View File

@@ -0,0 +1,19 @@
package common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class SocketManager {
public static void send(Socket socket, String data) throws IOException {
OutputStream output = socket.getOutputStream();
output.write(String.format("%s\r\n", data).getBytes(StandardCharsets.UTF_8));
output.flush();
}
public static String readResponse(BufferedReader reader) throws IOException {
return reader.readLine();
}
}

View File

@@ -13,6 +13,11 @@ import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.Base64;
import static common.Logger.displayInfo;
import static common.Logger.displayReceived;
import static common.Logger.displaySent;
import static common.SocketManager.send;
import static common.SocketManager.readResponse;
public class Main {
@@ -87,34 +92,6 @@ public class Main {
}
}
public static void displayInfo(String message){
final String BLUE = "\u001B[34m";
final String RESET = "\u001B[0m";
System.out.println(BLUE + "Info >>> " + message + RESET);
}
public static void displayReceived(String message){
String GREEN = "\u001B[32m";
String RESET = "\u001B[0m";
System.out.println(GREEN + "Received >>> " + message + RESET);
}
public static void displaySent(String message){
final String RED = "\u001B[31m";
final String RESET = "\u001B[0m";
System.out.println(RED + "Sent >>> " + message + RESET);
}
public static void send(Socket socket, String data) throws IOException {
OutputStream output = socket.getOutputStream();
output.write(String.format("%s\r\n", data).getBytes(StandardCharsets.UTF_8));
output.flush();
}
public static String readResponse(BufferedReader reader) throws IOException {
return reader.readLine();
}
public static SecretKey get3DESKey() throws NoSuchAlgorithmException, NoSuchProviderException {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM, PROVIDER);
keyGen.init(new SecureRandom());