MCPcopy Create free account
hub / github.com/TelegramMessenger/cocoon / main

Function main

runners/EncryptMessage.cpp:11–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9#include <ostream>
10
11int main(int argc, char **argv) {
12 SET_VERBOSITY_LEVEL(verbosity_INFO);
13
14 bool decrypt_mode_ = false;
15 std::string private_key_file_;
16 td::Bits256 public_key = td::Bits256::zero();
17
18 td::set_default_failure_signal_handler().ensure();
19
20 td::OptionParser option_parser;
21 option_parser.add_option('d', "decrypt", "decrypt mode", [&]() { decrypt_mode_ = true; });
22 option_parser.add_option('k', "private-key", "private key file", [&](td::Slice v) { private_key_file_ = v.str(); });
23 option_parser.add_checked_option('p', "public-key", "public key", [&](td::Slice v) {
24 TRY_RESULT_ASSIGN(public_key, cocoon::parse_bits256_from_json(v));
25 return td::Status::OK();
26 });
27 option_parser.run(argc, argv, 0).ensure();
28
29 td::Bits256 private_key;
30 {
31 auto R = td::read_file_str(private_key_file_);
32 if (R.is_error()) {
33 LOG(FATAL) << "cannot read file '" << private_key_file_ << "'";
34 }
35 auto res = R.move_as_ok();
36 if (res.size() != 32) {
37 LOG(FATAL) << "private key in '" << private_key_file_ << "' must be 32 bytes long";
38 }
39 private_key.as_slice().copy_from(res);
40 }
41
42 std::string input{std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>()};
43 std::stringstream ss(input);
44 while (true) {
45 try {
46 nlohmann::json v;
47 ss >> v;
48 if (decrypt_mode_) {
49 auto S = cocoon::decrypt_json(v, private_key, public_key, true, false);
50 if (S.is_error()) {
51 LOG(ERROR) << "failed to decrypt: " << S;
52 }
53 } else {
54 cocoon::encrypt_json(v, private_key, public_key, true);
55 }
56 std::cout << v.dump() << "\n" << std::flush;
57 } catch (...) {
58 break;
59 }
60 }
61 return 0;
62}

Callers

nothing calls this directly

Calls 5

parse_bits256_from_jsonFunction · 0.85
decrypt_jsonFunction · 0.85
encrypt_jsonFunction · 0.85
as_sliceMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected