signed save

This commit is contained in:
Thomas 2022-01-03 17:21:18 +01:00
parent ccdb001b9d
commit 3beb1a4d77
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
2 changed files with 10 additions and 9 deletions

View File

@ -1,3 +1,6 @@
11083679872916328441
13363418835389185581
thomas2,2
thomas2,2
thomas2,2
thomas2,2
thomas2,2
thomas,1

View File

@ -21,7 +21,7 @@ static std::hash<string> hasher;
bool verifyHash(size_t savedHash, string& content){
// non-cryptographic hash, but it is part of the std, and openssl is REALLY difficult
// to use in C++ while keeping semantic, because there are no wrappers...
int actualHash = hasher(content+SECRET_KEY);
size_t actualHash = hasher(content+SECRET_KEY);
return actualHash==savedHash;
}
@ -35,6 +35,7 @@ void ScoresManager::readFile() {
*/
size_t hash;
ifs >> hash;
ifs.get(); // remove \n
string content;
readWholeFile(ifs, content);
@ -44,14 +45,12 @@ void ScoresManager::readFile() {
stringstream ss(content);
string line;
Score s;
while(true){
getline(ss, line);
if(ss.eof())break;
size_t index = line.find(',');
s.name = line.substr(0, index);
s.points = stoi(line.substr(index));
scores.emplace_back(line.substr(0, index), stoi(line.substr(index+1)));
}
}else{
cerr << "Integrity check of the save file failed. Has it been tampered ?" << endl;
@ -61,8 +60,7 @@ void ScoresManager::readFile() {
void ScoresManager::writeFile() {
ofstream ofs(SCORE_FILE);
string str;
cout << scores.size() << endl;
string str; // this one must be counted in the hash too
for(Score& sc : scores){
str.append(sc.name);
str.append(",");
@ -70,7 +68,7 @@ void ScoresManager::writeFile() {
str.append("\n");
}
ofs << hasher(str) << endl << str;
ofs << hasher(str+SECRET_KEY) << endl << str;
}
/**