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 thomas2,2
thomas,1

View File

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