qBittorrent
digest32.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2015, 2021 Vladimir Golovnev <glassez@yandex.ru>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #pragma once
30 
31 #include <libtorrent/sha1_hash.hpp>
32 
33 #include <QByteArray>
34 #include <QHash>
35 #include <QString>
36 
37 template <int N>
38 class Digest32
39 {
40 public:
41  using UnderlyingType = lt::digest32<N>;
42 
43  Digest32() = default;
44 
45  Digest32(const UnderlyingType &nativeDigest)
46  : m_valid {true}
47  , m_nativeDigest {nativeDigest}
48  {
49  const QByteArray raw = QByteArray::fromRawData(nativeDigest.data(), length());
50  m_hashString = QString::fromLatin1(raw.toHex());
51  }
52 
53  static constexpr int length()
54  {
55  return UnderlyingType::size();
56  }
57 
58  bool isValid() const
59  {
60  return m_valid;
61  }
62 
63  operator UnderlyingType() const
64  {
65  return m_nativeDigest;
66  }
67 
68  static Digest32 fromString(const QString &digestString)
69  {
70  if (digestString.size() != (length() * 2))
71  return {};
72 
73  const QByteArray raw = QByteArray::fromHex(digestString.toLatin1());
74  if (raw.size() != length()) // QByteArray::fromHex() will skip over invalid characters
75  return {};
76 
77  Digest32 result;
78  result.m_valid = true;
79  result.m_hashString = digestString;
80  result.m_nativeDigest.assign(raw.constData());
81 
82  return result;
83  }
84 
85  QString toString() const
86  {
87  return m_hashString;
88  }
89 
90 private:
91  bool m_valid = false;
93  QString m_hashString;
94 };
95 
96 template <int N>
97 bool operator==(const Digest32<N> &left, const Digest32<N> &right)
98 {
99  return (static_cast<typename Digest32<N>::UnderlyingType>(left)
100  == static_cast<typename Digest32<N>::UnderlyingType>(right));
101 }
102 
103 template <int N>
104 bool operator!=(const Digest32<N> &left, const Digest32<N> &right)
105 {
106  return !(left == right);
107 }
108 
109 template <int N>
110 bool operator<(const Digest32<N> &left, const Digest32<N> &right)
111 {
112  return static_cast<typename Digest32<N>::UnderlyingType>(left)
113  < static_cast<typename Digest32<N>::UnderlyingType>(right);
114 }
115 
116 template <int N>
117 uint qHash(const Digest32<N> &key, const uint seed)
118 {
119  return ::qHash(std::hash<typename Digest32<N>::UnderlyingType>()(key), seed);
120 }
static Digest32 fromString(const QString &digestString)
Definition: digest32.h:68
bool m_valid
Definition: digest32.h:91
static constexpr int length()
Definition: digest32.h:53
UnderlyingType m_nativeDigest
Definition: digest32.h:92
lt::digest32< N > UnderlyingType
Definition: digest32.h:41
QString toString() const
Definition: digest32.h:85
Digest32()=default
bool isValid() const
Definition: digest32.h:58
Digest32(const UnderlyingType &nativeDigest)
Definition: digest32.h:45
QString m_hashString
Definition: digest32.h:93
uint qHash(const Digest32< N > &key, const uint seed)
Definition: digest32.h:117
bool operator==(const Digest32< N > &left, const Digest32< N > &right)
Definition: digest32.h:97
bool operator<(const Digest32< N > &left, const Digest32< N > &right)
Definition: digest32.h:110
bool operator!=(const Digest32< N > &left, const Digest32< N > &right)
Definition: digest32.h:104