summaryrefslogtreecommitdiff
path: root/src/IOFileClass/FileManIOFile.cpp
blob: ea49201a3e8b1df8861a7c0703aa1c73f6f5d9ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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;
}