diff options
Diffstat (limited to 'IOFileClass')
| -rw-r--r-- | IOFileClass/FileManIOFile.cpp | 35 | ||||
| -rw-r--r-- | IOFileClass/FileManIOFile.hpp | 26 |
2 files changed, 56 insertions, 5 deletions
diff --git a/IOFileClass/FileManIOFile.cpp b/IOFileClass/FileManIOFile.cpp index d0ae1f2..bee2ebe 100644 --- a/IOFileClass/FileManIOFile.cpp +++ b/IOFileClass/FileManIOFile.cpp @@ -16,6 +16,7 @@ FileManIOFile::FileManIOFile(std::string filename){ this->filename=filename; this->readable=false; this->data=""; + this->key; } FileManIOFile::~FileManIOFile(){ } @@ -55,6 +56,7 @@ void FileManIOFile::read(std::string key){ if(hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){ this->readable=true; + hash.getSHA_256(key, this->key, 32); } else{ this->readable=false; @@ -66,12 +68,39 @@ void FileManIOFile::read(std::string key){ } -void FileManIOFile::write(std::string key, std::string data){ + + + +void FileManIOFile::write(std::string key,std::string data){ AESCrypt aes; + std::string dataEncrypted; + + dataEncrypted=aes.encrypt(key, data); + + this->writeRoutine(data, dataEncrypted); + + +} + +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; + std::string dataEncrypted; + + dataEncrypted=aes.encrypt(this->key, data); + this->writeRoutine(data, dataEncrypted); + + +} + + +void FileManIOFile::writeRoutine(std::string data, std::string dataEncrypted){ HASHCrypt hash; - std::string dataEncrypted=aes.encrypt(key, data); byte digest[16]; hash.getMD5_128(data, digest, sizeof(digest)); @@ -89,10 +118,10 @@ void FileManIOFile::write(std::string key, 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 4dfcfc4..7013b27 100644 --- a/IOFileClass/FileManIOFile.hpp +++ b/IOFileClass/FileManIOFile.hpp @@ -50,15 +50,35 @@ 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. @@ -87,6 +107,8 @@ class FileManIOFile { bool readable; ///< Readable attribute + byte key[32]; ///< Key in SHA-256 + |
