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 sessionId;
27  };
28 
32  struct RandomTag {};
33 
34  using RandomData = std::string;
35 
36  inline void from_json(const nlohmann::json &j, KeyOfGroupSession &k)
37  {
38  k.roomId = j.at("roomId");
39  k.sessionId = j.at("sessionId");
40  }
41 
42  inline void to_json(nlohmann::json &j, const KeyOfGroupSession &k)
43  {
44  j = nlohmann::json::object({
45  {"roomId", k.roomId},
46  {"sessionId", k.sessionId},
47  });
48  }
49 
51  {
52  return a.roomId == b.roomId
53  && a.sessionId == b.sessionId;
54  }
55 
57  {
58  std::string userId;
59  std::string deviceId;
60  };
61 
63  {
64  return a.userId == b.userId
65  && a.deviceId == b.deviceId;
66  };
67 
68  [[nodiscard]] inline ByteArray genRandom(int len)
69  {
70  auto rd = std::random_device{};
71  auto ret = ByteArray(len, '\0');
72  std::generate(ret.begin(), ret.end(), [&] { return rd(); });
73  return ret;
74  }
75 
76  [[nodiscard]] inline RandomData genRandomData(int len)
77  {
78  auto rd = std::random_device{};
79  auto ret = RandomData(len, '\0');
80  std::generate(ret.begin(), ret.end(), [&] { return rd(); });
81  return ret;
82  }
83 
84 
85  namespace CryptoConstants
86  {
87  inline const std::string ed25519{"ed25519"};
88  inline const std::string curve25519{"curve25519"};
89  inline const std::string signedCurve25519{"signed_curve25519"};
90 
91  inline const std::string olmAlgo{"m.olm.v1.curve25519-aes-sha2"};
92  inline const std::string megOlmAlgo{"m.megolm.v1.aes-sha2"};
93  }
94 }
95 
96 namespace std
97 {
98  template<> struct hash<Kazv::KeyOfGroupSession>
99  {
100  std::size_t operator()(const Kazv::KeyOfGroupSession & k) const noexcept {
101  std::size_t seed = 0;
102  boost::hash_combine(seed, k.roomId);
103  boost::hash_combine(seed, k.sessionId);
104  return seed;
105  }
106  };
107 
108  template<> struct hash<Kazv::KeyOfOutboundSession>
109  {
110  std::size_t operator()(const Kazv::KeyOfOutboundSession & k) const noexcept {
111  std::size_t seed = 0;
112  boost::hash_combine(seed, k.userId);
113  boost::hash_combine(seed, k.deviceId);
114  return seed;
115  }
116  };
117 }
Kazv::CryptoConstants::ed25519
const std::string ed25519
Definition: crypto-util.hpp:87
Kazv::genRandom
ByteArray genRandom(int len)
Definition: crypto-util.hpp:68
Kazv::RandomData
std::string RandomData
Definition: crypto-util.hpp:34
Kazv::KeyOfOutboundSession::deviceId
std::string deviceId
Definition: crypto-util.hpp:59
Kazv::KeyOfGroupSession::sessionId
std::string sessionId
Definition: crypto-util.hpp:26
Kazv::CryptoConstants::olmAlgo
const std::string olmAlgo
Definition: crypto-util.hpp:91
Kazv::operator==
bool operator==(BaseJob a, BaseJob b)
Definition: basejob.cpp:280
Kazv
Definition: location.hpp:10
Kazv::KeyOfOutboundSession
Definition: crypto-util.hpp:56
Kazv::KeyOfGroupSession
Definition: crypto-util.hpp:23
Kazv::CryptoConstants::curve25519
const std::string curve25519
Definition: crypto-util.hpp:88
Kazv::KeyOfOutboundSession::userId
std::string userId
Definition: crypto-util.hpp:58
Kazv::from_json
void from_json(const nlohmann::json &j, KeyOfGroupSession &k)
Definition: crypto-util.hpp:36
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:76
Kazv::CryptoConstants::megOlmAlgo
const std::string megOlmAlgo
Definition: crypto-util.hpp:92
std::hash< Kazv::KeyOfOutboundSession >::operator()
std::size_t operator()(const Kazv::KeyOfOutboundSession &k) const noexcept
Definition: crypto-util.hpp:110
Kazv::to_json
void to_json(nlohmann::json &j, const KeyOfGroupSession &k)
Definition: crypto-util.hpp:42
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:100
Kazv::CryptoConstants::signedCurve25519
const std::string signedCurve25519
Definition: crypto-util.hpp:89
libkazv-config.hpp
Kazv::RandomTag
The tag to indicate that a constructor should use user-provided random data.
Definition: crypto-util.hpp:32