Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / digest32.h
blobb187f3bb4910ddb8fe8fbccf12db5e5be6a9fdb8
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015, 2021 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #pragma once
31 #include <libtorrent/sha1_hash.hpp>
33 #include <QtGlobal>
34 #include <QByteArray>
35 #include <QHash>
36 #include <QSharedData>
37 #include <QSharedDataPointer>
38 #include <QString>
40 template <int N>
41 class Digest32
43 public:
44 using UnderlyingType = lt::digest32<N>;
46 Digest32() = default;
47 Digest32(const Digest32 &other) = default;
48 Digest32(Digest32 &&other) = default;
50 Digest32(const UnderlyingType &nativeDigest)
51 : m_dataPtr {new Data(nativeDigest)}
55 static constexpr int length()
57 return UnderlyingType::size();
60 bool isValid() const
62 return m_dataPtr->isValid();
65 Digest32 &operator=(const Digest32 &other) = default;
66 Digest32 &operator=(Digest32 &&other) = default;
68 operator UnderlyingType() const
70 return m_dataPtr->nativeDigest();
73 QString toString() const
75 return m_dataPtr->hashString();
78 static Digest32 fromString(const QString &digestString)
80 return Digest32(QSharedDataPointer<Data>(new Data(digestString)));
83 private:
84 class Data;
86 explicit Digest32(QSharedDataPointer<Data> dataPtr)
87 : m_dataPtr {dataPtr}
91 QSharedDataPointer<Data> m_dataPtr {new Data};
94 template <int N>
95 class Digest32<N>::Data : public QSharedData
97 public:
98 Data() = default;
100 explicit Data(UnderlyingType nativeDigest)
101 : m_isValid {true}
102 , m_nativeDigest {nativeDigest}
106 explicit Data(const QString &digestString)
108 if (digestString.size() != (length() * 2))
109 return;
111 const QByteArray raw = QByteArray::fromHex(digestString.toLatin1());
112 if (raw.size() != length()) // QByteArray::fromHex() will skip over invalid characters
113 return;
115 m_isValid = true;
116 m_hashString = digestString;
117 m_nativeDigest.assign(raw.constData());
120 bool isValid() const { return m_isValid; }
121 UnderlyingType nativeDigest() const { return m_nativeDigest; }
123 QString hashString() const
125 if (m_hashString.isEmpty())
127 const QByteArray raw = QByteArray::fromRawData(m_nativeDigest.data(), length());
128 m_hashString = QString::fromLatin1(raw.toHex());
131 return m_hashString;
134 private:
135 bool m_isValid = false;
136 UnderlyingType m_nativeDigest;
137 mutable QString m_hashString;
140 template <int N>
141 bool operator==(const Digest32<N> &left, const Digest32<N> &right)
143 return (static_cast<typename Digest32<N>::UnderlyingType>(left)
144 == static_cast<typename Digest32<N>::UnderlyingType>(right));
147 template <int N>
148 bool operator!=(const Digest32<N> &left, const Digest32<N> &right)
150 return !(left == right);
153 template <int N>
154 bool operator<(const Digest32<N> &left, const Digest32<N> &right)
156 return static_cast<typename Digest32<N>::UnderlyingType>(left)
157 < static_cast<typename Digest32<N>::UnderlyingType>(right);
160 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
161 template <int N>
162 std::size_t qHash(const Digest32<N> &key, const std::size_t seed = 0)
164 return ::qHash(static_cast<typename Digest32<N>::UnderlyingType>(key), seed);
166 #else
167 template <int N>
168 uint qHash(const Digest32<N> &key, const uint seed = 0)
170 return static_cast<uint>((std::hash<typename Digest32<N>::UnderlyingType> {})(key)) ^ seed;
172 #endif