mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-02-02 18:04:32 +00:00
Define template for classes that represent SHA hashes
This commit is contained in:
parent
8cfd803222
commit
bea32cfe38
@ -32,6 +32,7 @@ add_library(qbt_base STATIC
|
|||||||
bittorrent/torrentinfo.h
|
bittorrent/torrentinfo.h
|
||||||
bittorrent/tracker.h
|
bittorrent/tracker.h
|
||||||
bittorrent/trackerentry.h
|
bittorrent/trackerentry.h
|
||||||
|
digest32.h
|
||||||
exceptions.h
|
exceptions.h
|
||||||
filesystemwatcher.h
|
filesystemwatcher.h
|
||||||
global.h
|
global.h
|
||||||
|
@ -31,6 +31,7 @@ HEADERS += \
|
|||||||
$$PWD/bittorrent/torrentinfo.h \
|
$$PWD/bittorrent/torrentinfo.h \
|
||||||
$$PWD/bittorrent/tracker.h \
|
$$PWD/bittorrent/tracker.h \
|
||||||
$$PWD/bittorrent/trackerentry.h \
|
$$PWD/bittorrent/trackerentry.h \
|
||||||
|
$$PWD/digest32.h \
|
||||||
$$PWD/exceptions.h \
|
$$PWD/exceptions.h \
|
||||||
$$PWD/filesystemwatcher.h \
|
$$PWD/filesystemwatcher.h \
|
||||||
$$PWD/global.h \
|
$$PWD/global.h \
|
||||||
|
@ -28,70 +28,14 @@
|
|||||||
|
|
||||||
#include "infohash.h"
|
#include "infohash.h"
|
||||||
|
|
||||||
#include <QByteArray>
|
const int InfoHashTypeId = qRegisterMetaType<BitTorrent::InfoHash>();
|
||||||
#include <QHash>
|
|
||||||
|
|
||||||
using namespace BitTorrent;
|
BitTorrent::InfoHash BitTorrent::InfoHash::fromString(const QString &hashString)
|
||||||
|
|
||||||
const int InfoHashTypeId = qRegisterMetaType<InfoHash>();
|
|
||||||
|
|
||||||
InfoHash::InfoHash(const lt::sha1_hash &nativeHash)
|
|
||||||
: m_valid {true}
|
|
||||||
, m_nativeHash {nativeHash}
|
|
||||||
{
|
{
|
||||||
const QByteArray raw = QByteArray::fromRawData(nativeHash.data(), length());
|
return {SHA1Hash::fromString(hashString)};
|
||||||
m_hashString = QString::fromLatin1(raw.toHex());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InfoHash::isValid() const
|
uint BitTorrent::qHash(const BitTorrent::InfoHash &key, const uint seed)
|
||||||
{
|
{
|
||||||
return m_valid;
|
return ::qHash(std::hash<InfoHash::UnderlyingType>()(key), seed);
|
||||||
}
|
|
||||||
|
|
||||||
InfoHash::operator lt::sha1_hash() const
|
|
||||||
{
|
|
||||||
return m_nativeHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
InfoHash InfoHash::fromString(const QString &hashString)
|
|
||||||
{
|
|
||||||
if (hashString.size() != (length() * 2))
|
|
||||||
return {};
|
|
||||||
|
|
||||||
const QByteArray raw = QByteArray::fromHex(hashString.toLatin1());
|
|
||||||
if (raw.size() != length()) // QByteArray::fromHex() will skip over invalid characters
|
|
||||||
return {};
|
|
||||||
|
|
||||||
InfoHash result;
|
|
||||||
result.m_valid = true;
|
|
||||||
result.m_hashString = hashString;
|
|
||||||
result.m_nativeHash.assign(raw.constData());
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString InfoHash::toString() const
|
|
||||||
{
|
|
||||||
return m_hashString;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BitTorrent::operator==(const InfoHash &left, const InfoHash &right)
|
|
||||||
{
|
|
||||||
return (static_cast<lt::sha1_hash>(left)
|
|
||||||
== static_cast<lt::sha1_hash>(right));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BitTorrent::operator!=(const InfoHash &left, const InfoHash &right)
|
|
||||||
{
|
|
||||||
return !(left == right);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BitTorrent::operator<(const InfoHash &left, const InfoHash &right)
|
|
||||||
{
|
|
||||||
return static_cast<lt::sha1_hash>(left) < static_cast<lt::sha1_hash>(right);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint BitTorrent::qHash(const InfoHash &key, const uint seed)
|
|
||||||
{
|
|
||||||
return ::qHash((std::hash<lt::sha1_hash> {})(key), seed);
|
|
||||||
}
|
}
|
||||||
|
@ -28,42 +28,25 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <libtorrent/sha1_hash.hpp>
|
#include <QHash>
|
||||||
|
|
||||||
#include <QMetaType>
|
#include <QMetaType>
|
||||||
#include <QString>
|
|
||||||
|
#include "base/digest32.h"
|
||||||
|
|
||||||
|
using SHA1Hash = Digest32<160>;
|
||||||
|
using SHA256Hash = Digest32<256>;
|
||||||
|
|
||||||
namespace BitTorrent
|
namespace BitTorrent
|
||||||
{
|
{
|
||||||
class InfoHash
|
class InfoHash : public SHA1Hash
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InfoHash() = default;
|
using SHA1Hash::SHA1Hash;
|
||||||
InfoHash(const lt::sha1_hash &nativeHash);
|
|
||||||
InfoHash(const InfoHash &other) = default;
|
|
||||||
|
|
||||||
static constexpr int length()
|
|
||||||
{
|
|
||||||
return lt::sha1_hash::size();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isValid() const;
|
|
||||||
|
|
||||||
operator lt::sha1_hash() const;
|
|
||||||
|
|
||||||
static InfoHash fromString(const QString &hashString);
|
static InfoHash fromString(const QString &hashString);
|
||||||
QString toString() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool m_valid = false;
|
|
||||||
lt::sha1_hash m_nativeHash;
|
|
||||||
QString m_hashString;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool operator==(const InfoHash &left, const InfoHash &right);
|
uint qHash(const InfoHash &key, const uint seed);
|
||||||
bool operator!=(const InfoHash &left, const InfoHash &right);
|
|
||||||
bool operator<(const InfoHash &left, const InfoHash &right);
|
|
||||||
uint qHash(const InfoHash &key, uint seed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(BitTorrent::InfoHash)
|
Q_DECLARE_METATYPE(BitTorrent::InfoHash)
|
||||||
|
121
src/base/digest32.h
Normal file
121
src/base/digest32.h
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* Bittorrent Client using Qt and libtorrent.
|
||||||
|
* Copyright (C) 2015, 2021 Vladimir Golovnev <glassez@yandex.ru>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
* In addition, as a special exception, the copyright holders give permission to
|
||||||
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
||||||
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
||||||
|
* and distribute the linked executables. You must obey the GNU General Public
|
||||||
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
||||||
|
* modify file(s), you may extend this exception to your version of the file(s),
|
||||||
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
||||||
|
* exception statement from your version.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <libtorrent/sha1_hash.hpp>
|
||||||
|
|
||||||
|
#include <QByteArray>
|
||||||
|
#include <QHash>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
class Digest32
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using UnderlyingType = lt::digest32<N>;
|
||||||
|
|
||||||
|
Digest32() = default;
|
||||||
|
Digest32(const Digest32 &other) = default;
|
||||||
|
|
||||||
|
Digest32(const UnderlyingType &nativeDigest)
|
||||||
|
: m_valid {true}
|
||||||
|
, m_nativeDigest {nativeDigest}
|
||||||
|
{
|
||||||
|
const QByteArray raw = QByteArray::fromRawData(nativeDigest.data(), length());
|
||||||
|
m_hashString = QString::fromLatin1(raw.toHex());
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr int length()
|
||||||
|
{
|
||||||
|
return UnderlyingType::size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isValid() const
|
||||||
|
{
|
||||||
|
return m_valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator UnderlyingType() const
|
||||||
|
{
|
||||||
|
return m_nativeDigest;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Digest32 fromString(const QString &digestString)
|
||||||
|
{
|
||||||
|
if (digestString.size() != (length() * 2))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
const QByteArray raw = QByteArray::fromHex(digestString.toLatin1());
|
||||||
|
if (raw.size() != length()) // QByteArray::fromHex() will skip over invalid characters
|
||||||
|
return {};
|
||||||
|
|
||||||
|
Digest32 result;
|
||||||
|
result.m_valid = true;
|
||||||
|
result.m_hashString = digestString;
|
||||||
|
result.m_nativeDigest.assign(raw.constData());
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString toString() const
|
||||||
|
{
|
||||||
|
return m_hashString;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_valid = false;
|
||||||
|
UnderlyingType m_nativeDigest;
|
||||||
|
QString m_hashString;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool operator==(const Digest32<N> &left, const Digest32<N> &right)
|
||||||
|
{
|
||||||
|
return (static_cast<typename Digest32<N>::UnderlyingType>(left)
|
||||||
|
== static_cast<typename Digest32<N>::UnderlyingType>(right));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool operator!=(const Digest32<N> &left, const Digest32<N> &right)
|
||||||
|
{
|
||||||
|
return !(left == right);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool operator<(const Digest32<N> &left, const Digest32<N> &right)
|
||||||
|
{
|
||||||
|
return static_cast<typename Digest32<N>::UnderlyingType>(left)
|
||||||
|
< static_cast<typename Digest32<N>::UnderlyingType>(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
uint qHash(const Digest32<N> &key, const uint seed)
|
||||||
|
{
|
||||||
|
return ::qHash(std::hash<typename Digest32<N>::UnderlyingType>()(key), seed);
|
||||||
|
}
|
@ -29,6 +29,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QSortFilterProxyModel>
|
#include <QSortFilterProxyModel>
|
||||||
|
|
||||||
#include "base/torrentfilter.h"
|
#include "base/torrentfilter.h"
|
||||||
|
|
||||||
namespace BitTorrent
|
namespace BitTorrent
|
||||||
|
Loading…
x
Reference in New Issue
Block a user