Changeset View
Changeset View
Standalone View
Standalone View
src/misc/hash.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2017 Volker Krause <vkrause@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #ifndef GRAVATAR_HASH_H | ||||
21 | #define GRAVATAR_HASH_H | ||||
22 | | ||||
23 | #include "gravatar_export.h" | ||||
24 | | ||||
25 | #include <cstdint> | ||||
26 | #include <cstring> | ||||
27 | | ||||
28 | class QByteArray; | ||||
29 | class QString; | ||||
30 | | ||||
31 | namespace Gravatar { | ||||
32 | | ||||
33 | template <int Size> struct UnsignedInt | ||||
34 | { | ||||
35 | bool operator<(const UnsignedInt<Size> &other) const | ||||
36 | { | ||||
37 | return memcmp(data, other.data, Size) < 0; | ||||
38 | } | ||||
39 | | ||||
40 | bool operator==(const UnsignedInt<Size> &other) const | ||||
41 | { | ||||
42 | return memcmp(data, other.data, Size) == 0; | ||||
43 | } | ||||
44 | | ||||
45 | uint8_t data[Size]; | ||||
46 | }; | ||||
47 | struct Hash128 : public UnsignedInt<16> {}; | ||||
48 | struct Hash256 : public UnsignedInt<32> {}; | ||||
49 | | ||||
50 | class Hash; | ||||
51 | unsigned int qHash(const Hash &h, unsigned int seed = 0); | ||||
52 | | ||||
53 | // exported for unit tests only | ||||
54 | class GRAVATAR_EXPORT Hash | ||||
55 | { | ||||
56 | public: | ||||
57 | enum Type { Invalid, Md5, Sha256 }; | ||||
58 | Hash(); | ||||
59 | explicit Hash(const QByteArray &data, Type type); | ||||
60 | | ||||
61 | bool operator==(const Hash& other) const; | ||||
62 | | ||||
63 | bool isValid() const; | ||||
64 | | ||||
65 | Type type() const; | ||||
66 | Hash128 md5() const; | ||||
67 | Hash256 sha256() const; | ||||
68 | | ||||
69 | QString hexString() const; | ||||
70 | | ||||
71 | private: | ||||
72 | friend unsigned int qHash(const Hash &h, unsigned int seed); | ||||
73 | union { | ||||
74 | Hash128 md5; | ||||
75 | Hash256 sha256; | ||||
76 | } m_hash; | ||||
77 | Type m_type; | ||||
78 | }; | ||||
79 | | ||||
80 | } | ||||
81 | | ||||
82 | #endif // GRAVATAR_HASH_H |