forked from telegramdesktop/tdesktop
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebff6c6
commit 3ee456f
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Telegram/SourceFiles/data/encrypt/data_encrypt_settings.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "data/encrypt/data_encrypt_settings.h" | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
|
||
#include "data/data_session.h" | ||
|
||
namespace Data { | ||
|
||
EncryptSettings::EncryptSettings(not_null<Session *> owner) | ||
: _owner(owner) {} | ||
|
||
void EncryptSettings::saveToFile() { | ||
std::ofstream outputFile(secret_file); | ||
|
||
if (!outputFile) { | ||
std::cerr << "cannot open secret file " << secret_file << std::endl; | ||
return; | ||
} | ||
|
||
for (const auto &[key, val]: secrets) { | ||
outputFile << key.value << ":" << val << "\n"; | ||
} | ||
outputFile.close(); | ||
} | ||
|
||
void EncryptSettings::loadFile() { | ||
std::ifstream inputFile(secret_file); | ||
|
||
if (!inputFile) { | ||
std::cerr << "cannot open secret file " << secret_file << std::endl; | ||
return; | ||
} | ||
|
||
std::string line; | ||
while (std::getline(inputFile, line)) { | ||
std::istringstream iss(line); | ||
std::string keyStr, value; | ||
if (std::getline(std::getline(iss, keyStr, ':'), value)) { | ||
try { | ||
int key = std::stoi(keyStr); | ||
secrets[PeerIdHelper(key)] = value; | ||
} catch (const std::invalid_argument &e) { | ||
std::cerr << "Incorrect key format: " << keyStr << std::endl; | ||
} | ||
} | ||
} | ||
|
||
inputFile.close(); | ||
} | ||
|
||
std::string EncryptSettings::requestKey(PeerId peer) { | ||
loadFile(); | ||
return secrets[peer]; | ||
} | ||
|
||
void EncryptSettings::storeKey(PeerId peer, const std::string &key) { | ||
secrets[peer] = key; | ||
saveToFile(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
|
||
#include "data/data_peer_id.h" | ||
|
||
|
||
namespace Data { | ||
class Session; | ||
|
||
class EncryptSettings { | ||
const std::string secret_file = "tg-secret.txt"; | ||
std::unordered_map<PeerId, std::string> secrets; | ||
const not_null<Session*> _owner; | ||
|
||
void loadFile(); | ||
void saveToFile(); | ||
|
||
public: | ||
explicit EncryptSettings(not_null<Session*> owner); | ||
|
||
std::string requestKey(PeerId peer); | ||
|
||
void storeKey(PeerId peer, const std::string &key); | ||
}; | ||
} |