Système de hash correction de bugs

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-12 14:41:33 +01:00
parent 7d55699f7b
commit 0a7cdba652
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
12 changed files with 113 additions and 35 deletions

View File

@ -134,7 +134,6 @@ public class ClientHandler implements Runnable, AutoCloseable {
repository.disconnect(this); repository.disconnect(this);
break; break;
} }
} }
try { try {
@ -201,7 +200,6 @@ public class ClientHandler implements Runnable, AutoCloseable {
} }
} }
/** /**
* Permet au Client d'attendre la fin de la réalisation de sa tâche * Permet au Client d'attendre la fin de la réalisation de sa tâche
*/ */
@ -224,8 +222,6 @@ public class ClientHandler implements Runnable, AutoCloseable {
} }
} }
/** /**
* AutoClosable Function * AutoClosable Function
* Close the Client thread and resources. * Close the Client thread and resources.

View File

@ -3,9 +3,7 @@ package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context; import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.protocol.rules.writer.FilesRule; import lightcontainer.protocol.rules.writer.FilesRule;
import lightcontainer.protocol.rules.writer.SignOkRule;
/** /**
* Règle permettant de récupérer la liste des fichiers d'un utilisateur * Règle permettant de récupérer la liste des fichiers d'un utilisateur

View File

@ -3,10 +3,6 @@ package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context; import lightcontainer.domains.client.Context;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
/** /**
* Règle permettant d'être alerter de l'annoncement d'un SBE * Règle permettant d'être alerter de l'annoncement d'un SBE
*/ */

View File

