diff options
| author | manzerbredes <loic.guegan_secondary@yahoo.fr> | 2015-03-20 10:57:03 +0400 |
|---|---|---|
| committer | manzerbredes <loic.guegan_secondary@yahoo.fr> | 2015-03-20 10:57:03 +0400 |
| commit | 45f7cc5d27b338dae1d36c211cc5720c82f3de35 (patch) | |
| tree | 38c356a4e5ba77520e59964f25cde28527e552bd /src | |
| parent | eb267792cd70a763bf2dcbfce6e7a09251b3913f (diff) | |
Change file architecture
Diffstat (limited to 'src')
| -rw-r--r-- | src/CryptClass/AESCrypt.cpp | 126 | ||||
| -rw-r--r-- | src/CryptClass/AESCrypt.hpp | 101 | ||||
| -rw-r--r-- | src/CryptClass/AbstractSKA.hpp | 57 | ||||
| -rw-r--r-- | src/CryptClass/HASHCrypt.cpp | 116 | ||||
| -rw-r--r-- | src/CryptClass/HASHCrypt.hpp | 135 | ||||
| -rw-r--r-- | src/IOFileClass/FileManIOFile.cpp | 147 | ||||
| -rw-r--r-- | src/IOFileClass/FileManIOFile.hpp | 133 | ||||
| -rw-r--r-- | src/ParserClass/AbstractIDManager.cpp | 39 | ||||
| -rw-r--r-- | src/ParserClass/AbstractIDManager.hpp | 59 | ||||
| -rw-r--r-- | src/ParserClass/FileManContainer/Website.cpp | 67 | ||||
| -rw-r--r-- | src/ParserClass/FileManContainer/Website.hpp | 83 | ||||
| -rw-r--r-- | src/ParserClass/FileManParser.cpp | 132 | ||||
| -rw-r--r-- | src/ParserClass/FileManParser.hpp | 114 | ||||
| -rw-r--r-- | src/main.cpp | 80 |
14 files changed, 1389 insertions, 0 deletions
diff --git a/src/CryptClass/AESCrypt.cpp b/src/CryptClass/AESCrypt.cpp new file mode 100644 index 0000000..d3ea949 --- /dev/null +++ b/src/CryptClass/AESCrypt.cpp @@ -0,0 +1,126 @@ +/** + * @file AESCrypt.cpp + * @brief AESCrypt class definitions + * @author manzerbredes + * @date 8 Mars 2015 + * + * Contain all definitions of AESCrypt class. + * + */ + + +//----- class ----- +#include "AESCrypt.hpp" + + + +//Constructor +AESCrypt::AESCrypt(){ + this->hash=HASHCrypt(); //Init hash attribute +} + +//Destructor +AESCrypt::~AESCrypt(){ + +} + + + +//Encrypt string with string key +std::string AESCrypt::encrypt(std::string key, std::string data){ + + //Generate SHA-256 + byte digest[32]; + hash.getSHA_256(key, digest, (int)sizeof(digest)); + + return encryptRoutine(data, digest, sizeof(digest)); + +} + + + +//Encrypt string with byte* key +std::string AESCrypt::encrypt(byte* key, std::string data){ + return encryptRoutine(data, key, 32); +} + + + +//The encryptRoutine +std::string AESCrypt::encryptRoutine(std::string data, byte* digest, int size){ + //Contain data encrypted + std::string cipher; + + + //Use try, catch to be ensure no problems happening + try{ + //Create encoder to encrypt data + CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption encoder; + encoder.SetKey( digest, size ); + + //Encrypt data with StreamTransformationFilter with NO PADDING + CryptoPP::StringSource ss1(data, true, + new CryptoPP::StreamTransformationFilter( encoder, + new CryptoPP::StringSink( cipher ), + CryptoPP::StreamTransformationFilter::ZEROS_PADDING + + ) + ); + } + catch( CryptoPP::Exception& e ) + { + std::cerr << e.what() << std::endl; + exit(EXIT_FAILURE); + } + + //return encrypted data + return cipher; + + +} + + + +//Decrypt string +std::string AESCrypt::decrypt(std::string key, std::string data){ + + //Get SHA-256 + byte digest[32]; + hash.getSHA_256(key, digest, (int)sizeof(digest)); + + + //Contain data decrypted + std::string cipher; + + //Use try, catch to be ensure no problems happening + try { + + //Create decoder to encrypt data + CryptoPP::ECB_Mode< CryptoPP::AES >::Decryption decoder; + decoder.SetKey( digest, sizeof(digest) ); + + //Decrypt data with StreamTransformationFilter with NO PADDING + CryptoPP::StringSource ss3( data, true, + new CryptoPP::StreamTransformationFilter( decoder, + new CryptoPP::StringSink( cipher ), + CryptoPP::StreamTransformationFilter::NO_PADDING + ) + ); + } + catch( CryptoPP::Exception& e ) + { + std::cerr << e.what() << std::endl; + exit(1); + } + + //Remove ZEROS padding + int i=0; + for(i=0;i<cipher.length();i++){ + if(cipher[i]=='\0') + break; + } + cipher.erase(i,cipher.length()-1); //Erase ZEROS + + //return decrypted data + return cipher; +} diff --git a/src/CryptClass/AESCrypt.hpp b/src/CryptClass/AESCrypt.hpp new file mode 100644 index 0000000..c4d75aa --- /dev/null +++ b/src/CryptClass/AESCrypt.hpp @@ -0,0 +1,101 @@ +/** + * @file AESCrypt.hpp + * @brief AESCrypt class header + * @author manzerbredes + * @date 8 Mars 2015 + * + * Contain all prototypes of AESCrypt class. + * + */ + +#ifndef __AESCrypt__ +#define __AESCrypt__ + +//----- std ----- +#include "AbstractSKA.hpp" +#include "HASHCrypt.hpp" +#include <iostream> + +//----- crypto++ ----- +#include <crypto++/aes.h> +#include <crypto++/modes.h> +#include <crypto++/filters.h> + + +/** + * @class AESCrypt AESCrypt.hpp "/CryptClass/AESCrypt.hpp" + * @brief Class for Advanced Encryption Standard (AES) algorithm + * @author manzerbredes + * + * This class provide AES encrypt and decrypt. + * Key used is 32 bytes key (256 bits). + * + * \bug Find another solution for managing padding. + */ +class AESCrypt : public AbstractSKA { + + + public: + //Constructor + AESCrypt(); + + //Destructor + ~AESCrypt(); + + + /** + * @brief Run encryptRoutine. + * + * @param key : key used to encrypt data + * @param data : contain data to encrypt. + * + * @return string : correspond to crypted data + * + * Run encryptRoutine with byte* key or string key. + * Allow you to choose between string key or byte key. + * + */ + std::string encrypt(std::string key, std::string data); + std::string encrypt(byte* key, std::string data); + + + /** + * @brief Decrypt data from AES algorithm. + * + * @param key : key used to encrypt data + * @param data : contain data to decrypt from AES encrypt. + * + * @return string : correspond to decrypted data + * + * Decrypt data, and return them into a string. + * + */ + std::string decrypt(std::string key, std::string data); + + + + private: + + /** + * @brief Encrypt data with AES algorithm. + * + * @param key : key used to encrypt data + * @param data : contain data to encrypt. + * + * @return string : correspond to crypted data + * + * Encrypt data, and return them into a string. + * Padding are blank space. + * + */ + std::string encryptRoutine(std::string data, byte* digest, int size); + + + //Attributes: + + HASHCrypt hash; ///< hash instance to generate SHA-256 hash code. + + +}; + +#endif diff --git a/src/CryptClass/AbstractSKA.hpp b/src/CryptClass/AbstractSKA.hpp new file mode 100644 index 0000000..7622ab4 --- /dev/null +++ b/src/CryptClass/AbstractSKA.hpp @@ -0,0 +1,57 @@ +/** + * @file AbstractSKA.hpp + * @brief Class for Symmetric-Key Algorithm (SKA) + * @author manzerbredes + * @date 8 Mars 2015 + * + * Specify which method the algorithm must be implement. + * + */ + +#ifndef __AbstractSKA__ +#define __AbstractSKA__ + + +//----- std ----- +#include <string> + + + +/** + * @class AbstractSKA AbstractSKA.hpp "/CryptClass/AbstractSKA.hpp" + * @brief Class for Symmetric-Key Algorithm (SKA) + * @author manzerbredes + * + * This class should not be instantiate directly. + * + */ +class AbstractSKA { + + public: + /** + * @brief Encrypt data. + * + * @param key : key used to encrypt data + * @param data : contain data to encrypt. + * + * This method must be overwritten. + * **Warning** data will be modified. + * + */ + virtual std::string encrypt(std::string key, std::string data) = 0; + + + /** + * @brief Decrypt data. + * + * @param key : key used to decrypt data + * @param data : contain data to decrypt. + * + * This method must be overwritten. + * **Warning** data will be modified. + * + */ + virtual std::string decrypt(std::string key, std::string data) = 0; +}; + +#endif diff --git a/src/CryptClass/HASHCrypt.cpp b/src/CryptClass/HASHCrypt.cpp new file mode 100644 index 0000000..eed9cb5 --- /dev/null +++ b/src/CryptClass/HASHCrypt.cpp @@ -0,0 +1,116 @@ +/** + * @file HASHCrypt.cpp + * @brief HASHCrypt class definitions + * @author manzerbredes + * @date 8 Mars 2015 + * + * Contain all definitions of HASHCrypt class. + * + */ + + +//----- class ----- +#include "HASHCrypt.hpp" + + + +//Constructor +HASHCrypt::HASHCrypt(){ +} + +//Destructor +HASHCrypt::~HASHCrypt(){ +} + + + +//Contruct MD5 over 128 bits and put it into digest +void HASHCrypt::getMD5_128(std::string chain, byte* digest, int size){ + + //Digest size controller + this->checkDigestSize(CryptoPP::Weak1::MD5::DIGESTSIZE,size); + + //Create the MD5 on digest parameter + CryptoPP::Weak1::MD5 hash; + hash.CalculateDigest( digest, (byte*) chain.c_str(), chain.length() ); + +} + + + +//Contruct SHA-256 and put it into digest +void HASHCrypt::getSHA_256(std::string chain, byte* digest, int size){ + + //Digest size controller + this->checkDigestSize(CryptoPP::SHA256::DIGESTSIZE,size); + + //Create the SHA-256 on digest parameter + CryptoPP::SHA256 hash; + hash.CalculateDigest( digest, (byte*) chain.c_str(), chain.length() ); + + +} + + + +//Compare 2 digest (same size) +bool HASHCrypt::compareDigest(byte* digest1, byte* digest2, int size){ + + //Try is more safe + try + { + //Compare the two digest + for(int i=0; i<size; i++){ + //Return false if digest are different + if(digest1[i] != digest2[i]){ + return false; + } + } + } + catch (std::exception& e) + { + std::cerr << "Exception catched : " << e.what() << std::endl; + } + + //Return true if digest are equals + return true; +} + + + +//Convert digest to string +std::string HASHCrypt::digestToString(byte* digest, int size){ + + CryptoPP::HexEncoder encoder; + std::string output; + encoder.Attach( new CryptoPP::StringSink( output ) ); + encoder.Put( digest, size ); + encoder.MessageEnd(); + + return output; +} + + + +//Check the size of the digest +void HASHCrypt::checkDigestSize(int sizeRequired, int size){ + try{ + if(size !=sizeRequired){ + throw this->getInvalidDigestSizeError(sizeRequired, size); + } + + } + catch(std::string erreur){ + std::cerr << erreur <<std::endl; + std::exit(EXIT_FAILURE); + } +} + + + +//Make the error +std::string HASHCrypt::getInvalidDigestSizeError(int sizeRequired, int size){ + std::ostringstream erreurStream; + erreurStream << "Invalid digest size ! ("<< sizeRequired <<" bytes required and "<< size <<" given)"; + return erreurStream.str(); +} diff --git a/src/CryptClass/HASHCrypt.hpp b/src/CryptClass/HASHCrypt.hpp new file mode 100644 index 0000000..3eafb2f --- /dev/null +++ b/src/CryptClass/HASHCrypt.hpp @@ -0,0 +1,135 @@ +/** + * @file HASHCrypt.hpp + * @brief HASHCrypt class header + * @author manzerbredes + * @date 8 Mars 2015 + * + * Contain all prototypes of HASHCrypt class. + * + */ + +#ifndef __HASHCrypt__ +#define __HASHCrypt__ + +//----- std ----- +#include <iostream> +#include <string> +#include <sstream> + +//----- crypto++ ----- +#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 +#include <crypto++/md5.h> //For MD5 +#include <crypto++/hex.h> //For Hex convertion +#include <crypto++/sha.h> //For SHA + + +/** + * @class HASHCrypt HASHCrypt.hpp "/CryptClass/HASHCrypt.hpp" + * @brief Hashing class + * @author manzerbredes + * + * Class who handle hashing functions on a byte* parameter. + * HASHCrypt try to detect errors and throw exceptions. + * HASHCrypt use crypto++ library. + */ +class HASHCrypt{ + + public: + /** + * @brief Contructor + */ + HASHCrypt(); + + /** + * @brief Destructor + */ + ~HASHCrypt(); + + + /** + * @brief Create an MD5 over 128 bits on a digest array of bytes. + * + * @param chain : Chain to hash + * @param digest : An array of bytes (8 bits) + * @param size : Length of digest + * + * **Warning** digest will be modified. + * Digest must be an array of byte with 16 entries + * Invalid size can cause "Segmentation Fault" + */ + void getMD5_128(std::string chain, byte* digest, int size); + + + /** + * @brief Create an SHA over 256 bits on a digest array of bytes. + * + * @param chain : Chain to hash + * @param digest : An array of bytes (8 bits) + * @param size : Length of digest + * + * **Warning** digest will be modified. + * Digest must be an array of byte with 32 entries + * Invalid size can cause "Segmentation Fault" + */ + void getSHA_256(std::string chain, byte* digest, int size); //Return SHA_256 + + + /** + * @brief Convert digest to a string of HEX characters + * + * @param digest : an array of bytes (8 bits) + * @param size : length of digest + * + * @return return a string of hex digest equivalent + * + * Digest must be an array of byte. + */ + std::string digestToString(byte* digest, int size); //Return a string + + + /** + * @brief Compare 2 digest + * + * @param digest1 : An array of bytes (8 bits) + * @param digest2 : An array of bytes (8 bits) + * @param size : Length of the array digest1 or digest2 + * + * @return return a boolean (true if digest1 equals to digest2 and false else) + * + * **Warning** if sizeof(digest1) != sizeof(digest 2) : segmentation fault ! + * Compare the two digest. + * + */ + bool compareDigest(byte* digest1, byte* digest2, int size); + + + private: + + /** + * @brief Check the digest size + * + * @param sizeRequired : Digest size expected + * @param size : Given digest size + * + * Throw an exception, and stop the program if + * sizeRequired != size + * Use getInvalidDigestSizeError method. + */ + void checkDigestSize(int sizeRequired, int size); + + + /** + * @brief Make "invalid digest size" error message. + * + * @param sizeRequired : Digest size expected + * @param size : Given digest size + * + * @return a string correspond to the error message + * + * Construct an error message with sizeRequired and size. + */ + std::string getInvalidDigestSizeError(int sizeRequired, int size); + +}; + +#endif diff --git a/src/IOFileClass/FileManIOFile.cpp b/src/IOFileClass/FileManIOFile.cpp new file mode 100644 index 0000000..ea49201 --- /dev/null +++ b/src/IOFileClass/FileManIOFile.cpp @@ -0,0 +1,147 @@ +/** + * @file FileManIOFile.cpp + * @brief FileManIOFile class definitions + * @author manzerbredes + * @date 9 Mars 2015 + * + * Contain all definitions of FileManIOFile class. + * + */ + + +#include "FileManIOFile.hpp" + +//Constructor with filename +FileManIOFile::FileManIOFile(std::string filename){ + this->filename=filename; + this->readable=false; + this->data=""; + this->key; +} + +//Destructor +FileManIOFile::~FileManIOFile(){ +} + + + +//Read the filename with a key +void FileManIOFile::read(std::string key){ + + //create file object + std::ifstream file; + + //Clear data + this->data.clear(); + + //Open file + file.open (this->filename, std::ios::in | std::ios::binary); + + //Get MD5 of decrypted data + byte fileMD5[16]; + file.read((char*) fileMD5, sizeof(fileMD5)); + + //Read all data + char car; + file.read(&car, sizeof(car)); + + while(file){ + this->data+=car, + file.read(&car, sizeof(car)); + + } + + //Decrypt data + this->data=this->aes.decrypt(key, this->data); + + //Get current MD5 of decrypted data + byte currentMD5[16]; + this->hash.getMD5_128(this->data, currentMD5, sizeof(currentMD5)); + + //Compare the 2 MD5 to find if file is fully decrypted + if(this->hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){ + //Set readable + this->readable=true; + //Save the key + this->hash.getSHA_256(key, this->key, 32); + } + else{ + this->readable=false; + } + + //Close file + file.close(); + + +} + + + +//Write file with key +void FileManIOFile::write(std::string key,std::string data){ + + std::string dataEncrypted; + + dataEncrypted=this->aes.encrypt(key, data); + + this->writeRoutine(data, dataEncrypted); + + +} + +//Write file without key +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); + } + + std::string dataEncrypted; + + dataEncrypted=this->aes.encrypt(this->key, data); + this->writeRoutine(data, dataEncrypted); + + +} + + + +//Get readable attribute +bool FileManIOFile::isReadable(){ + return this->readable; +} + + + +//Write file +void FileManIOFile::writeRoutine(std::string data, std::string dataEncrypted){ + + //Save MD5 of decrypted data + byte digest[16]; + this->hash.getMD5_128(data, digest, sizeof(digest)); + + //Create file instance + std::ofstream file; + + //Open it + file.open(this->filename, std::ios::out | std::ios::binary); + + //Write MD5 on 16 first bytes + file.write((char *) digest,sizeof(digest)); + + //Write data + file.write(dataEncrypted.c_str(), dataEncrypted.size()); + + //Close file + file.close(); + + //Save data to attribute + this->data=data; +} + + + +//Get data +std::string FileManIOFile::getData(){ + return this->data; +} diff --git a/src/IOFileClass/FileManIOFile.hpp b/src/IOFileClass/FileManIOFile.hpp new file mode 100644 index 0000000..221cf13 --- /dev/null +++ b/src/IOFileClass/FileManIOFile.hpp @@ -0,0 +1,133 @@ +/** + * @file FileManIOFile.hpp + * @brief FileManIOFile class definitions + * @author manzerbredes + * @date 9 Mars 2015 + * + * Contain all definitions of FileManIOFile class. + * + */ + + +#ifndef __FileManIOFile__ +#define __FileManIOFile__ + + +//----- 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 files. + * @author manzerbredes + * + * -----File organisation----- + * + * 16 first bytes : md5 of decrypted data + * Rest of the file : data encrypted (ASE for now) + * + * \bug Need check if file exist and can be opened + */ +class FileManIOFile { + + public: + + //Constructor + FileManIOFile(std::string filename); + + //Destructor + ~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, + * and key saved in key attribute (before been crypted with SHA-256 algorithm). + * + */ + void read(std::string key); + + + /** + * @brief Write data in encrypted file. + * + * @param key : key to encrypt data + * @param data : data to write + * + * 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 True if file fully decrypted. + * + * @return readable attribute + * + * Return "readable" attribute. + * + */ + bool isReadable(); + + + /** + * @brief Get data attribute. + * + * @return data attribute. + * + * **Warning** if data not fully decrypted (readable!=true), + * data will be unreadable (unparsable). + * + */ + std::string getData(); + + + private: + + /** + * @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); + + + //Attributes: + + AESCrypt aes; ///< AES instance + + HASHCrypt hash; ///< HASH instance + + std::string filename; ///< Filename attribute + + std::string data; ///< Data attribute + + bool readable; ///< Readable attribute + + byte key[32]; ///< Key in SHA-256 + +}; + +#endif diff --git a/src/ParserClass/AbstractIDManager.cpp b/src/ParserClass/AbstractIDManager.cpp new file mode 100644 index 0000000..e1c5e13 --- /dev/null +++ b/src/ParserClass/AbstractIDManager.cpp @@ -0,0 +1,39 @@ +/** + * @file AbstractIDManager.cpp + * @brief AbstractIDManager class definitions + * @author manzerbredes + * @date 11 Mars 2015 + * + * Contain all implémentations of AbstractIDManager class. + * + */ + +#include "AbstractIDManager.hpp" + +AbstractIDManager::AbstractIDManager(){ + this->id=this->generateId(); +} + +AbstractIDManager::AbstractIDManager(std::string id){ + this->id=id; +} +AbstractIDManager::~AbstractIDManager(){ + this->id=id; +} + + + +void AbstractIDManager::setId(std::string id){ + this->id = id; +} + +std::string AbstractIDManager::generateId(){ + boost::uuids::uuid uuid = boost::uuids::random_generator()(); + std::stringstream ss; + ss << uuid; + return ss.str(); +} + +std::string AbstractIDManager::getId() const{ + return this->id; +} diff --git a/src/ParserClass/AbstractIDManager.hpp b/src/ParserClass/AbstractIDManager.hpp new file mode 100644 index 0000000..67d5f6f --- /dev/null +++ b/src/ParserClass/AbstractIDManager.hpp @@ -0,0 +1,59 @@ +/** + * @file AbstractIDManager.hpp + * @brief AbstractIDManager class definitions + * @author manzerbredes + * @date 11 Mars 2015 + * + * Contain all definitions of AbstractIDManager class. + * If you want to manage id in class (like container), use + * this class as superclass. + * + */ + +//----- std ----- +#include <string> +#include <sstream> + + +//----- boost ----- +#include <boost/uuid/uuid.hpp> // uuid class +#include <boost/uuid/uuid_generators.hpp> // generators +#include <boost/uuid/uuid_io.hpp> // streaming operators etc. + + + + +/** + * @class AbstractIDManager AbstractIDManager.hpp "/CryptClass/AbstractIDManager.hpp" + * @brief Managing ID + * @author manzerbredes + * + * This class should not be instantiate directly. + * + */ + class AbstractIDManager{ + + + public: + //Constructor + AbstractIDManager(); + + //Constructor, init with id + AbstractIDManager(std::string); + + //Destructor + ~AbstractIDManager(); + + + //Getters and setters + std::string getId() const; + void setId(std::string id); + + + private: + //Generate and random id + std::string generateId(); + + std::string id; ///< String id attribute + + }; diff --git a/src/ParserClass/FileManContainer/Website.cpp b/src/ParserClass/FileManContainer/Website.cpp new file mode 100644 index 0000000..f65a66d --- /dev/null +++ b/src/ParserClass/FileManContainer/Website.cpp @@ -0,0 +1,67 @@ +/** + * @file Website.cpp + * @brief Website class definitions + * @author manzerbredes + * @date 11 Mars 2015 + * + * Contain all implementations of Website class. + * + */ + + +#include "Website.hpp" + + +//Constructor +Website::Website(){ + +} + + + +//----- Getters ----- +std::string Website::getTitle(){ + return this->title; +} +std::string Website::getUrl(){ + return this->url; +} +std::string Website::getUsername(){ + return this->username; +} +std::string Website::getPassword(){ + return this->password; +} +std::string Website::getDescription(){ + return this->description; +} + + + +//----- Setters ----- +void Website::setTitle(std::string title){ + this->title = title; +} +void Website::setUrl(std::string url){ + this->url = url; +} +void Website::setUsername(std::string username){ + this->username = username; +} +void Website::setPassword(std::string password){ + this->password = password; +} +void Website::setDescription(std::string description){ + this->description = description; +} + + + +//Equality comparator +bool Website::operator==(const Website& website) const{ + if((this->getId()).compare(website.getId())==0){ + return true; + } + return false; +} + diff --git a/src/ParserClass/FileManContainer/Website.hpp b/src/ParserClass/FileManContainer/Website.hpp new file mode 100644 index 0000000..cb293d4 --- /dev/null +++ b/src/ParserClass/FileManContainer/Website.hpp @@ -0,0 +1,83 @@ +/** + * @file Website.hpp + * @brief Website class definitions + * @author manzerbredes + * @date 11 Mars 2015 + * + * Contain all definitions of Website class. + * + */ + +#ifndef __WEBSITE__ +#define __WEBSITE__ + +#include <string> + + +/** + * @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) + * + */ + +#include <string> +#include "AbstractIDManager.hpp" + +/** + * @class Website Website.hpp "/ParserClass/FileManContainer/Website.hpp" + * @brief Class for quick open and close encrypted file. + * @author manzerbredes + * + * Container for website data. + * + */ +class Website : public AbstractIDManager { + + + public: + + Website(); + + /** + * @brief Containner getters. + */ + + std::string getTitle(); + std::string getUrl(); + std::string getUsername(); + std::string getPassword(); + std::string getDescription(); + + + /** + * @brief Containner setters. + */ + void setTitle(std::string title); + void setUrl(std::string url); + void setUsername(std::string username); + void setPassword(std::string password); + void setDescription(std::string description); + + + /** + * @brief Equality comparator + */ + bool operator==(const Website& website) const; + + private: + + std::string title; ///< Title of the website + std::string url; ///< Url of the website + std::string username; ///< username of the account + std::string password; ///< password of the account + std::string description; ///< Description of the website +}; + + +#endif diff --git a/src/ParserClass/FileManParser.cpp b/src/ParserClass/FileManParser.cpp new file mode 100644 index 0000000..57a485a --- /dev/null +++ b/src/ParserClass/FileManParser.cpp @@ -0,0 +1,132 @@ +/** + * @file FileManParser.cpp + * @brief FileManParser class definitions + * @author manzerbredes + * @date 11 Mars 2015 + * + * Contain all implementations of FileManParser class. + * + */ + + +#include "FileManParser.hpp" + + +FileManParser::FileManParser(std::string data){ + + //String to stringstream + (this->dataStream) << data; + + //Create parser + //parser.parse_stream(dataStream); + (this->parser).parse_file("Doxygen/doc.xml"); + + //Retrieve document + this->document=(this->parser).get_document(); + + //Init root Node + this->rootNode=(this->document)->get_root_node(); + + //Init container: + this->initWebsites(); + + + +} + + +std::string FileManParser::getDocument(){ + std::string data=(this->document)->write_to_string(); + return data; +} + + +std::vector<Website>* FileManParser::getWebsites(){ + return this->websites; +} + + + + +//----------------Container part------------------------ +void FileManParser::initWebsites(){ + this->websites=new std::vector<Website>; + + std::vector<xmlpp::Node*> websitesNodeSet=this->rootNode->find("//websites"); + this->websitesNode=websitesNodeSet.at(0); + + std::vector<xmlpp::Node*> websiteNodeSet=this->websitesNode->find("*"); + + for(int i=0;i<websiteNodeSet.size();i++){ + xmlpp::Node* current=websiteNodeSet.at(i); + xmlpp::Element* currentElement=(xmlpp::Element*)current; + + Website newWebsite; + newWebsite.setId(currentElement->get_attribute_value("id")); + + std::vector<xmlpp::Node*> websiteChildren=current->find("*"); + + for(int j=0;j<websiteChildren.size();j++){ + xmlpp::Element* currentChild=(xmlpp::Element*)websiteChildren.at(j); + + std::list<xmlpp::Node*> contentNodes=currentChild->get_children(); + xmlpp::CdataNode* cdataNode=(xmlpp::CdataNode*)contentNodes.front(); + std::string cdataContent=cdataNode->get_content(); + + + if(currentChild->get_name().compare("title")==0){ + newWebsite.setTitle(cdataContent); + } + else if(currentChild->get_name().compare("url")==0){ + newWebsite.setUrl(cdataContent); + } + else if(currentChild->get_name().compare("username")==0){ + newWebsite.setUsername(cdataContent); + } + else if(currentChild->get_name().compare("password")==0){ + newWebsite.setPassword(cdataContent); + } + else if(currentChild->get_name().compare("description")==0){ + newWebsite.setDescription(cdataContent); + } + } + this->websites->push_back(newWebsite); + } +} + + + + + + + + + +void FileManParser::updateParser(){ + this->rootNode->remove_child(this->websitesNode); + + xmlpp::Element* websitesNode=this->rootNode->add_child("websites"); + this->websitesNode=(xmlpp::Node*)websitesNode; + + for(int i=0;i<this->websites->size();i++){ + xmlpp::Element* current=this->websitesNode->add_child("website"); + Website currentWebsite=this->websites->at(i); + + current->set_attribute("id", currentWebsite.getId()); + xmlpp::Element* title=current->add_child("title"); + title->add_child_cdata(currentWebsite.getTitle()); + + xmlpp::Element* url=current->add_child("url"); + url->add_child_cdata(currentWebsite.getUrl()); + + xmlpp::Element* username=current->add_child("username"); + username->add_child_cdata(currentWebsite.getUsername()); + + xmlpp::Element* password=current->add_child("password"); + password->add_child_cdata(currentWebsite.getPassword()); + + xmlpp::Element* description=current->add_child("description"); + description->add_child_cdata(currentWebsite.getDescription()); + + } +} diff --git a/src/ParserClass/FileManParser.hpp b/src/ParserClass/FileManParser.hpp new file mode 100644 index 0000000..f0d01e5 --- /dev/null +++ b/src/ParserClass/FileManParser.hpp @@ -0,0 +1,114 @@ +/** + * @file FileManParser.hpp + * @brief FileManParser class definitions + * @author manzerbredes + * @date 11 Mars 2015 + * + * Contain all definitions of FileManParser class. + * + */ + + + +#ifndef __FileManParser__ +#define __FileManParser__ + + + +//----- std ----- +#include <iostream> +#include <sstream> +#include <string> +#include <vector> + +//----- class ----- +#include "Website.hpp" + +//----- libxml++ ----- +#include <cstdlib> +#include <libxml++/libxml++.h> + + + +/** + * @class FileManParser FileManParser.hpp "/ParserClass/FileManContainer/FileManParser.hpp" + * @brief Class for parser un xml file in a string. + * @author manzerbredes + * + * Parse string using libxml++ library. + * + */ +class FileManParser{ + + + public: + + //Constructor + FileManParser(std::string data); + + + /** + * @brief Get document in string + * + * @return a string that contain the document + * + * Return current document. + * To have an up-to-date document, please run updateParser() before. + * + */ + std::string getDocument(); + + + /** + * @brief Write data in encrypted file. + * + * @return vector pointer that point to the vector of website in document + * + * You can modified this vector, and for apply change run updateParser() + * + */ + std::vector<Website>* getWebsites(); + + + + /** + * @brief Update the parser + * + * Apply all modifications you have made on the vector object (example std::vector<Website>* websites). + * + */ + void updateParser(); + + + private: + + + /** + * @brief Instanciate websites vector + * + * Read the document and create all Website object and put them into + * the websites vector attribute. + * + */ + void initWebsites(); + + + //Parser attributes + std::stringstream dataStream; ///< Contain the document you want to parse + xmlpp::DomParser parser; ///< Contain the parser + + + + //Document attributes + xmlpp::Document* document; ///< Contain the document (generate by the parser and dataStream) + xmlpp::Node* rootNode; ///< Contain the root node of the document + + + //Website attributes + xmlpp::Node* websitesNode; ///< Contain the websites node of the document + std::vector<Website> *websites; ///< Contain all website of the document (you can modify it and run updateParser to apply all modifications). + +}; + + +#endif diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b2f344f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,80 @@ +/** + * @file main.cpp + * @brief Entry point + * @author manzerbredes + * @version Prototype + * @date 8 Mars 2015 + * + * Entry point of the application. + * + */ + + + + +//----- std ----- + +#include <iostream> +#include <string> +#include <vector> + + +//----- class ----- +#include "FileManIOFile.hpp" +#include "FileManParser.hpp" +#include "Website.hpp" + + +#include <gtk/gtk.h> + + +/** + * @fn int main(int argc, char *argv[]) + * @author manzerbredes + * @brief main function + * @param argc contain *argv[] length + * @param *argv[] contain the arguments list + * @return Return code, an int. + */ +int main(int argc, char *argv[]){ + + + + /* Initialisation de GTK+ */ + gtk_init(&argc, &argv); + + + GtkWidget* MainWindow=NULL; + + MainWindow=gtk_window_new(GTK_WINDOW_TOPLEVEL); + + g_signal_connect(G_OBJECT(MainWindow), "delete-event", G_CALLBACK(gtk_main_quit), NULL); + + + GtkWidget* bouton; + + bouton=gtk_button_new_with_label("Hello Bro :"); + gtk_container_add(GTK_CONTAINER(MainWindow), bouton); + + g_signal_connect(G_OBJECT(bouton), "leave", G_CALLBACK(gtk_main_quit), NULL); + + gtk_window_set_title(GTK_WINDOW(MainWindow), "forgetIt"); + gtk_window_set_default_size(GTK_WINDOW(MainWindow), 500,500); + + gtk_window_set_position(GTK_WINDOW(MainWindow), GTK_WIN_POS_CENTER); + + + + gtk_widget_show_all(MainWindow); + + + gtk_main(); + + + + return EXIT_SUCCESS; + +} + + + |
