From 9b99763c2ed924e340c7dbf73842207ba56ed125 Mon Sep 17 00:00:00 2001
From: manzerbredes <loic.guegan_secondary@yahoo.fr>
Date: Fri, 13 Mar 2015 15:39:56 +0400
Subject: [PATCH] Check all IOFileClass.

---
 IOFileClass/FileManIOFile.cpp | 68 ++++++++++++++++++++-----------
 IOFileClass/FileManIOFile.hpp | 77 +++++++++++++++++++++--------------
 main.cpp                      |  5 +++
 3 files changed, 95 insertions(+), 55 deletions(-)

diff --git a/IOFileClass/FileManIOFile.cpp b/IOFileClass/FileManIOFile.cpp
index bee2ebe..ea49201 100644
--- a/IOFileClass/FileManIOFile.cpp
+++ b/IOFileClass/FileManIOFile.cpp
@@ -10,36 +10,38 @@
 
 
 #include "FileManIOFile.hpp"
-#include <iomanip> // This might be necessary
 
+//Constructor with filename
 FileManIOFile::FileManIOFile(std::string filename){
     this->filename=filename;
     this->readable=false;
     this->data="";
     this->key;
 }
+
+//Destructor
 FileManIOFile::~FileManIOFile(){
 }
 
 
-bool FileManIOFile::isReadable(){
-    return this->readable;
-}
-
 