@ -4,14 +4,12 @@ import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.SaveFileErrorRule; import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
import lightcontainer.protocol.rules.writer.SaveFileOkRule;
import lightcontainer.protocol.rules.writer.SendfileRule; import lightcontainer.protocol.rules.writer.SendfileRule;
import lightcontainer.utils.AES_GCM; import lightcontainer.utils.AES_GCM;
import lightcontainer.utils.FileReceiver; import lightcontainer.utils.FileReceiver;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
/** /**
* Règle permettant de sauvegarder un fichier sur le SBE. * Règle permettant de sauvegarder un fichier sur le SBE.
@ -62,8 +60,7 @@ public class SavefileRule extends ProtocolReader {
if (!fileReceiver.receiveFile(reader, this.filename, this.size, key, iv)) if (!fileReceiver.receiveFile(reader, this.filename, this.size, key, iv))
throw new IOException(); throw new IOException();
System.out.println("AHAHAHAH"); this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), ""), ResultCmdReceiver.STOREBACKEND);
this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), "EMPREINTEBLBLBLBLBLABLABLBALBALBALBALBALBALBALBALBALABLBALBALBALABLABLABLABLABLABLABALBLABALABLABLABLABKJABKAHBHKBHJbhjvgkh"), ResultCmdReceiver.STOREBACKEND);
} catch (IOException | AES_GCM.AesGcmException e) { } catch (IOException | AES_GCM.AesGcmException e) {
this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
e.printStackTrace(); e.printStackTrace();

View File

@ -3,7 +3,6 @@ package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context; import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.protocol.rules.writer.SaveFileOkRule; import lightcontainer.protocol.rules.writer.SaveFileOkRule;
/** /**
@ -11,7 +10,6 @@ import lightcontainer.protocol.rules.writer.SaveFileOkRule;
*/ */
public class SendOkRule extends ProtocolReader { public class SendOkRule extends ProtocolReader {
// Constants // Constants
private static final String PATTERN = "^SEND_OK\r\n$"; private static final String PATTERN = "^SEND_OK\r\n$";

View File

@ -3,13 +3,9 @@ package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context; import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
import lightcontainer.protocol.rules.writer.SignErrorRule; import lightcontainer.protocol.rules.writer.SignErrorRule;
import lightcontainer.protocol.rules.writer.SignOkRule; import lightcontainer.protocol.rules.writer.SignOkRule;
import java.io.InputStream;
/** /**
* Règle permettant de gérer la connection d'un utilisateur * Règle permettant de gérer la connection d'un utilisateur
*/ */

View File

@ -2,7 +2,6 @@ package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context; import lightcontainer.domains.client.Context;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
/** /**
* Règle demandant la déconnexion du client * Règle demandant la déconnexion du client

View File

@ -2,7 +2,6 @@ package lightcontainer.protocol.rules.writer;
import lightcontainer.domains.client.Context; import lightcontainer.domains.client.Context;
import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.utils.AES_GCM;
import lightcontainer.utils.FileSender; import lightcontainer.utils.FileSender;
import java.io.OutputStream; import java.io.OutputStream;

View File

@ -22,10 +22,12 @@ public class AES_GCM {
public static final int GCM_IV_LENGTH = 16; public static final int GCM_IV_LENGTH = 16;
public static final int GCM_TAG_LENGTH = 16; public static final int GCM_TAG_LENGTH = 16;
// Main method for testing
public static void main(String[] args) throws Exception public static void main(String[] args) throws Exception
{ {
/* /*
* FILE ENCRYPTION DEMO * FILE ENCRYPTION DEMO
*/
// Init files // Init files
File inFile = new File("D:\\HELMo.png"); File inFile = new File("D:\\HELMo.png");
File outFile = new File("D:\\HELMoCrypted.png"); File outFile = new File("D:\\HELMoCrypted.png");
@ -42,6 +44,7 @@ public class AES_GCM {
encryptStream( encryptStream(
new FileInputStream(inFile), new FileInputStream(inFile),
new FileOutputStream(outFile), new FileOutputStream(outFile),
(int)inFile.length(),
IVFile, IVFile,
keyFile keyFile
); );
@ -49,10 +52,10 @@ public class AES_GCM {
decryptStream( decryptStream(
new FileInputStream(outFile), new FileInputStream(outFile),
new FileOutputStream(clearFile), new FileOutputStream(clearFile),
(int)outFile.length(),
IVFile, IVFile,
keyFile keyFile
); );
*/
/* /*
* TEXT ENCRYPTION DEMO * TEXT ENCRYPTION DEMO
@ -202,6 +205,7 @@ public class AES_GCM {
* *
* @param in InputStream to the input, flux to encrypt. * @param in InputStream to the input, flux to encrypt.
* @param out OutputStream to the output, encrypted flux. * @param out OutputStream to the output, encrypted flux.
* @param fileSize Stream/file size.
* @param key Base64 encoded secret key. * @param key Base64 encoded secret key.
* @param IV Base64 encoded vector. * @param IV Base64 encoded vector.
* *
@ -209,18 +213,18 @@ public class AES_GCM {
*/ */
public static void encryptStream(InputStream in, OutputStream out, int fileSize, String key, String IV) throws AesGcmException { public static void encryptStream(InputStream in, OutputStream out, int fileSize, String key, String IV) throws AesGcmException {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int currentSize = 0;
int bytes; int bytes;
int currSize = 0;
try { try {
// Make the cipher for encryption // Make the cipher for encryption
Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, key, IV); Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, key, IV);
// Initialize a CipherOutputStream // Initialize a CipherOutputStream
CipherOutputStream cipherOut = new CipherOutputStream(out, cipher); CipherOutputStream cipherOut = new CipherOutputStream(out, cipher);
// Encryption Process // Encryption Process
while(currSize < fileSize && (bytes = in.read(buffer)) > 0) { while((currentSize < fileSize) && (bytes = in.read(buffer)) > 0) {
currSize += bytes;
cipherOut.write(buffer, 0, bytes); cipherOut.write(buffer, 0, bytes);
cipherOut.flush(); cipherOut.flush();
currentSize += bytes;
} }
// Close CipherOutputStream // Close CipherOutputStream
cipherOut.close(); cipherOut.close();
@ -256,13 +260,15 @@ public class AES_GCM {
* *
* @param in InputStream to the input, flux to decrypt. * @param in InputStream to the input, flux to decrypt.
* @param out OutputStream to the output, decrypted flux. * @param out OutputStream to the output, decrypted flux.
* @param fileSize Stream/file size.
* @param key Base64 encoded secret key. * @param key Base64 encoded secret key.
* @param IV Base64 encoded vector. * @param IV Base64 encoded vector.
* *
* @throws AesGcmException Exception if an error occur. * @throws AesGcmException Exception if an error occur.
*/ */
public static void decryptStream(InputStream in, OutputStream out, String key, String IV) throws AesGcmException { public static void decryptStream(InputStream in, OutputStream out, int fileSize, String key, String IV) throws AesGcmException {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int currentSize = 0;
int bytes; int bytes;
try { try {
// Make the cipher for decryption // Make the cipher for decryption
@ -270,9 +276,10 @@ public class AES_GCM {
// Initialize a CipherOutputStream // Initialize a CipherOutputStream
CipherInputStream cipherIn = new CipherInputStream(in, cipher); CipherInputStream cipherIn = new CipherInputStream(in, cipher);
// Encryption Process // Encryption Process
while((bytes = cipherIn.read(buffer)) > 0) { while((currentSize < fileSize) && (bytes = cipherIn.read(buffer)) > 0) {
out.write(buffer, 0, bytes); out.write(buffer, 0, bytes);
out.flush(); out.flush();
currentSize += bytes;
} }
// Close CipherOutputStream // Close CipherOutputStream
cipherIn.close(); cipherIn.close();
@ -285,7 +292,9 @@ public class AES_GCM {
* Internal Error from AES_GCM encryption Class * Internal Error from AES_GCM encryption Class
*/ */
public static class AesGcmException extends Exception { public static class AesGcmException extends Exception {
// Constant
private static final long serialVersionUID = -145972354893514657L; private static final long serialVersionUID = -145972354893514657L;
/** /**
* Constructor of AesGcmException, * Constructor of AesGcmException,
* which define it's own detail message. * which define it's own detail message.

View File

@ -4,19 +4,19 @@ import java.io.*;
public class FileSender { public class FileSender {
private static final int DEFAULT_BUFFER=8000; private static final int DEFAULT_BUFFER=8000;
private String path; private final String path;
public FileSender(String path) { this.path = path; } public FileSender(String path) { this.path = path; }
public boolean sendFile(String filename, OutputStream out, int fileSize, String aesKey, String iv) { public boolean sendFile(String filename, OutputStream out, int fileSize, String aesKey, String iv) {
BufferedInputStream bisFile = null; BufferedInputStream bisFile;
System.out.printf("Envoie fichier : %s - %s - %s \n", filename, aesKey, iv); System.out.printf("Envoie fichier : %s - %s - %s \n", filename, aesKey, iv);
try { try {
File f = new File(String.format("%s/%s", path, filename)); File f = new File(String.format("%s/%s", path, filename));
if(f.exists()) { if(f.exists()) {
bisFile = new BufferedInputStream(new FileInputStream(f)); bisFile = new BufferedInputStream(new FileInputStream(f));
AES_GCM.decryptStream(bisFile, out, aesKey, iv); AES_GCM.decryptStream(bisFile, out, fileSize, aesKey, iv);
bisFile.close(); bisFile.close();
return true; return true;
@ -29,7 +29,7 @@ public class FileSender {
} }
public boolean sendFile(String filename, OutputStream out) { public boolean sendFile(String filename, OutputStream out) {
BufferedInputStream bisFile = null; BufferedInputStream bisFile;
int bytesReaded = 0; int bytesReaded = 0;
try { try {

View File

@ -0,0 +1,91 @@
package lightcontainer.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.Base64;
/**
* SHA 256 Hashing and borrowing Class [DO NOT EDIT]
*
* @since 1.0
* @version 1.0
*
* @author Jérémi Nihart <contact@endmove.eu>
*/
public class SHA {
// Constants
public static final String SHA_VERSION = "SHA-256";
// Main method for testing
public static void main(String[] args) throws Exception {
/*
* BORROWING ENCRYPTION DEMO
*/
File inFile = new File("D:\\HELMo.txt");
System.out.println(hashStream(
new FileInputStream(inFile),
(int)inFile.length()
));
}
/**
* Make a borrowing of the stream.
*
* @param in InputStream to the input, flux to hash.
* @param fileSize Stream/file size.
*
* @return Borrowing of the full current flux.
*
* @throws ShaException if an error occur.
*/
public static String hashStream(InputStream in, int fileSize) throws ShaException {
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[1024];
int currentSize = 0;
int bytes;
try {
// Init Digest algo
MessageDigest digest = MessageDigest.getInstance(SHA_VERSION);
// Process flux
while ((currentSize < fileSize) && (bytes=in.read(buffer)) > 0) {
digest.update(buffer, 0, bytes);
currentSize += bytes;
}
// Make hash result
byte[] hashBytes = digest.digest();
// Convert result and make the final hash formatting
for (byte hashByte : hashBytes) {
sb.append(String.format("%02x", hashByte));
}
} catch (Exception e) {
throw new ShaException(e);
}
return sb.toString();
}
/**
* Internal Error from SHA encryption Class
*/
public static class ShaException extends Exception {
// Constant
private static final long serialVersionUID = -145979547823516845L;
/**
* Constructor of ShaException,
* which propagates the error triggering
* a crash of the hash system.
*
* @param e Previous exception throwable.
*/
public ShaException(Throwable e) {
super(e);
}
}
}

View File

@ -10,7 +10,6 @@ import java.util.Base64;
* Permet de hasher du texte * Permet de hasher du texte
*/ */
public class ShaHasher { public class ShaHasher {
/** /**
* Mot de passe non-hashé * Mot de passe non-hashé
*/ */
@ -45,8 +44,8 @@ public class ShaHasher {
md.update(passwordSalt); md.update(passwordSalt);
byte[] bytes = md.digest(password.getBytes(StandardCharsets.UTF_8)); byte[] bytes = md.digest(password.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) { for (byte aByte : bytes) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
} }
generatedPassword = sb.toString(); generatedPassword = sb.toString();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {