forgetIt/IOFileClass/FileManIOFile.cpp

148 lines
2.8 KiB
C++
Raw Normal View History

/**
* @file FileManIOFile.cpp
* @brief FileManIOFile class definitions
* @author manzerbredes
* @date 9 Mars 2015
*
* Contain all definitions of FileManIOFile class.
*
*/
#include "FileManIOFile.hpp"
2015-03-13 15:39:56 +04:00
//Constructor with filename
FileManIOFile::FileManIOFile(std::string filename){
this->filename=filename;
this->readable=false;
this->data="";
this->key;
}
2015-03-13 15:39:56 +04:00
//Destructor
FileManIOFile::~FileManIOFile(){
}
2015-03-13 15:39:56 +04:00
//Read the filename with a key
void FileManIOFile::read(std::string key){
2015-03-13 15:39:56 +04:00
//create file object
std::ifstream file;
2015-03-13 15:39:56 +04:00
//Clear data
this->data.clear();
2015-03-13 15:39:56 +04:00
//Open file
file.open (this->filename, std::ios::in | std::ios::binary);
2015-03-13 15:39:56 +04:00
//Get MD5 of decrypted data
byte fileMD5[16];
file.read((char*) fileMD5, sizeof(fileMD5));
2015-03-13 15:39:56 +04:00
//Read all data
char car;
file.read(&car, sizeof(car));
while(file){
this->data+=car,
file.read(&car, sizeof(car));
}
2015-03-13 15:39:56 +04:00
//Decrypt data
this->data=this->aes.decrypt(key, this->data);
2015-03-13 15:39:56 +04:00
//Get current MD5 of decrypted data
byte currentMD5[16];
2015-03-13 15:39:56 +04:00
this->hash.getMD5_128(this->data, currentMD5, sizeof(currentMD5));
2015-03-13 15:39:56 +04:00
//Compare the 2 MD5 to find if file is fully decrypted
if(this->hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){
//Set readable
this->readable=true;
2015-03-13 15:39:56 +04:00
//Save the key
this->hash.getSHA_256(key, this->key, 32);
}
else{
this->readable=false;
}
2015-03-13 15:39:56 +04:00
//Close file
file.close();
}
2015-03-13 15:39:56 +04:00
//Write file with key
void FileManIOFile::write(std::string key,std::string data){
std::string dataEncrypted;
2015-03-13 15:39:56 +04:00
dataEncrypted=this->aes.encrypt(key, data);
2015-03-11 17:21:24 +04:00
this->writeRoutine(data, dataEncrypted);
}
2015-03-13 15:39:56 +04:00
//Write file without key
void FileManIOFile::write(std::string data){
2015-03-11 17:21:24 +04:00
if(not(this->readable)){
std::cout << "Can't write data without key (read it before) !" << std::endl;
std::exit(EXIT_FAILURE);
}
2015-03-13 15:39:56 +04:00
std::string dataEncrypted;
2015-03-13 15:39:56 +04:00
dataEncrypted=this->aes.encrypt(this->key, data);
2015-03-11 17:21:24 +04:00
this->writeRoutine(data, dataEncrypted);
2015-03-11 17:21:24 +04:00
}
2015-03-13 15:39:56 +04:00
//Get readable attribute
bool FileManIOFile::isReadable(){
return this->readable;
}
//Write file
2015-03-11 17:21:24 +04:00
void FileManIOFile::writeRoutine(std::string data, std::string dataEncrypted){
2015-03-13 15:39:56 +04:00
//Save MD5 of decrypted data
byte digest[16];
2015-03-13 15:39:56 +04:00
this->hash.getMD5_128(data, digest, sizeof(digest));
2015-03-13 15:39:56 +04:00
//Create file instance
std::ofstream file;
2015-03-13 15:39:56 +04:00
//Open it
file.open(this->filename, std::ios::out | std::ios::binary);
2015-03-13 15:39:56 +04:00
//Write MD5 on 16 first bytes
file.write((char *) digest,sizeof(digest));
2015-03-13 15:39:56 +04:00
//Write data
file.write(dataEncrypted.c_str(), dataEncrypted.size());
2015-03-13 15:39:56 +04:00
//Close file
file.close();
2015-03-13 15:39:56 +04:00
//Save data to attribute
this->data=data;
}
2015-03-11 17:21:24 +04:00
2015-03-13 15:39:56 +04:00
//Get data
std::string FileManIOFile::getData(){
return this->data;
}