Remove incorrect assertions
[qBittorrent.git] / src / base / digest32.h
blob3aaf27c35c429deb8bbf7454530760a55c51c3d0
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 <QByteArray>
34 #include <QHash>
35 #include <QSharedData>
36 #include <QSharedDataPointer>
37 #include <QString>
39 template <int N>
40 class Digest32
42 public:
43 using UnderlyingType = lt::digest32<N>;
45 Digest32() = default;
46 Digest32(const Digest32 &other) = default;
47 Digest32(Digest32 &&other) noexcept = default;
49 Digest32(const UnderlyingType &nativeDigest)
50 : m_dataPtr {new Data(nativeDigest)}
54 static constexpr int length()
56 return UnderlyingType::size();
59 bool isValid() const
61 return m_dataPtr->isValid();
64 Digest32 &operator=(const Digest32 &other) = default;
65 Digest32 &operator=(Digest32 &&other) noexcept = default;
67 operator UnderlyingType() const
69 return m_dataPtr->nativeDigest();
72 QString toString() const
74 return m_dataPtr->hashString();
77 static Digest32 fromString(const QString &digestString)
79 return Digest32(QSharedDataPointer<Data>(new Data(digestString)));
82 private:
83 class Data;
85 explicit Digest32(QSharedDataPointer<Data> dataPtr)
86 : m_dataPtr {std::move(dataPtr)}
90 QSharedDataPointer<Data> m_dataPtr {new Data};
93 template <int N>
94 class Digest32<N>::Data : public QSharedData
96 public:
97 Data() = default;
99 explicit Data(UnderlyingType nativeDigest)
100 : m_isValid {true}
101 , m_nativeDigest {nativeDigest}
105 explicit Data(const QString &digestString)
107 if (digestString.size() != (length() * 2))
108 return;
110 const QByteArray raw = QByteArray::fromHex(digestString.toLatin1());
111 if (raw.size() != length()) // QByteArray::fromHex() will skip over invalid characters
112 return;
114 m_isValid = true;
115 m_hashString = digestString;
116 m_nativeDigest.assign(raw.constData());
119 bool isValid() const { return m_isValid; }
120 UnderlyingType nativeDigest() const { return m_nativeDigest; }
122 QString hashString() const
124 if (m_hashString.isEmpty() && isValid())
126 const QByteArray raw = QByteArray::fromRawData(m_nativeDigest.data(), length());
127 m_hashString = QString::fromLatin1(raw.toHex());
130 return m_hashString;
133 private:
134 bool m_isValid = false;
135 UnderlyingType m_nativeDigest;
136 mutable QString m_hashString;
139 template <int N>
140 bool operator==(const Digest32<N> &left, const Digest32<N> &right)
142 return (static_cast<typename Digest32<N>::UnderlyingType>(left)
143 == static_cast<typename Digest32<N>::UnderlyingType>(right));
146 template <int N>
147 bool operator<(const Digest32<N> &left, const Digest32<N> &right)
149 return static_cast<typename Digest32<N>::UnderlyingType>(left)
150 < static_cast<typename Digest32<N>::UnderlyingType>(right);
153 template <int N>
154 std::size_t qHash(const Digest32<N> &key, const std::size_t seed = 0)
156 return ::qHash(static_cast<typename Digest32<N>::UnderlyingType>(key), seed);