From 0a7cdba6521894d34b65f4b1bcdb92b889f7e9b2 Mon Sep 17 00:00:00 2001 From: EndMove Date: Sat, 12 Mar 2022 14:41:33 +0100 Subject: [PATCH] =?UTF-8?q?Syst=C3=A8me=20de=20hash=20correction=20de=20bu?= =?UTF-8?q?gs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domains/client/ClientHandler.java | 4 - .../protocol/rules/reader/FilelistRule.java | 2 - .../protocol/rules/reader/HelloRule.java | 4 - .../protocol/rules/reader/SavefileRule.java | 5 +- .../protocol/rules/reader/SendOkRule.java | 2 - .../protocol/rules/reader/SigninRule.java | 4 - .../protocol/rules/reader/SignoutRule.java | 1 - .../protocol/rules/writer/SendfileRule.java | 1 - .../java/lightcontainer/utils/AES_GCM.java | 21 +++-- .../java/lightcontainer/utils/FileSender.java | 8 +- .../main/java/lightcontainer/utils/SHA.java | 91 +++++++++++++++++++ .../java/lightcontainer/utils/ShaHasher.java | 5 +- 12 files changed, 113 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/lightcontainer/utils/SHA.java diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 6600704..48059b6 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -134,7 +134,6 @@ public class ClientHandler implements Runnable, AutoCloseable { repository.disconnect(this); break; } - } 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 */ @@ -224,8 +222,6 @@ public class ClientHandler implements Runnable, AutoCloseable { } } - - /** * AutoClosable Function * Close the Client thread and resources. diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java index 4a755ed..920b6a9 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java @@ -3,9 +3,7 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; -import lightcontainer.protocol.ProtocolWriter; 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 diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java index 2440a97..b2b797d 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java @@ -3,10 +3,6 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; 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 */ diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index 05a40e6..996dd81 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -4,14 +4,12 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileErrorRule; -import lightcontainer.protocol.rules.writer.SaveFileOkRule; import lightcontainer.protocol.rules.writer.SendfileRule; import lightcontainer.utils.AES_GCM; import lightcontainer.utils.FileReceiver; import java.io.IOException; import java.io.InputStream; -import java.security.NoSuchAlgorithmException; /** * 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)) throw new IOException(); - System.out.println("AHAHAHAH"); - this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), "EMPREINTEBLBLBLBLBLABLABLBALBALBALBALBALBALBALBALBALABLBALBALBALABLABLABLABLABLABLABALBLABALABLABLABLABKJABKAHBHKBHJbhjvgkh"), ResultCmdReceiver.STOREBACKEND); + this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), ""), ResultCmdReceiver.STOREBACKEND); } catch (IOException | AES_GCM.AesGcmException e) { this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); e.printStackTrace(); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index 0d18a55..54f21a5 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -3,7 +3,6 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; -import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.rules.writer.SaveFileOkRule; /** @@ -11,7 +10,6 @@ import lightcontainer.protocol.rules.writer.SaveFileOkRule; */ public class SendOkRule extends ProtocolReader { - // Constants private static final String PATTERN = "^SEND_OK\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java index a2282d9..6f697b5 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java @@ -3,13 +3,9 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; 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.SignOkRule; -import java.io.InputStream; - /** * Règle permettant de gérer la connection d'un utilisateur */ diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java index ee6936a..2b7a39f 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java @@ -2,7 +2,6 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolReader; -import lightcontainer.protocol.ProtocolWriter; /** * Règle demandant la déconnexion du client diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index 2a7d97e..850c600 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -2,7 +2,6 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolWriter; -import lightcontainer.utils.AES_GCM; import lightcontainer.utils.FileSender; import java.io.OutputStream; diff --git a/app/src/main/java/lightcontainer/utils/AES_GCM.java b/app/src/main/java/lightcontainer/utils/AES_GCM.java index 2fff44b..c7b2dae 100644 --- a/app/src/main/java/lightcontainer/utils/AES_GCM.java +++ b/app/src/main/java/lightcontainer/utils/AES_GCM.java @@ -22,10 +22,12 @@ public class AES_GCM { public static final int GCM_IV_LENGTH = 16; public static final int GCM_TAG_LENGTH = 16; + // Main method for testing public static void main(String[] args) throws Exception { /* * FILE ENCRYPTION DEMO + */ // Init files File inFile = new File("D:\\HELMo.png"); File outFile = new File("D:\\HELMoCrypted.png"); @@ -42,6 +44,7 @@ public class AES_GCM { encryptStream( new FileInputStream(inFile), new FileOutputStream(outFile), + (int)inFile.length(), IVFile, keyFile ); @@ -49,10 +52,10 @@ public class AES_GCM { decryptStream( new FileInputStream(outFile), new FileOutputStream(clearFile), + (int)outFile.length(), IVFile, keyFile ); - */ /* * TEXT ENCRYPTION DEMO @@ -202,6 +205,7 @@ public class AES_GCM { * * @param in InputStream to the input, flux to encrypt. * @param out OutputStream to the output, encrypted flux. + * @param fileSize Stream/file size. * @param key Base64 encoded secret key. * @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 { byte[] buffer = new byte[1024]; + int currentSize = 0; int bytes; - int currSize = 0; try { // Make the cipher for encryption Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, key, IV); // Initialize a CipherOutputStream CipherOutputStream cipherOut = new CipherOutputStream(out, cipher); // Encryption Process - while(currSize < fileSize && (bytes = in.read(buffer)) > 0) { - currSize += bytes; + while((currentSize < fileSize) && (bytes = in.read(buffer)) > 0) { cipherOut.write(buffer, 0, bytes); cipherOut.flush(); + currentSize += bytes; } // Close CipherOutputStream cipherOut.close(); @@ -256,13 +260,15 @@ public class AES_GCM { * * @param in InputStream to the input, flux to decrypt. * @param out OutputStream to the output, decrypted flux. + * @param fileSize Stream/file size. * @param key Base64 encoded secret key. * @param IV Base64 encoded vector. * * @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]; + int currentSize = 0; int bytes; try { // Make the cipher for decryption @@ -270,9 +276,10 @@ public class AES_GCM { // Initialize a CipherOutputStream CipherInputStream cipherIn = new CipherInputStream(in, cipher); // Encryption Process - while((bytes = cipherIn.read(buffer)) > 0) { + while((currentSize < fileSize) && (bytes = cipherIn.read(buffer)) > 0) { out.write(buffer, 0, bytes); out.flush(); + currentSize += bytes; } // Close CipherOutputStream cipherIn.close(); @@ -285,7 +292,9 @@ public class AES_GCM { * Internal Error from AES_GCM encryption Class */ public static class AesGcmException extends Exception { + // Constant private static final long serialVersionUID = -145972354893514657L; + /** * Constructor of AesGcmException, * which define it's own detail message. diff --git a/app/src/main/java/lightcontainer/utils/FileSender.java b/app/src/main/java/lightcontainer/utils/FileSender.java index 9ed006c..bc58fc5 100644 --- a/app/src/main/java/lightcontainer/utils/FileSender.java +++ b/app/src/main/java/lightcontainer/utils/FileSender.java @@ -4,19 +4,19 @@ import java.io.*; public class FileSender { private static final int DEFAULT_BUFFER=8000; - private String path; + private final String path; public FileSender(String path) { this.path = path; } 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); try { File f = new File(String.format("%s/%s", path, filename)); if(f.exists()) { bisFile = new BufferedInputStream(new FileInputStream(f)); - AES_GCM.decryptStream(bisFile, out, aesKey, iv); + AES_GCM.decryptStream(bisFile, out, fileSize, aesKey, iv); bisFile.close(); return true; @@ -29,7 +29,7 @@ public class FileSender { } public boolean sendFile(String filename, OutputStream out) { - BufferedInputStream bisFile = null; + BufferedInputStream bisFile; int bytesReaded = 0; try { diff --git a/app/src/main/java/lightcontainer/utils/SHA.java b/app/src/main/java/lightcontainer/utils/SHA.java new file mode 100644 index 0000000..d942c61 --- /dev/null +++ b/app/src/main/java/lightcontainer/utils/SHA.java @@ -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 + */ +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); + } + } + +} diff --git a/app/src/main/java/lightcontainer/utils/ShaHasher.java b/app/src/main/java/lightcontainer/utils/ShaHasher.java index 0c99234..dc35a56 100644 --- a/app/src/main/java/lightcontainer/utils/ShaHasher.java +++ b/app/src/main/java/lightcontainer/utils/ShaHasher.java @@ -10,7 +10,6 @@ import java.util.Base64; * Permet de hasher du texte */ public class ShaHasher { - /** * Mot de passe non-hashé */ @@ -45,8 +44,8 @@ public class ShaHasher { md.update(passwordSalt); byte[] bytes = md.digest(password.getBytes(StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); - for (int i = 0; i < bytes.length; i++) { - sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); + for (byte aByte : bytes) { + sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1)); } generatedPassword = sb.toString(); } catch (NoSuchAlgorithmException e) {