File indexing completed on 2024-11-24 04:49:03

0001 /*
0002    SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "gravatar_private_export.h"
0010 
0011 #include <cstdint>
0012 #include <cstring>
0013 
0014 class QByteArray;
0015 class QString;
0016 
0017 namespace Gravatar
0018 {
0019 template<int Size>
0020 struct UnsignedInt {
0021     bool operator<(const UnsignedInt<Size> &other) const
0022     {
0023         return memcmp(data, other.data, Size) < 0;
0024     }
0025 
0026     bool operator==(const UnsignedInt<Size> &other) const
0027     {
0028         return memcmp(data, other.data, Size) == 0;
0029     }
0030 
0031     uint8_t data[Size];
0032 };
0033 struct Hash128 : public UnsignedInt<16> {
0034 };
0035 struct Hash256 : public UnsignedInt<32> {
0036 };
0037 
0038 class Hash;
0039 size_t qHash(const Hash &h, size_t seed = 0) noexcept;
0040 
0041 // exported for unit tests only
0042 class GRAVATAR_TESTS_EXPORT Hash
0043 {
0044 public:
0045     enum Type { Invalid, Md5, Sha256 };
0046     Hash();
0047     explicit Hash(const QByteArray &data, Type type);
0048 
0049     bool operator==(const Hash &other) const;
0050 
0051     bool isValid() const;
0052 
0053     Type type() const;
0054     Hash128 md5() const;
0055     Hash256 sha256() const;
0056 
0057     QString hexString() const;
0058 
0059 private:
0060     friend size_t qHash(const Hash &h, size_t seed) noexcept;
0061     union {
0062         Hash128 md5;
0063         Hash256 sha256;
0064     } m_hash;
0065     Type m_type;
0066 };
0067 }