-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWriter.h
83 lines (69 loc) · 2.7 KB
/
FileWriter.h
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
//==================================================================================
// Name : FileWriter.h
// Author : Julien Thibodeau
// Description : Declaration for FileWriter.cpp in C++, Ansi-style
//==================================================================================
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <unistd.h>
#include <experimental/filesystem>
#include <iostream>
#include <sys/stat.h>
#include "cryptopp/modes.h"
#include "cryptopp/aes.h"
#include "cryptopp/osrng.h"
#include "cryptopp/hex.h"
#include "cryptopp/hkdf.h"
#include "cryptopp/pssr.h"
#include <jsoncpp/json/json.h>
inline bool isFileExistWriter(const std::string &name)
{
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
std::string getCurrentDirWriter()
{
char buff[FILENAME_MAX];
getcwd(buff, FILENAME_MAX);
std::string currentWorkingDir(buff);
return currentWorkingDir;
}
struct SHA512keysWriter
{
std::string key;
std::string iv;
};
struct DataInfosJsonRowWriter
{
std::string url;
std::string user;
std::string pwd;
};
class FileWriter
{
private:
inline static const std::string ivFile = "D04BF8250278335D017A5FE311151DE2";
inline static const std::string pathFile = getCurrentDirWriter() + "/accounts.encrypted";
std::string aesCTREncrypt(std::string KEY, std::string IV, std::string infoToEncrypt);
std::string aesCTRDecrypt(std::string KEY, std::string IV, std::string infoToDecrypt);
std::string aesCBCEncrypt(std::string KEY, std::string IV, std::string infoToEncrypt);
std::string aesCBCDecrypt(std::string KEY, std::string IV, std::string infoToDecrypt);
std::string addEntryToJsonString(std::string JsonString, DataInfosJsonRowWriter dataInfosJsonRow, SHA512keysWriter keys);
std::string deletedRowToJsonString(std::string JsonString, int lineToSupress);
std::string DecryptJsonFile(SHA512keysWriter sha512keys);
SHA512keysWriter generateKeyIv(std::string pwdContent);
SHA512keysWriter generateKeyFromPwdAndIv(std::string pwdContent, std::string encodedIv);
void initiateJsonFile(std::string pwdFile);
void saveEncryptedData(std::string EncryptedData);
public:
FileWriter();
~FileWriter();
bool validatepwdFile(std::string pwdFile);
bool validatepwdContent(std::string pwdFile, std::string pwdContent);
void changepwdFile(std::string oldPwdFile, std::string newPwdFile);
void changepwdContent(std::string pwdFile, std::string oldPwdContent, std::string newPwdContent);
void saveEntryInfos(DataInfosJsonRowWriter entryInfos, std::string pwdFile, std::string pwdContent);
void saveDeletedRowEntryInfos(int lineToSupress, std::string pwdFile);
};