+//Read the filename with a key
 void FileManIOFile::read(std::string key){
 
-    AESCrypt aes;
-    HASHCrypt hash;
-
+    //create file object
     std::ifstream file;
+
+    //Clear data
     this->data.clear();
 
+    //Open file
     file.open (this->filename, std::ios::in | std::ios::binary);
 
+    //Get MD5 of decrypted data
     byte fileMD5[16];
     file.read((char*) fileMD5, sizeof(fileMD5));
 
+    //Read all data
     char car;
     file.read(&car, sizeof(car));
 
@@ -49,20 +51,25 @@ void FileManIOFile::read(std::string key){
 
     }
 
-    this->data=aes.decrypt(key, this->data);
+    //Decrypt data
+    this->data=this->aes.decrypt(key, this->data);
 
+    //Get current MD5 of decrypted data
     byte currentMD5[16];
-    hash.getMD5_128(this->data, currentMD5, sizeof(currentMD5));
+    this->hash.getMD5_128(this->data, currentMD5, sizeof(currentMD5));
 
-    if(hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){
+    //Compare the 2 MD5 to find if file is fully decrypted
+    if(this->hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){
+        //Set readable
         this->readable=true;
-        hash.getSHA_256(key, this->key, 32);
+        //Save the key
+        this->hash.getSHA_256(key, this->key, 32);
     }
     else{
         this->readable=false;
     }
 
-
+    //Close file
     file.close();
 
 
@@ -70,58 +77,71 @@ void FileManIOFile::read(std::string key){
 
 
 
-
+//Write file with key
 void FileManIOFile::write(std::string key,std::string data){
 
-    AESCrypt aes;
     std::string dataEncrypted;
 
-    dataEncrypted=aes.encrypt(key, data);
+    dataEncrypted=this->aes.encrypt(key, data);
 
     this->writeRoutine(data, dataEncrypted);
 
 
 }
 
+//Write file without key
 void FileManIOFile::write(std::string data){
     if(not(this->readable)){
         std::cout << "Can't write data without key (read it before) !" << std::endl;
         std::exit(EXIT_FAILURE);
     }
-    AESCrypt aes;
+
     std::string dataEncrypted;
 
-    dataEncrypted=aes.encrypt(this->key, data);
+    dataEncrypted=this->aes.encrypt(this->key, data);
     this->writeRoutine(data, dataEncrypted);
 
 
 }
 
 
+
+//Get readable attribute
+bool FileManIOFile::isReadable(){
+    return this->readable;
+}
+
+
+
+//Write file
 void FileManIOFile::writeRoutine(std::string data, std::string dataEncrypted){
-    HASHCrypt hash;
 
+    //Save MD5 of decrypted data
     byte digest[16];
-    hash.getMD5_128(data, digest, sizeof(digest));
-
+    this->hash.getMD5_128(data, digest, sizeof(digest));
 
+    //Create file instance
     std::ofstream file;
+
+    //Open it
     file.open(this->filename, std::ios::out | std::ios::binary);
 
+    //Write MD5 on 16 first bytes
     file.write((char *) digest,sizeof(digest));
 
-
+    //Write data
     file.write(dataEncrypted.c_str(), dataEncrypted.size());
 
-
-
+    //Close file
     file.close();
 
+    //Save data to attribute
     this->data=data;
 }
 
 
 
+//Get data
 std::string FileManIOFile::getData(){
     return this->data;
 }
diff --git a/IOFileClass/FileManIOFile.hpp b/IOFileClass/FileManIOFile.hpp
index 7013b27..221cf13 100644
--- a/IOFileClass/FileManIOFile.hpp
+++ b/IOFileClass/FileManIOFile.hpp
@@ -12,40 +12,51 @@
 #ifndef __FileManIOFile__
 #define __FileManIOFile__
 
-//--- std -----
+
+//----- std -----
 #include <iostream>
 #include <string>
 #include <fstream>
 
+
 //----- class -----
 #include "HASHCrypt.hpp"
 #include "AESCrypt.hpp"
 
 
+
+
 /**
  * @class FileManIOFile FileManIOFile.hpp "/CryptClass/FileManIOFile.hpp"
- * @brief Class for quick open and close encrypted file.
+ * @brief Class for quick open and close encrypted files.
  * @author manzerbredes
  *
  * -----File organisation-----
  *
- * 16 first bytes : md5 of decrypted file
- * rest of the file : data encrypted (ASE for now)
+ * 16 first bytes : md5 of decrypted data
+ * Rest of the file : data encrypted (ASE for now)
  *
+ * \bug Need check if file exist and can be opened
  */
 class FileManIOFile {
 
     public:
+
+        //Constructor
         FileManIOFile(std::string filename);
+
+        //Destructor
         ~FileManIOFile();
 
+
         /**
         * @brief Read encrypted file.
         *
         * @param key : key to encrypt data
         *
         * Read data from "filename" attribute.
-        * If file fully decrypted, readable var switch to true.
+        * If file fully decrypted, readable var switch to true,
+        * and key saved in key attribute (before been crypted with SHA-256 algorithm).
         *
         */
         void read(std::string key);
@@ -57,15 +68,40 @@ class FileManIOFile {
         * @param key : key to encrypt data
         * @param data : data to write
         *
-        * Write the file with or without key
+        * Write the file with or without key.
         * To write data without key, you need to read it before (to save the key
-        * in attribute key;
+        * in attribute key).
         *
         */
         void write(std::string key, std::string data);
         void write(std::string data);
 
 
+        /**
+        * @brief True if file fully decrypted.
+        *
+        * @return readable attribute
+        *
+        * Return "readable" attribute.
+        *
+        */
+        bool isReadable();
+
+
+        /**
+        * @brief Get data attribute.
+        *
+        * @return data attribute.
+        *
+        * **Warning** if data not fully decrypted (readable!=true),
+        * data will be unreadable (unparsable).
+        *
+        */
+        std::string getData();
+
+
+    private:
+
         /**
         * @brief Write data in encrypted file.
         *
@@ -78,28 +114,11 @@ class FileManIOFile {
         void writeRoutine(std::string data, std::string dataEncrypted);
 
 
+        //Attributes:
 
+        AESCrypt aes; ///< AES instance
 
-        /**
-        * @brief True if file fully decrypted.
-        *
-        * Return "readable" attribute.
-        *
-        */
-        bool isReadable();
-
-
-        /**
-        * @brief Get data attribute.
-        *
-        * Return "data" attribute.
-        *
-        * **Warning** if data not fully decrypted (readable!=true),
-        * data will be unreadable.
-        */
-        std::string getData();
-
-    private:
+        HASHCrypt hash; ///< HASH instance
 
         std::string filename; ///< Filename attribute
 
@@ -109,10 +128,6 @@ class FileManIOFile {
 
         byte key[32]; ///< Key in SHA-256
 
-
-
-
-
 };
 
 #endif
diff --git a/main.cpp b/main.cpp
index 2cbc9ef..08aa05d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -33,7 +33,12 @@
  */
 int main(int argc, char *argv[]){
 
+    /*
+    FileManIOFile fichier("Doxygen/bob.bin");
 
+    fichier.read("loic");
+
+    std::cout << fichier.getData();*/
 
 
     std::string xml="<?xml version=\"1.0\" standalone=\"yes\" ?>\n\