From 46cbcaf705e90e5426deaf0e577312125873c26a Mon Sep 17 00:00:00 2001 From: EndMove Date: Sat, 12 Mar 2022 15:32:46 +0100 Subject: [PATCH] Ajout du hashing --- .../protocol/rules/reader/SavefileRule.java | 11 ++++---- .../lightcontainer/storage/AppConfig.java | 3 +- .../lightcontainer/utils/FileReceiver.java | 2 -- .../main/java/lightcontainer/utils/SHA.java | 24 ++++++++++++++++ .../java/lightcontainer/utils/ShaHasher.java | 2 -- app/src/main/resources/appdata.json | 28 +------------------ 6 files changed, 32 insertions(+), 38 deletions(-) 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 87c7672..82aae71 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -8,6 +8,7 @@ import lightcontainer.protocol.rules.writer.SaveFileOkRule; import lightcontainer.protocol.rules.writer.SendfileRule; import lightcontainer.utils.AES_GCM; import lightcontainer.utils.FileReceiver; +import lightcontainer.utils.SHA; import lightcontainer.utils.ShaHasher; import java.io.IOException; @@ -65,20 +66,22 @@ public class SavefileRule extends ProtocolReader { this.filename = hasher.nextHashing(); String fileNameSalt = hasher.getSalt(); - String key = AES_GCM.generateSecretKey(); + String key = getContext().getAesKey(); String iv = AES_GCM.generateIV(); if (!fileReceiver.receiveFile(reader, this.filename, this.size, key, iv)) throw new IOException(); + String fileHash = SHA.hashFile(storagePath, this.filename); + // On met les données de la requête actuelle getContext().putDataString("fileName", filename); getContext().putDataInt("size", size); getContext().putDataString("iv", iv); getContext().putDataString("fileNameSalt", fileNameSalt); - this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), "EMPREINTEBLBLBLBLBLABLABLBALBALBALBALBALBALBALBALBALABLBALBALBALABLABLABLABLABLABLABALBLABALABLABLABLABKJABKAHBHKBHJbhjvgkh"), ResultCmdReceiver.STOREBACKEND); - } catch (IOException | AES_GCM.AesGcmException e) { + this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), fileHash), ResultCmdReceiver.STOREBACKEND); + } catch (IOException | SHA.ShaException e) { this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); e.printStackTrace(); } @@ -92,7 +95,6 @@ public class SavefileRule extends ProtocolReader { @Override protected SavefileRule.Result onExecuted(Context context, String... data) { SavefileRule.Result result = new SavefileRule.Result(context, data[FILE_NAME], Integer.parseInt(data[FILE_SIZE])); - return result; } @@ -101,7 +103,6 @@ public class SavefileRule extends ProtocolReader { ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); // Commande renvoyée en cas d'erreur result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); - return result; } } diff --git a/app/src/main/java/lightcontainer/storage/AppConfig.java b/app/src/main/java/lightcontainer/storage/AppConfig.java index 5091bbf..7cc0033 100644 --- a/app/src/main/java/lightcontainer/storage/AppConfig.java +++ b/app/src/main/java/lightcontainer/storage/AppConfig.java @@ -8,7 +8,7 @@ package lightcontainer.storage; * @since 1.0 */ public class AppConfig { - + // Variables private static AppConfig instance = null; private int unicastPort; private String multicastIp; @@ -98,7 +98,6 @@ public class AppConfig { /** * Méthode permettant d'assigner le chemin de sauvegarde des fichiers - * @return Chemin de sauvegarde */ public void setStoragePath(String storagePath) { this.storagePath = storagePath; diff --git a/app/src/main/java/lightcontainer/utils/FileReceiver.java b/app/src/main/java/lightcontainer/utils/FileReceiver.java index e513857..50e1b8d 100644 --- a/app/src/main/java/lightcontainer/utils/FileReceiver.java +++ b/app/src/main/java/lightcontainer/utils/FileReceiver.java @@ -7,13 +7,11 @@ import java.io.IOException; import java.io.InputStream; public class FileReceiver { - private static final int DEFAULT_BUFFER = 8000; private String path; public FileReceiver(String path) { this.path = path; } public boolean receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { - int bytesReceived = 0; BufferedOutputStream bosFile = null; try { diff --git a/app/src/main/java/lightcontainer/utils/SHA.java b/app/src/main/java/lightcontainer/utils/SHA.java index d942c61..4ffe472 100644 --- a/app/src/main/java/lightcontainer/utils/SHA.java +++ b/app/src/main/java/lightcontainer/utils/SHA.java @@ -32,6 +32,11 @@ public class SHA { new FileInputStream(inFile), (int)inFile.length() )); + + System.out.println(hashFile( // caca5439dc02f2ced5094e95f1a3403d42127cda29feecd2eb1c68ff38a6fee3 + "D:\\ffe", + "46ba86ddecd2fe80c3bdb9fb2f9480b4c92057447e9d1b43863dd1a6d540f3a316571684f9e3a7459f533a9792d4925e" + )); } /** @@ -69,6 +74,25 @@ public class SHA { return sb.toString(); } + /** + * Make a borrowing of the file. + * + * @param rootPath Root path of the file. + * @param fileName File Name. + * + * @return Borrowing of the file. + * + * @throws ShaException if an error occur. + */ + public static String hashFile(String rootPath, String fileName) throws ShaException { + try { + File file = new File(String.format("%s/%s", rootPath, fileName)); + return hashStream(new FileInputStream(file), (int)file.length()); + } catch (Exception e) { + throw new ShaException(e); + } + } + /** * Internal Error from SHA encryption Class */ diff --git a/app/src/main/java/lightcontainer/utils/ShaHasher.java b/app/src/main/java/lightcontainer/utils/ShaHasher.java index 71e1654..735c38c 100644 --- a/app/src/main/java/lightcontainer/utils/ShaHasher.java +++ b/app/src/main/java/lightcontainer/utils/ShaHasher.java @@ -54,7 +54,6 @@ public class ShaHasher { return generatedText; } - private byte[] generateSalt() { this.salt = new byte[16]; SecureRandom random = new SecureRandom(); @@ -63,7 +62,6 @@ public class ShaHasher { return salt; } - public String getSalt() { Base64.Encoder b64Encoder = Base64.getEncoder(); return b64Encoder.encodeToString(this.salt); diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 4345981..3bc0c1b 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1,27 +1 @@ -{ - "unicast_port": 8000, - "multicast_ip": "226.66.66.1", - "multicast_port": 15502, - "network_interface": "My network interface", - "tls": true, - "storagePath": "/home/benjamin/ffe", - "users": [ - { - "name": "aaaaa", - "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", - "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", - "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", - "files": [ - { - "name": "ebf1d834055adf836823c46d36334edf24f40245948cff0173f999392d8429536ccd57bf82090807a6b9cb4317b1bf64", - "fileNameSalt": "zXjO49TAftzugcnyaw3myQ==", - "size": 854, - "iv": "2DLTOZ4SX9MhTd6JdqFkuw==", - "storage": [ - "lightcontainerSB01" - ] - } - ] - } - ] -} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"D:\\ffe","users":[{"name":"endmove","password":"1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b","aes_key":"p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=","passwordSalt":"ItYiXkwPa84Gwb6dGHQvXQ==","files":[{"name":"096a4b2f5673e1f1e1f4cb7e1a569f06cc31064f5dd254948f5d7c44769fd02a098f48ce60c333fae14cfcb29cacc87e","fileNameSalt":"v9ByBhvxds33+6Ha7c8oPQ==","size":12648,"iv":"OM9l2Rt6dqLDsGS3bQuuEQ==","storage":["lightcontainerSB01"]}]},{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[]}]} \ No newline at end of file