Arrange FileManIOFile class

This commit is contained in:
manzerbredes 2015-03-11 17:21:24 +04:00
parent ecda75a273
commit 34b47f8e08
2 changed files with 34 additions and 23 deletions

View file

@ -74,41 +74,33 @@ void FileManIOFile::read(std::string key){
void FileManIOFile::write(std::string key,std::string data){
AESCrypt aes;
HASHCrypt hash;
std::string dataEncrypted;
dataEncrypted=aes.encrypt(key, data);
this->writeRoutine(data, dataEncrypted);
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;
}
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;
HASHCrypt hash;
std::string dataEncrypted;
dataEncrypted=aes.encrypt(this->key, data);
this->writeRoutine(data, dataEncrypted);
}
void FileManIOFile::writeRoutine(std::string data, std::string dataEncrypted){
HASHCrypt hash;
byte digest[16];
hash.getMD5_128(data, digest, sizeof(digest));
@ -126,9 +118,10 @@ void FileManIOFile::write(std::string data){
file.close();
this->data=data;
}
std::string FileManIOFile::getData(){
return this->data;
}

View file

@ -50,18 +50,36 @@ class FileManIOFile {
*/
void read(std::string key);
/**
* @brief Read encrypted file.
* @brief Write data in encrypted file.
*
* @param key : key to encrypt data
* @param data : data to write
*
* Save data to "filename" attribute.
* 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 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);
/**
* @brief True if file fully decrypted.
*