1// SPDX-FileCopyrightText: 2017 Kitsune Ral <kitsune-ral@users.sf.net>
2// SPDX-FileCopyrightText: 2019 Alexey Andreyev <aa13q@ya.ru>
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#include "encryptionevent.h"
6
7#include "../logging_categories_p.h"
8
9#include "../e2ee/e2ee_common.h"
10
11using namespace Quotient;
12
13static constexpr std::array encryptionStrings { MegolmV1AesSha2AlgoKey };
14
15template <>
16EncryptionType Quotient::fromJson(const QJsonValue& jv)
17{
18 const auto& encryptionString = jv.toString();
19 for (auto it = encryptionStrings.begin(); it != encryptionStrings.end();
20 ++it)
21 if (encryptionString == *it)
22 return EncryptionType(it - encryptionStrings.begin());
23
24 if (!encryptionString.isEmpty())
25 qCWarning(EVENTS) << "Unknown EncryptionType: " << encryptionString;
26 return EncryptionType::Undefined;
27}
28
29EncryptionEventContent::EncryptionEventContent(const QJsonObject& json)
30 : encryption(fromJson<Quotient::EncryptionType>(jv: json[AlgorithmKeyL]))
31 , algorithm(sanitized(plainText: json[AlgorithmKeyL].toString()))
32{
33 // NB: fillFromJson only fills the variable if the JSON key exists
34 fillFromJson<int>(jv: json[RotationPeriodMsKeyL], pod&: rotationPeriodMs);
35 fillFromJson<int>(jv: json[RotationPeriodMsgsKeyL], pod&: rotationPeriodMsgs);
36}
37
38EncryptionEventContent::EncryptionEventContent(Quotient::EncryptionType et)
39 : encryption(et)
40{
41 if(encryption != Quotient::EncryptionType::Undefined) {
42 algorithm = encryptionStrings[static_cast<size_t>(encryption)];
43 }
44}
45
46QJsonObject EncryptionEventContent::toJson() const
47{
48 QJsonObject o;
49 if (encryption != Quotient::EncryptionType::Undefined)
50 o.insert(key: AlgorithmKey, value: algorithm);
51 o.insert(key: RotationPeriodMsKey, value: rotationPeriodMs);
52 o.insert(key: RotationPeriodMsgsKey, value: rotationPeriodMsgs);
53 return o;
54}
55