summaryrefslogtreecommitdiff
path: root/src/IOFileClass
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2015-03-21 07:08:34 +0100
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2015-03-21 07:08:34 +0100
commit3a10207bf5ddf14c478623b6e32ff72af6379a48 (patch)
tree8506da971d0661fc074b0b79a56fe705273d1c32 /src/IOFileClass
parent77affb6d5564f691ea337a3971fdc85f2460ed92 (diff)
parent697d44fd357779309bb0c1931f589a92bb88a642 (diff)
Merge branch 'PARSER'
Diffstat (limited to 'src/IOFileClass')
-rw-r--r--src/IOFileClass/FileManIOFile.cpp147
-rw-r--r--src/IOFileClass/FileManIOFile.hpp133
2 files changed, 280 insertions, 0 deletions
diff --git a/src/IOFileClass/FileManIOFile.cpp b/src/IOFileClass/FileManIOFile.cpp
new file mode 100644
index 0000000..ea49201
--- /dev/null
+++ b/src/IOFileClass/FileManIOFile.cpp
@@ -0,0 +1,147 @@
+/**
+ * @file FileManIOFile.cpp
+ * @brief FileManIOFile class definitions
+ * @author manzerbredes
+ * @date 9 Mars 2015
+ *
+ * Contain all definitions of FileManIOFile class.
+ *
+ */
+
+
+#include "FileManIOFile.hpp"
+
+//Constructor with filename
+FileManIOFile::FileManIOFile(std::string filename){
+ this->filename=filename;
+ this->readable=false;
+ this->data="";
+ this->key;
+}
+
+//Destructor
+FileManIOFile::~FileManIOFile(){
+}
+
+
+
+//Read the filename with a key
+void FileManIOFile::read(std::string key){
+
+ //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));
+
+ while(file){
+ this->data+=car,
+ file.read(&car, sizeof(car));
+
+ }
+
+ //Decrypt data
+ this->data=this->aes.decrypt(key, this->data);
+
+ //Get current MD5 of decrypted data
+ byte currentMD5[16];
+ this->hash.getMD5_128(this->data, 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;
+ //Save the key
+ this->hash.getSHA_256(key, this->key, 32);
+ }
+ else{
+ this->readable=false;
+ }
+
+ //Close file
+ file.close();
+
+
+}
+
+
+
+//Write file with key
+void FileManIOFile::write(std::string key,std::string data){
+
+ std::string dataEncrypted;
+
+ 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);
+ }
+
+ std::string dataEncrypted;
+
+ 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){
+
+ //Save MD5 of decrypted data
+ byte digest[16];
+ 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/src/IOFileClass/FileManIOFile.hpp b/src/IOFileClass/FileManIOFile.hpp
new file mode 100644
index 0000000..221cf13
--- /dev/null
+++ b/src/IOFileClass/FileManIOFile.hpp
@@ -0,0 +1,133 @@
+/**
+ * @file FileManIOFile.hpp
+ * @brief FileManIOFile class definitions
+ * @author manzerbredes
+ * @date 9 Mars 2015
+ *
+ * Contain all definitions of FileManIOFile class.
+ *
+ */
+
+
+#ifndef __FileManIOFile__
+#define __FileManIOFile__
+
+
+//----- 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 files.
+ * @author manzerbredes
+ *
+ * -----File organisation-----
+ *
+ * 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,
+ * and key saved in key attribute (before been crypted with SHA-256 algorithm).
+ *
+ */
+ void read(std::string key);
+
+
+ /**
+ * @brief Write data in encrypted file.
+ *
+ * @param key : key to encrypt data
+ * @param data : data to write
+ *
+ * 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).
+ *
+ */
+ 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.
+ *
+ * @param data : data to write (for MD5)
+ * @param dataEncrypted : data to write
+ *
+ * Write encryptedData to filename
+ *
+ */
+ void writeRoutine(std::string data, std::string dataEncrypted);
+
+
+ //Attributes:
+
+ AESCrypt aes; ///< AES instance
+
+ HASHCrypt hash; ///< HASH instance
+
+ std::string filename; ///< Filename attribute
+
+ std::string data; ///< Data attribute
+
+ bool readable; ///< Readable attribute
+
+ byte key[32]; ///< Key in SHA-256
+
+};
+
+#endif