libkazv
Loading...
Searching...
No Matches
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
19namespace Kazv
20{
21 using ByteArray = std::vector<unsigned char>;
22
24 {
25 std::string roomId;
26 std::string sessionId;
27 friend bool operator==(const KeyOfGroupSession &a, const KeyOfGroupSession &b) = default;
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.sessionId = j.at("sessionId");
41 }
42
43 inline void to_json(nlohmann::json &j, const KeyOfGroupSession &k)
44 {
45 j = nlohmann::json::object({
46 {"roomId", k.roomId},
47 {"sessionId", k.sessionId},
48 });
49 }
50
52 {
53 std::string userId;
54 std::string deviceId;
55 friend bool operator==(const KeyOfOutboundSession &a, const KeyOfOutboundSession &b) = default;
56 };
57
58 [[nodiscard]] inline ByteArray genRandom(int len)
59 {
60 auto rd = std::random_device{};
61 auto ret = ByteArray(len, '\0');
62 std::generate(ret.begin(), ret.end(), [&] { return rd(); });
63 return ret;
64 }
65
66 [[nodiscard]] inline RandomData genRandomData(int len)
67 {
68 auto rd = std::random_device{};
69 auto ret = RandomData(len, '\0');
70 std::generate(ret.begin(), ret.end(), [&] { return rd(); });
71 return ret;
72 }
73
74
75 namespace CryptoConstants
76 {
77 inline const std::string ed25519{"ed25519"};
78 inline const std::string curve25519{"curve25519"};
79 inline const std::string signedCurve25519{"signed_curve25519"};
80
81 inline const std::string olmAlgo{"m.olm.v1.curve25519-aes-sha2"};
82 inline const std::string megOlmAlgo{"m.megolm.v1.aes-sha2"};
83 }
84}
85
86namespace std
87{
88 template<> struct hash<Kazv::KeyOfGroupSession>
89 {
90 std::size_t operator()(const Kazv::KeyOfGroupSession & k) const noexcept {
91 std::size_t seed = 0;
92 boost::hash_combine(seed, k.roomId);
93 boost::hash_combine(seed, k.sessionId);
94 return seed;
95 }
96 };
97
98 template<> struct hash<Kazv::KeyOfOutboundSession>
99 {
100 std::size_t operator()(const Kazv::KeyOfOutboundSession & k) const noexcept {
101 std::size_t seed = 0;
102 boost::hash_combine(seed, k.userId);
103 boost::hash_combine(seed, k.deviceId);
104 return seed;
105 }
106 };
107}
const std::string ed25519
Definition crypto-util.hpp:77
const std::string curve25519
Definition crypto-util.hpp:78
const std::string olmAlgo
Definition crypto-util.hpp:81
const std::string megOlmAlgo
Definition crypto-util.hpp:82
const std::string signedCurve25519
Definition crypto-util.hpp:79
Definition location.hpp:10
ByteArray genRandom(int len)
Definition crypto-util.hpp:58
std::string RandomData
Definition crypto-util.hpp:35
nlohmann::json json
Definition jsonwrap.hpp:20
RandomData genRandomData(int len)
Definition crypto-util.hpp:66
void to_json(nlohmann::json &j, const KeyOfGroupSession &k)
Definition crypto-util.hpp:43
void from_json(const nlohmann::json &j, KeyOfGroupSession &k)
Definition crypto-util.hpp:37
std::vector< unsigned char > ByteArray
Definition crypto-util.hpp:21
Definition clientutil.hpp:140
Definition crypto-util.hpp:24
std::string sessionId
Definition crypto-util.hpp:26
std::string roomId
Definition crypto-util.hpp:25
friend bool operator==(const KeyOfGroupSession &a, const KeyOfGroupSession &b)=default
Definition crypto-util.hpp:52
std::string userId
Definition crypto-util.hpp:53
std::string deviceId
Definition crypto-util.hpp:54
friend bool operator==(const KeyOfOutboundSession &a, const KeyOfOutboundSession &b)=default
The tag to indicate that a constructor should use user-provided random data.
Definition crypto-util.hpp:33
std::size_t operator()(const Kazv::KeyOfGroupSession &k) const noexcept
Definition crypto-util.hpp:90
std::size_t operator()(const Kazv::KeyOfOutboundSession &k) const noexcept
Definition crypto-util.hpp:100