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);
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.

View File

@ -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

View File

@ -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
*/

View File

@ -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();

View File

@ -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$";

View File

@ -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
*/

View File

@ -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

View File

@ -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;

View File

@ -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.

View File

@ -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 {

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
*/
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) {