Skip to content

Commit

Permalink
add encrypt file keys lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
astrophysik committed Nov 6, 2024
1 parent ebff6c6 commit 3ee456f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions Telegram/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ PRIVATE
data/components/sponsored_messages.h
data/components/top_peers.cpp
data/components/top_peers.h
data/encrypt/data_encrypt_settings.cpp
data/notify/data_notify_settings.cpp
data/notify/data_notify_settings.h
data/notify/data_peer_notify_settings.cpp
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/data/data_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ For license and copyright information please follow this link:
#include "data/components/sponsored_messages.h"
#include "data/stickers/data_stickers.h"
#include "data/notify/data_notify_settings.h"
#include "data/encrypt/data_encrypt_settings.h"
#include "data/data_bot_app.h"
#include "data/data_changes.h"
#include "data/data_group_call.h"
Expand Down Expand Up @@ -243,6 +244,7 @@ Session::Session(not_null<Main::Session*> session)
, _emojiStatuses(std::make_unique<EmojiStatuses>(this))
, _forumIcons(std::make_unique<ForumIcons>(this))
, _notifySettings(std::make_unique<NotifySettings>(this))
, _encryptSettings(std::make_unique<EncryptSettings>(this))
, _customEmojiManager(std::make_unique<CustomEmojiManager>(this))
, _stories(std::make_unique<Stories>(this))
, _savedMessages(std::make_unique<SavedMessages>(this))
Expand Down
5 changes: 5 additions & 0 deletions Telegram/SourceFiles/data/data_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class PhotoMedia;
class Stickers;
class GroupCall;
class NotifySettings;
class EncryptSettings;
class CustomEmojiManager;
class Stories;
class SavedMessages;
Expand Down Expand Up @@ -158,6 +159,9 @@ class Session final {
[[nodiscard]] NotifySettings &notifySettings() const {
return *_notifySettings;
}
[[nodiscard]] EncryptSettings &encryptSettings() const {
return *_encryptSettings;
}
[[nodiscard]] CustomEmojiManager &customEmojiManager() const {
return *_customEmojiManager;
}
Expand Down Expand Up @@ -1127,6 +1131,7 @@ class Session final {
const std::unique_ptr<EmojiStatuses> _emojiStatuses;
const std::unique_ptr<ForumIcons> _forumIcons;
const std::unique_ptr<NotifySettings> _notifySettings;
const std::unique_ptr<EncryptSettings> _encryptSettings;
const std::unique_ptr<CustomEmojiManager> _customEmojiManager;
const std::unique_ptr<Stories> _stories;
const std::unique_ptr<SavedMessages> _savedMessages;
Expand Down
61 changes: 61 additions & 0 deletions Telegram/SourceFiles/data/encrypt/data_encrypt_settings.cpp
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();
}
}
26 changes: 26 additions & 0 deletions Telegram/SourceFiles/data/encrypt/data_encrypt_settings.h
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);
};
}

0 comments on commit 3ee456f

Please sign in to comment.