summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--IOFileClass/FileManIOFile.cpp35
-rw-r--r--IOFileClass/FileManIOFile.hpp22
2 files changed, 34 insertions, 23 deletions
diff --git a/IOFileClass/FileManIOFile.cpp b/IOFileClass/FileManIOFile.cpp
index 424d704..bee2ebe 100644
--- a/IOFileClass/FileManIOFile.cpp
+++ b/IOFileClass/FileManIOFile.cpp
@@ -74,40 +74,32 @@ 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;
}
diff --git a/IOFileClass/FileManIOFile.hpp b/IOFileClass/FileManIOFile.hpp
index 310ded9..7013b27 100644
--- a/IOFileClass/FileManIOFile.hpp
+++ b/IOFileClass/FileManIOFile.hpp
@@ -50,12 +50,16 @@ 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);
@@ -63,6 +67,20 @@ class FileManIOFile {
/**
+ * @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.
*
* Return "readable" attribute.