Implement FileManIOFileClass, general bug correction.
This commit is contained in:
parent
998745fe45
commit
783197aaa1
8 changed files with 228 additions and 18 deletions
|
@ -0,0 +1,98 @@
|
|||
/**
|
||||
* @file FileManIOFile.cpp
|
||||
* @brief FileManIOFile class definitions
|
||||
* @author manzerbredes
|
||||
* @date 9 Mars 2015
|
||||
*
|
||||
* Contain all definitions of FileManIOFile class.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "FileManIOFile.hpp"
|
||||
#include <iomanip> // This might be necessary
|
||||
|
||||
FileManIOFile::FileManIOFile(std::string filename){
|
||||
this->filename=filename;
|
||||
this->readable=false;
|
||||
this->data="";
|
||||
}
|
||||
FileManIOFile::~FileManIOFile(){
|
||||
}
|
||||
|
||||
|
||||
bool FileManIOFile::isReadable(){
|
||||
return this->readable;
|
||||
}
|
||||
|
||||
|
||||
void FileManIOFile::read(std::string key){
|
||||
|
||||
AESCrypt aes;
|
||||
HASHCrypt hash;
|
||||
|
||||
std::ifstream file;
|
||||
this->data.clear();
|
||||
|
||||
file.open (this->filename, std::ios::in | std::ios::binary);
|
||||
|
||||
byte fileMD5[16];
|
||||
file.read((char*) fileMD5, sizeof(fileMD5));
|
||||
|
||||
char car;
|
||||
file.read(&car, sizeof(car));
|
||||
|
||||
while(file){
|
||||
this->data+=car,
|
||||
file.read(&car, sizeof(car));
|
||||
|
||||
}
|
||||
|
||||
this->data=aes.decrypt(key, this->data);
|
||||
|
||||
byte currentMD5[16];
|
||||
hash.getMD5_128(this->data, currentMD5, sizeof(currentMD5));
|
||||
|
||||
if(hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){
|
||||
this->readable=true;
|
||||
}
|
||||
else{
|
||||
this->readable=false;
|
||||
}
|
||||
|
||||
|
||||
file.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void FileManIOFile::write(std::string key, std::string data){
|
||||
|
||||
AESCrypt aes;
|
||||
HASHCrypt hash;
|
||||
|
||||
std::string dataEncrypted=aes.encrypt(key, data);
|
||||
byte digest[16];
|
||||
hash.getMD5_128(data, digest, sizeof(digest));
|
||||
|
||||
|
||||
std::ofstream file;
|
||||
file.open(this->filename, std::ios::out | std::ios::binary);
|
||||
|
||||
file.write((char *) digest,sizeof(digest));
|
||||
|
||||
|
||||
file.write(dataEncrypted.c_str(), dataEncrypted.size());
|
||||
|
||||
|
||||
|
||||
file.close();
|
||||
|
||||
this->data=data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
std::string FileManIOFile::getData(){
|
||||
return this->data;
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* @file FileManIOFile.hpp
|
||||
* @brief FileManIOFile class definitions
|
||||
* @author manzerbredes
|
||||
* @date 9 Mars 2015
|
||||
*
|
||||
* Contain all definitions of FileManIOFile class.
|
||||
*
|
||||
*/
|
||||
|
||||
//--- 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.
|
||||
* @author manzerbredes
|
||||
*
|
||||
* -----File organisation-----
|
||||
*
|
||||
* 16 first bytes : md5 of decrypted file
|
||||
* rest of the file : data encrypted (ASE for now)
|
||||
*
|
||||
*/
|
||||
class FileManIOFile {
|
||||
|
||||
public:
|
||||
FileManIOFile(std::string filename);
|
||||
~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.
|
||||
*
|
||||
*/
|
||||
void read(std::string key);
|
||||
|
||||
/**
|
||||
* @brief Read encrypted file.
|
||||
*
|
||||
* @param key : key to encrypt data
|
||||
*
|
||||
* Save data to "filename" attribute.
|
||||
*
|
||||
*/
|
||||
void write(std::string key, std::string data);
|
||||
|
||||
/**
|
||||
* @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:
|
||||
|
||||
std::string filename; ///< Filename attribute
|
||||
|
||||
std::string data; ///< Data attribute
|
||||
|
||||
bool readable; ///< Readable attribute
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue