Define template for classes that represent SHA hashes

This commit is contained in:
Vladimir Golovnev (Glassez) 2021-03-02 19:44:57 +03:00
parent 8cfd803222
commit bea32cfe38
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
6 changed files with 138 additions and 87 deletions

View file

@ -32,6 +32,7 @@ add_library(qbt_base STATIC
bittorrent/torrentinfo.h
bittorrent/tracker.h
bittorrent/trackerentry.h
digest32.h
exceptions.h
filesystemwatcher.h
global.h

View file

@ -31,6 +31,7 @@ HEADERS += \
$$PWD/bittorrent/torrentinfo.h \
$$PWD/bittorrent/tracker.h \
$$PWD/bittorrent/trackerentry.h \
$$PWD/digest32.h \
$$PWD/exceptions.h \
$$PWD/filesystemwatcher.h \
$$PWD/global.h \

View file

@ -28,70 +28,14 @@
#include "infohash.h"
#include <QByteArray>
#include <QHash>
const int InfoHashTypeId = qRegisterMetaType<BitTorrent::InfoHash>();
using namespace BitTorrent;
const int InfoHashTypeId = qRegisterMetaType<InfoHash>();
InfoHash::InfoHash(const lt::sha1_hash &nativeHash)
: m_valid {true}
, m_nativeHash {nativeHash}
BitTorrent::InfoHash BitTorrent::InfoHash::fromString(const QString &hashString)
{
const QByteArray raw = QByteArray::fromRawData(nativeHash.data(), length());
m_hashString = QString::fromLatin1(raw.toHex());
return {SHA1Hash::fromString(hashString)};
}
bool InfoHash::isValid() const
uint BitTorrent::qHash(const BitTorrent::InfoHash &key, const uint seed)
{
return m_valid;
}
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);
return ::qHash(std::hash<InfoHash::UnderlyingType>()(key), seed);
}

View file

@ -28,42 +28,25 @@
#pragma once
#include <libtorrent/sha1_hash.hpp>
#include <QHash>
#include <QMetaType>
#include <QString>
#include "base/digest32.h"
using SHA1Hash = Digest32<160>;
using SHA256Hash = Digest32<256>;
namespace BitTorrent
{
class InfoHash
class InfoHash : public SHA1Hash
{
public:
InfoHash() = default;
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;
using SHA1Hash::SHA1Hash;
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);
bool operator!=(const InfoHash &left, const InfoHash &right);
bool operator<(const InfoHash &left, const InfoHash &right);
uint qHash(const InfoHash &key, uint seed);
uint qHash(const InfoHash &key, const uint seed);
}
Q_DECLARE_METATYPE(BitTorrent::InfoHash)

121
src/base/digest32.h Normal file
View 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);
}

View file

@ -29,6 +29,7 @@
#pragma once
#include <QSortFilterProxyModel>
#include "base/torrentfilter.h"
namespace BitTorrent