libkazv
crypto-util.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include <libkazv-config.hpp>
9 
10 #include <string>
11 #include <random>
12 #include <algorithm>
13 #include <vector>
14 
15 #include <nlohmann/json.hpp>
16 
17 #include <boost/container_hash/hash.hpp>
18 
19 namespace Kazv
20 {
21  using ByteArray = std::vector<unsigned char>;
22 
24  {
25  std::string roomId;
26  std::string senderKey;
27  std::string sessionId;
28  };
29 
33  struct RandomTag {};
34 
35  using RandomData = std::string;
36 
37  inline void from_json(const nlohmann::json &j, KeyOfGroupSession &k)
38  {
39  k.roomId = j.at("roomId");
40  k.senderKey = j.at("senderKey");
41  k.sessionId = j.at("sessionId");
42  }
43 
44  inline void to_json(nlohmann::json &j, const KeyOfGroupSession &k)
45  {
46  j = nlohmann::json::object({
47  {"roomId", k.roomId},
48  {"senderKey", k.senderKey},
49  {"sessionId", k.sessionId},
50  });
51  }
52 
54  {
55  return a.roomId == b.roomId
56  && a.senderKey == b.senderKey
57  && a.sessionId == b.sessionId;
58  }
59 
61  {
62  std::string userId;
63  std::string deviceId;
64  };
65 
67  {
68  return a.userId == b.userId
69  && a.deviceId == b.deviceId;
70  };
71 
72  [[nodiscard]] inline ByteArray genRandom(int len)
73  {
74  auto rd = std::random_device{};
75  auto ret = ByteArray(len, '\0');
76  std::generate(ret.begin(), ret.end(), [&] { return rd(); });
77  return ret;
78  }
79 
80  [[nodiscard]] inline RandomData genRandomData(int len)
81  {
82  auto rd = std::random_device{};
83  auto ret = RandomData(len, '\0');
84  std::generate(ret.begin(), ret.end(), [&] { return rd(); });
85  return ret;
86  }
87 
88 
89  namespace CryptoConstants
90  {
91  inline const std::string ed25519{"ed25519"};
92  inline const std::string curve25519{"curve25519"};
93  inline const std::string signedCurve25519{"signed_curve25519"};
94 
95  inline const std::string olmAlgo{"m.olm.v1.curve25519-aes-sha2"};
96  inline const std::string megOlmAlgo{"m.megolm.v1.aes-sha2"};
97  }
98 }
99 
100 namespace std
101 {
102  template<> struct hash<Kazv::KeyOfGroupSession>
103  {
104  std::size_t operator()(const Kazv::KeyOfGroupSession & k) const noexcept {
105  std::size_t seed = 0;
106  boost::hash_combine(seed, k.roomId);
107  boost::hash_combine(seed, k.senderKey);
108  boost::hash_combine(seed, k.sessionId);
109  return seed;
110  }
111  };
112 
113  template<> struct hash<Kazv::KeyOfOutboundSession>
114  {
115  std::size_t operator()(const Kazv::KeyOfOutboundSession & k) const noexcept {
116  std::size_t seed = 0;
117  boost::hash_combine(seed, k.userId);
118  boost::hash_combine(seed, k.deviceId);
119  return seed;
120  }
121  };
122 }
Kazv::CryptoConstants::ed25519
const std::string ed25519
Definition: crypto-util.hpp:91
Kazv::genRandom
ByteArray genRandom(int len)
Definition: crypto-util.hpp:72
Kazv::RandomData
std::string RandomData
Definition: crypto-util.hpp:35
Kazv::KeyOfOutboundSession::deviceId
std::string deviceId
Definition: crypto-util.hpp:63
Kazv::KeyOfGroupSession::sessionId
std::string sessionId
Definition: crypto-util.hpp:27
Kazv::CryptoConstants::olmAlgo
const std::string olmAlgo
Definition: crypto-util.hpp:95
Kazv::operator==
bool operator==(BaseJob a, BaseJob b)
Definition: basejob.cpp:280
Kazv
Definition: location.hpp:10
Kazv::KeyOfOutboundSession
Definition: crypto-util.hpp:60
Kazv::KeyOfGroupSession
Definition: crypto-util.hpp:23
Kazv::CryptoConstants::curve25519
const std::string curve25519
Definition: crypto-util.hpp:92
Kazv::KeyOfOutboundSession::userId
std::string userId
Definition: crypto-util.hpp:62
Kazv::from_json
void from_json(const nlohmann::json &j, KeyOfGroupSession &k)
Definition: crypto-util.hpp:37
Kazv::json
nlohmann::json json
Definition: jsonwrap.hpp:20
Kazv::KeyOfGroupSession::roomId
std::string roomId
Definition: crypto-util.hpp:25
Kazv::genRandomData
RandomData genRandomData(int len)
Definition: crypto-util.hpp:80
Kazv::CryptoConstants::megOlmAlgo
const std::string megOlmAlgo
Definition: crypto-util.hpp:96
std::hash< Kazv::KeyOfOutboundSession >::operator()
std::size_t operator()(const Kazv::KeyOfOutboundSession &k) const noexcept
Definition: crypto-util.hpp:115
Kazv::to_json
void to_json(nlohmann::json &j, const KeyOfGroupSession &k)
Definition: crypto-util.hpp:44
Kazv::ByteArray
std::vector< unsigned char > ByteArray
Definition: crypto-util.hpp:21
std
Definition: clientutil.hpp:216
std::hash< Kazv::KeyOfGroupSession >::operator()
std::size_t operator()(const Kazv::KeyOfGroupSession &k) const noexcept
Definition: crypto-util.hpp:104
Kazv::KeyOfGroupSession::senderKey
std::string senderKey
Definition: crypto-util.hpp:26
Kazv::CryptoConstants::signedCurve25519
const std::string signedCurve25519
Definition: crypto-util.hpp:93
libkazv-config.hpp
Kazv::RandomTag
The tag to indicate that a constructor should use user-provided random data.
Definition: crypto-util.hpp:33